簡體   English   中英

GDI +中發生了一般錯誤

[英]A generic error occurred in 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));

當從文件構造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://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);
    }
}

試試這個。

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

就像@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;
        }
    }

}

GDI +中發生了一般錯誤

我也遇到了同樣的問題。 我嘗試了很多方法來解決這個問題。 最后,我找到了一個出錯的地方。 問題是我在文件路徑中使用了空間,這是不可接受的。 現在它在撇號后刪除C前面的空格后工作正常:

"SupplyItems":"C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"

相反......我用了一個以下。

"SupplyItems":" C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"

輕微的錯誤,但花了很長時間才找到並修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM