在 C# 中保存图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2290644/
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-07 01:07:36  来源:igfitidea点击:

save image files in C#

c#.netimagesave

提问by

How can we save image files (types such as jpg or png) in C#?

我们如何在 C# 中保存图像文件(jpg 或 png 等类型)?

采纳答案by Chris Jones

in c# us the Image.Save Method with these parameters (string Filename , ImageFormat)

在 c# 中,我们使用带有这些参数的 Image.Save 方法(字符串 Filename ,ImageFormat)

http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx

http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx

Is that all you needed?

这就是你所需要的吗?

// Construct a bitmap from the button image resource.
Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

// Save the image as a GIF.
bmp1.Save("c:\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

回答by RSolberg

Image bitmap = Image.FromFile("C:\MyFile.bmp");
bitmap.Save("C:\MyFile2.bmp");  

You should be able to use the Save Methodfrom the Image Classand be just fine as shown above. The Save Method has 5 different options or overloads...

您应该能够使用Image 类中Save 方法,并且如上所示。Save 方法有 5 个不同的选项或重载...

  //Saves this Image  to the specified file or stream.
  img.Save(filePath);

  //Saves this image to the specified stream in the specified format.
  img.Save(Stream, ImageFormat);

  //Saves this Image to the specified file in the specified format.
  img.Save(String, ImageFormat);

  //Saves this image to the specified stream, with the specified encoder and image encoder parameters.
  img.Save(Stream, ImageCodecInfo, EncoderParameters);

  //Saves this Image to the specified file, with the specified encoder and image-encoder parameters.
  img.Save(String, ImageCodecInfo, EncoderParameters);

回答by Eric J.

If you need more extensive image handling than the .Net Framework provides out of the box, check out the FreeImageproject

如果您需要比 .Net Framework 开箱即用的更广泛的图像处理,请查看FreeImage项目

回答by A.Brit

            SaveFileDialog sv = new SaveFileDialog();
            sv.Filter = "Images|*.jpg ; *.png ; *.bmp";
            ImageFormat format = ImageFormat.Jpeg;

            if (sv.ShowDialog() == DialogResult.OK)
            {

                switch (sv.Filter )
                {
                    case ".jpg":

                        format = ImageFormat.Png;
                        break;

                    case ".bmp":

                        format = ImageFormat.Bmp;
                        break;
                }


                pictureBox.Image.Save(sv.FileName, format);
            }