将图像保存到文件

我正在研究一个基本的绘图应用程序。 我希望用户能够保存图像的内容。

在此处输入图像描述

我以为我应该用

System.Drawing.Drawing2D.GraphicsState img = drawRegion.CreateGraphics().Save(); 

但这对我保存到文件没有帮助。

您可以尝试使用此方法保存图像

 SaveFileDialog dialog = new SaveFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { int width = Convert.ToInt32(drawImage.Width); int height = Convert.ToInt32(drawImage.Height); Bitmap bmp = new Bitmap(width,height); drawImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height); bmp.Save(dialog.FileName, ImageFormat.Jpeg); } 

您可以尝试使用此代码

  Image.Save("myfile.png",ImageFormat.Png) 

链接: http : //msdn.microsoft.com/en-us/library/ms142147.aspx

如果您正在绘制控件的图形,那么您应该在Bitmap上绘制在canvas上绘制的所有内容,但请记住,Bitmap需要是您正在绘制的控件的确切大小:

  Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width,myControl.ClientRectangle.Height); Graphics gBmp = Graphics.FromImage(bmp); gBmp.DrawEverything(); //this is your code for drawing gBmp.Dispose(); bmp.Save("image.png", ImageFormat.Png); 

或者您可以使用Control的DrawToBitmap方法。 像这样的东西:

 Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width, myControl.ClientRectangle.Height); myControl.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height)); bmp.Save("image.png", ImageFormat.Png); 

您可以保存图像,将文件保存在当前目录应用程序中并将文件移动到任何目录。

 Bitmap btm = new Bitmap(image.width,image.height); Image img = btm; img.Save(@"img_" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); FileInfo img__ = new FileInfo(@"img_" + x + ".jpg"); img__.MoveTo("myVideo\\img_" + x + ".jpg");