本文整理汇总了C#中Bitmap.Save方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.Save方法的具体用法?C# Bitmap.Save怎么用?C# Bitmap.Save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: resize_pic
public static void resize_pic(int width, int height)
{
int i = 0;
Image img = null;
Image bmcpy = null;
Graphics gh = null;
Directory.CreateDirectory("icon");
string szcdir = Environment.CurrentDirectory;
string[] szfiles = Directory.GetFiles(szcdir);
foreach (string szfile in szfiles)
{
string sz_ex = Path.GetExtension(szfile);
if (sz_ex == ".jpg" || sz_ex == ".bmp" || sz_ex == ".png" || sz_ex == ".gif")
{
img = Image.FromFile(szfile);
bmcpy = new Bitmap(width, height);
gh = Graphics.FromImage(bmcpy);
gh.DrawImage(img, new Rectangle(0, 0, width, height));
bmcpy.Save("icon\\" + i.ToString() + ".jpg", ImageFormat.Jpeg);
i++;
}
}
Console.WriteLine("{0} pictures have been resized", i);
}
示例2: convertirGris
//Función que convierte una imagen a escala de grises
public void convertirGris(string imagefrom, string imageto)
{
//create a blank bitmap the same size as original
Bitmap original = new Bitmap(imagefrom);
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {.30f, .30f, .30f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
newBitmap.Save(imageto);
}
示例3: CreateImage
/// <summary>
/// ����ͼƬ
/// </summary>
/// <param name="checkCode">�����</param>
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);//����������趨ͼƬ���
Bitmap image = new Bitmap(iwidth, 20);//����һ������
Graphics g = Graphics.FromImage(image);//�ڻ����϶����ͼ��ʵ��
Font f = new Font("Arial",10,FontStyle.Bold);//���壬��С����ʽ
Brush b = new SolidBrush(Color.Black);//������ɫ
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.White);//������ɫ
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
/*�����
for (int i = 0; i < 5; i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen, 0, y, image.Width, y);
}
*/
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
示例4: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
string url = context.Request.RawUrl;
string path = context.Request.MapPath(url);
using (Image priImg = Image.FromFile(path))
{
int width = 660;
int height = 350;
if (priImg.Width > priImg.Height)
{
height = width * priImg.Height / priImg.Width;
}
else
{
width = height * priImg.Width / priImg.Height;
}
path = context.Request.MapPath("logo.png");
using (Image logo = Image.FromFile(path))
{
using (Bitmap bm = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bm))
{
g.DrawImage(priImg, 0, 0, bm.Width, bm.Height);
g.DrawImage(logo, bm.Width - logo.Width, bm.Height - logo.Height, logo.Width, logo.Height);
bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
示例5: CreateImage
private void CreateImage()
{
Session["captcha.guid"] = Guid.NewGuid().ToString ("N");
string code = GetRandomText();
Bitmap bitmap = new Bitmap(WIDTH,HEIGHT,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.DarkSlateGray);
Rectangle rect = new Rectangle(0,0,WIDTH,HEIGHT);
SolidBrush background = new SolidBrush(Color.AntiqueWhite);
SolidBrush textcolor = new SolidBrush(Color.DarkSlateGray);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(background, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(),
new Font("Verdana", 10 + rand.Next(6, 14)),
textcolor,
new PointF(10 + counter, 10));
counter += 25;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream,ImageFormat.Gif);
g.Dispose();
bitmap.Dispose();
}
示例6: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
int num = 4;
Bitmap generatedImage = new Bitmap(200, 200);
using (generatedImage)
{
Graphics gr = Graphics.FromImage(generatedImage);
using (gr)
{
var fonttt = new Font("Arial", 14, FontStyle.Bold);
gr.FillRectangle(Brushes.MediumSeaGreen, 0, 0, 200, 200);
gr.FillPie(Brushes.Yellow, 25, 25, 150, 150, 0, 45);
gr.FillPie(Brushes.Green, 25, 25, 150, 150, 45, 315);
gr.DrawString(num.ToString(), fonttt, SystemBrushes.WindowText, new PointF(10, 40));
// Set response header and write the image into response stream
Response.ContentType = "image/gif";
//Response.AppendHeader("Content-Disposition",
// "attachment; filename=\"Financial-Report-April-2013.gif\"");
generatedImage.Save(Response.OutputStream, ImageFormat.Gif);
}
}
}
示例7: TakeScreenshot
private void TakeScreenshot(TestContext testContext)
{
var filename = Path.GetFullPath(testContext.FullyQualifiedTestClassName + ".jpg");
try
{
var screenLeft = SystemInformation.VirtualScreen.Left;
var screenTop = SystemInformation.VirtualScreen.Top;
var screenWidth = SystemInformation.VirtualScreen.Width;
var screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the screenshot.
using (var bmp = new Bitmap(screenWidth, screenHeight))
{
// Draw the screenshot into our bitmap.
using (var g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
}
bmp.Save(filename, ImageFormat.Jpeg);
}
testContext.AddResultFile(filename);
}
catch (Exception ex)
{
Logger.WriteLine("An exception occured while trying to take screenshot:");
Logger.WriteLine(ex);
}
}
示例8: returnNumer
private void returnNumer()
{
/*
Random num1 = new Random();
Random num2 = new Random();
int numQ1, numQ2;
string QString;
numQ1 = num1.Next(10, 15);
numQ2 = num1.Next(17, 31);
QString = numQ1.ToString() + " + " + numQ2.ToString() + " = ";
*/
HttpContext.Current.Session["answer"] = GetRandomString();
Bitmap bitmap = new Bitmap(85, 35);
Graphics Grfx = Graphics.FromImage(bitmap);
Font font = new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel);
Rectangle Rect = new Rectangle(0, 0, 100, 50);
Grfx.FillRectangle(Brushes.Brown, Rect);
Grfx.DrawRectangle(Pens.PeachPuff, Rect);
Grfx.DrawString(HttpContext.Current.Session["answer"].ToString(), font, Brushes.Azure, 0, 8);
//Session["answer"] = numQ1 + numQ2;
Response.ContentType = "Image/jpeg";
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
Grfx.Dispose();
}
示例9: Change
public static void Change()
{
Bitmap myBitmap;
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Create a Bitmap object based on a BMP file.
myBitmap = new Bitmap(@"D:\Shapes.jpg");
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(@"c:\temp.jpg", myImageCodecInfo, myEncoderParameters);
Byte[] bytes = File.ReadAllBytes(@"c:\temp.jpg");
// Save the bitmap as a JPEG file with quality level 50.
//myEncoderParameter = new EncoderParameter(myEncoder, 50L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//myBitmap.Save(@"D:\Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
//// Save the bitmap as a JPEG file with quality level 75.
//myEncoderParameter = new EncoderParameter(myEncoder, 75L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//myBitmap.Save(@"D:\Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
}
示例10: Page_Load
protected void Page_Load(object sender, System.EventArgs e)
{
// Create the in-memory bitmap where you will draw the image.
// This bitmap is 300 pixels wide and 50 pixels high.
Bitmap image = new Bitmap(300, 50);
Graphics g = Graphics.FromImage(image);
// Draw a solid white rectangle..
// Start from point (1, 1).
// Make it 298 pixels wide and 48 pixels high.
g.FillRectangle(Brushes.White, 0, 0, 300, 50);
g.DrawRectangle(Pens.Green, 0, 0, 299, 49);
// Draw some text using a fancy font.
Font font = new Font("Impact", 20, FontStyle.Regular);
g.DrawString("This is a test.", font, Brushes.Blue, 10, 5);
// Render the image to the HTML output stream.
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
}
示例11: ValidateCode
private void ValidateCode( string VNum )
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 10;
Img = new Bitmap( gheight, 15 );
g = Graphics.FromImage( Img );
//背景颜色
g.Clear( Color.White );
//文字字体
Font f = new Font( "宋体", 10 );
//文字颜色
SolidBrush s = new SolidBrush( Color.Red );
g.DrawString( VNum, f, s, 3, 3 );
ms = new MemoryStream();
Img.Save( ms, ImageFormat.Jpeg );
Response.ClearContent();
Response.ContentType = "images/Jpeg";
Response.BinaryWrite( ms.ToArray() );
g.Dispose();
Img.Dispose();
Response.End();
}
示例12: CropFile
public void CropFile(Int32 X, Int32 Y, Int32 Width, Int32 Height)
{
string ppname = hidPicName.Value.ToString();
string pathToImage = Server.MapPath("~/fooPicDB/tmp/") + ppname;
System.Drawing.Image img = System.Drawing.Image.FromFile(pathToImage);
Bitmap bmpCropped = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(bmpCropped);
Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
Rectangle rectCropArea = new Rectangle(X, Y, Width, Height);
g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel);
g.Dispose();
ppname = "Cover" + Session["UserName"].ToString() + ".jpg";
string profilePicPath = Server.MapPath("~/fooPicDB/" + Session["UserName"].ToString() + "/Profile/"+ppname.ToString());
bmpCropped.Save(profilePicPath, System.Drawing.Imaging.ImageFormat.Jpeg);
String cmdStr = "UPDATE General_Information SET CoverPic = '" + ppname + "' WHERE UName = '" + myUserID + "'";
SqlCommand scom = new SqlCommand(cmdStr, con);
con.Open();
scom.ExecuteNonQuery();
con.Close();
}
示例13: CropImageFile
public void CropImageFile(string savePath, string sPhysicalPath, string sOrgFileName, string sThumbNailFileName, ImageFormat oFormat, int targetW, int targetH, int targetX, int targetY)
{
try
{
Size tsize = new Size(targetW, targetH);
System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath + @"\" + sOrgFileName);
System.Drawing.Image oThumbNail = new Bitmap(tsize.Width, tsize.Height, oImg.PixelFormat);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
oGraphic.DrawImage(oImg, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);
oThumbNail.Save(savePath + @"\" + sThumbNailFileName, oFormat);
oGraphic.Dispose();
oThumbNail.Dispose();
oImg.Dispose();
drag.Visible = false;
ddFiles2.SelectedItem.Text = sThumbNailFileName;
System.Drawing.Image origImg = System.Drawing.Image.FromFile(Server.MapPath("~/App_Uploads_Img/") + ddCat3.SelectedItem.Text + "/" + sThumbNailFileName);
divimage.InnerHtml = "<img src=" + epicCMSLib.Navigation.SiteRoot + "App_Uploads_Img/" + ddCat3.SelectedItem.Text + "/" + ddFiles2.SelectedItem.Text + "></img>";
Label8.Text = "Your selected Image's Width is " + origImg.Width.ToString() + "px and Height is " + origImg.Height.ToString() + "px.";
Hidden1.Value = origImg.Width.ToString();
Hidden2.Value = origImg.Height.ToString();
UpdatePanel3.Update();
lbSuccess3.ForeColor = System.Drawing.Color.Green;
lbSuccess3.Text = "Image " + sThumbNailFileName + " created.";
}
catch (Exception e1) { errorlabelcrop.ForeColor = System.Drawing.Color.Red;
errorlabelcrop.Text = e1.Message; }
}
示例14: GetWindowRect
String CaptureNow
()
{
if (InitGeometryReflectionInfo())
update_display.Invoke(null, new object[]{});
IntPtr main_window = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
RECT rect;
GetWindowRect(main_window, out rect);
int width = (int)(rect.Right - rect.Left);
int height = (int)(rect.Bottom - rect.Top);
string filename = BuildFileName();
using (Bitmap image = new Bitmap(width, height))
{
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen((int)(rect.Left), (int)(rect.Top), 0, 0, new System.Drawing.Size(width, height));
image.Save(filename);
}
counter++;
return filename;
}
示例15: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
var fcaptcha = MapPath("~/Css/Login-Box/Captcha.bmp");
var bmpCaptcha = new Bitmap(fcaptcha);
var g = Graphics.FromImage(bmpCaptcha);
var code = RandomizeText(5);
var gray = new SolidBrush(Color.DimGray);
var rand = new Random();
var counter = 0;
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(),
new Font("Verdena", 10 + rand.Next(20)),
gray, new PointF(40 + counter, 10));
counter += 20;
}
// Assign Code
Session["Captcha"] = code;
// Response
Response.Clear();
Response.ContentType = "image/jpeg";
bmpCaptcha.Save(Response.OutputStream, ImageFormat.Jpeg);
bmpCaptcha.Dispose();
g.Dispose();
Response.End();
}