简体   繁体   English

保存图像BMP C#

[英]Saving an image bmp c#

I have this function that draw an image in bmp format 我有此功能以bmp格式绘制图像

private void DrawImage(Byte[] imgData, PictureBox picBox)
  {
     int colorval;
     Bitmap bmp = new Bitmap(m_ImageWidth, m_ImageHeight);
     picBox.Image = (Image)bmp;

     for (int i=0; i<bmp.Width; i++)
     {
        for (int j=0; j<bmp.Height; j++)
        {
           colorval = (int)imgData[(j*m_ImageWidth)+ i];
           bmp.SetPixel(i,j,Color.FromArgb(colorval,colorval, colorval));
        }
     }
     picBox.Refresh();

    }

When i add picBox.Image.Save(@"C:\\Users\\Jose Moreno\\Desktop", System.Drawing.Imaging.ImageFormat.Bmp); 当我添加picBox.Image.Save(@"C:\\Users\\Jose Moreno\\Desktop", System.Drawing.Imaging.ImageFormat.Bmp);

below PicBox.Refresh();, the program crash with the errror: "An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll" 在PicBox.Refresh();之下,程序会因错误而崩溃:“ System.Drawing.dll中发生了'System.Runtime.InteropServices.ExternalException类型的未处理的异常”

Why the program crash? 为什么程序崩溃? thatks for the help. 寻求帮助。

Image.Save(String, ImageFormat) requires the full filename , not just the file path . Image.Save(String, ImageFormat)需要完整的文件名 ,而不仅仅是文件路径 "C:\\Users\\Jose Moreno\\Desktop" is (presumably) the name of a pre-existing directory, not the name of a file that could be created. "C:\\Users\\Jose Moreno\\Desktop" (大概是)现有目录的名称,而不是可以创建的文件的名称。 You need to pass an actual file name, for instance: 您需要传递一个实际的文件名,例如:

picBox.Image.Save(@"C:\Users\Jose Moreno\Desktop\TestFilename.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

If a file of the specified name cannot be created, you will get this inscrutable error. 如果无法创建指定名称的文件,则会出现此难以理解的错误。

Note that if the image was originally loaded from a file, you cannot save back to the same file until the image is disposed of because the file gets locked. 请注意,如果图像最初是从文件加载的,则在文件被锁定之前,您无法保存回同一文件,直到该图像被处理为止。 See How can I fix this GDI+ generic exception when I save images? 请参阅保存图像时如何解决此GDI +通用异常? . But that's not the problem shown in your question. 但这不是您的问题中显示的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM