C# 将图像保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12909905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:56:15  来源:igfitidea点击:

Saving image to file

c#winformsimagegdi+save

提问by Victor

I am working on a basic drawing application. I want the user to be able to save the contents of the image.

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

enter image description here

在此处输入图片说明

I thought I should use

我想我应该用

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

but this does not help me for saving to file.

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

采纳答案by Steve

You could try to save the image using this approach

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

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);
}

回答by Aghilas Yakoub

You can try with this code

您可以尝试使用此代码

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

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

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

回答by Nikola Davidovic

If you are drawing on the Graphics of the Control than you should do something draw on the Bitmap everything you are drawing on the canvas, but have in mind that Bitmap needs to be the exact size of the control you are drawing on:

如果您在控件的图形上绘制,那么您应该在位图上绘制您在画布上绘制的所有内容,但请记住,位图需要与您正在绘制的控件的确切大小相同:

  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);

Or you can use a DrawToBitmapmethod of the Control. Something like this:

或者你可以使用DrawToBitmapControl 的一个方法。像这样的东西:

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);

回答by Mahmmoud Kassem

You can save image , save the file in your current directory application and move the file to any directory .

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

 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");