2011-08-18 83 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; 
     } 
    } 

} 
相關問題