GDI +中发生了一般错误

我使用以下方法将图像加载到Picture Box中:

picturebox1.Image = Image.FromFile() 

我用以下方法保存它:

 Bitmap bm = new Bitmap(pictureBox1.Image); bm.Save(FileName, ImageFormat.Bmp); 

它在创建新文件时工作得非常好,但是当我尝试替换现有图像时,我会抛出以下运行时错误:

GDI +中发生了一般错误

那么我该怎么做才能解决这个问题呢?

因为图片文件由picturebox1.Image使用,请尝试将其保存到不同的文件路径:

 picturebox1.Image = Image.FromFile(FileName); Bitmap bm = new Bitmap(pictureBox1.Image); bm.Save(@"New File Name", ImageFormat.Bmp); 

编辑:您还可以在第一个位置添加图像副本,如:

 picturebox1.Image = new Bitmap(Image.FromFile(FileName)); Bitmap bm = new Bitmap(pictureBox1.Image); bm.Save(FileName, ImageFormat.Bmp);//no error will occurs here. 

FromFile方法锁定文件,因此使用Image.FromStream()方法读取图像:

 byte[] bytes = System.IO.File.ReadAllBytes(filename); System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes); pictureBox1.Image = Image.FromStream(ms); 

然后像以前一样保存。

如果路径不存在,也会发生这种情况。

您可以使用以下命令创建目录:

 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FileName)); 

试试这个。

 picturebox1.Image = Image.FromFile(FileName); Bitmap bm = new Bitmat(pictureBox1.Image); Image img = (Image)b; img.Save(FileName, ImageFormat.Bmp); 

当从文件构造Bitmap对象或Image对象时,该文件在对象的生命周期内保持锁定状态。 因此,您无法更改图像并将其保存回原始位置的同一文件中。 http://support.microsoft.com/?id=814675

GDI +,JPEG Image to MemoryStream中发生一般错误:

 Image.Save(..) // throws a GDI+ exception because the memory stream is closed 

http://sofzh.miximages.com/c%23/ t objPropBannerImage.ImageId, ImageFormat.Jpeg);

有类似的东西:

 string outputFileName = "..."; using (MemoryStream memory = new MemoryStream()) { using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)) { thumbBMP.Save(memory, ImageFormat.Jpeg); byte[] bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); } } 

就像@Jalal Aldeen Saa’d所说的那样,图片框正在使用该文件并锁定文件替换。

 //unlock file by clearing it from picture box if (picturebox1.Image != null) { picturebox1.Image.Dispose(); picturebox1.Image = null; } //put back the picture inside the pictureBox? 

试试这个会起作用

 public void SavePicture() { Bitmap bm = new Bitmap(this.myBitmap) bm.Save("Output\\out.bmp" ,System.Drawing.Imaging.ImageFormat.Bmp ); } 

如果您忘记添加文件名,也会发生这种情况:

 bm.Save(@"C:\Temp\Download", System.Drawing.Imaging.ImageFormat.Png); 

并且可以通过添加文件名来修复:

 bm.Save(@"C:\Temp\Download\Image.png", System.Drawing.Imaging.ImageFormat.Png); 

注意:您实际上不必为其添加扩展名。

试试这个:

 private void LoadPictureBoxWithImage( string ImagePath) { Stream objInputImageStream = null; BitmapData bmdImageData = null; Bitmap bmpSrcImage = null, bmTemp = null; byte[] arrImageBytes = null; int bppModifier = 3; try { objInputImageStream = new MemoryStream(); using (FileStream objFile = new FileStream(ImagePath, FileMode.Open, FileAccess.Read)) { objFile.CopyTo(objInputImageStream); } bmpSrcImage = new Bitmap(objInputImageStream); bppModifier = bmpSrcImage.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4; //reda from byte[] to bitmap bmdImageData = bmpSrcImage.LockBits(new Rectangle(0, 0, bmpSrcImage.Width, bmpSrcImage.Height), ImageLockMode.ReadOnly, bmpSrcImage.PixelFormat); arrImageBytes = new byte[Math.Abs(bmdImageData.Stride) * bmpSrcImage.Height]; System.Runtime.InteropServices.Marshal.Copy(bmdImageData.Scan0, arrImageBytes, 0, arrImageBytes.Length); bmpSrcImage.UnlockBits(bmdImageData); pbSetup.Image = (Bitmap)bmpSrcImage.Clone(); pbSetup.Refresh(); } catch (Exception ex) { throw new Exception("Error in Function " + System.Reflection.MethodInfo.GetCurrentMethod().Name + "; " + ex.Message); } finally { if (objInputImageStream != null) { objInputImageStream.Dispose(); objInputImageStream = null; } if (bmdImageData != null) { bmdImageData = null; } if (bmpSrcImage != null) { bmpSrcImage.Dispose(); bmpSrcImage = null; } if (bmTemp != null) { bmTemp.Dispose(); bmTemp = null; } if (arrImageBytes != null) { arrImageBytes = null; } } }