2011-08-18 88 views
19

我使用加载图像插入图片框:一般性错误在GDI +

picturebox1.Image = Image.FromFile() 

,我保存它用:

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

创建一个新的文件时,它工作完全正常,但是当我试图取代现有的形象,我抛出以下运行时错误:

A generic error occurred in GDI+

所以我能做些什么来解决这个问题??

回答

15

这是因为图像文件被你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. 
+1

感谢。如果我想替换,我不能那样做吗? – Lakshani

+0

如果您想要替换,您应该先从'pictureBox.Image'中移除图像,然后替换,然后重新添加到'pictureBox.Image',您还可以在图片框中添加图像的副本在第一个地方... –

+0

@Lakshani:不要忘记标记答案,回答你的问题是[接受的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting- an-answer-work/5235#5235)所以别人会知道你的问题是如何解决的。 –

1

试试这个。

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

在这段代码中,发生了这个问题。 – Lakshani

+0

你必须先定义openfiledialog。比从文件读取图像并使用这些代码。它会对你有所帮助。 – TheMuyu

6

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

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

然后保存你好像之前。

+0

我认为这可能解决了我几个月来一直在经历的问题! – ScruffyDuck

+0

当然! @ScruffyDuck,方法** Image.FromFile **将打开该图像文件。 – adatapost

+0

@Jon坚果有没有一种节约方法? – Lakshani

4

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

可以创建通过dir:

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

当任一个位图对象或图像对象是从一个文件构成,该文件保持锁定的对象的生存期。因此,您无法更改图像并将其保存回原来的相同文件。在GDI +,JPEG图像到MemoryStream的发生http://support.microsoft.com/?id=814675

一般性错误:

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

http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html

编辑:从内存中只是写。保存到一个 '中间人' MemoryStream应该工作:

例如,更换此:

Bitmap newBitmap = new Bitmap(thumbBMP); 
thumbBMP.Dispose(); 
thumbBMP = null; 
newBitmap.Save("~/image/thumbs/" + "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); 
    } 
} 
0

就像@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? 
0

试试这个它会工作

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

它也将解决错误的gdi + –

0

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

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

而且可以通过添加文件名是固定的:

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

注意:您实际上并不需要添加扩展名为它工作。

0

试试这个:

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

} 
相关问题