GDI +中发生了一般错误

时间:2011-08-18 09:55:05

标签: c# bitmap gdi+

我使用以下方法将图像加载到图片框中

picturebox1.Image = Image.FromFile()

我用以下方法保存:

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

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

  

GDI +中发生了一般错误

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

11 个答案:

答案 0 :(得分: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 :(得分: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);

然后像以前一样保存。

答案 2 :(得分:4)

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

你可以用:

创建目录
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FileName));

答案 3 :(得分:1)

试试这个。

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

答案 4 :(得分:1)

当从文件构造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);
    }
}

答案 5 :(得分: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?

答案 6 :(得分:0)

试试这个会起作用

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

答案 7 :(得分:0)

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

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

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

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

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

答案 8 :(得分: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;
        }
    }

}

答案 9 :(得分:0)

GDI +中发生了一般性错误

我也面临同样的问题。我尝试了多种方法来解决此问题。终于,我找到了一个出问题的地方。问题是我在文件路径中使用了空间,这是不可接受的。现在,删除撇号后C前面的空格后,它可以正常工作:

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

相反...我用下面的一个。

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

轻微错误,但花了很长时间才找到并解决。

答案 10 :(得分:0)

请注意,由Image.Clone()创建的图像仍然会导致GDI +错误,如下面的BAD代码所示,您必须使用Image.FromStream()方法读取图像,如本页中的解决方案所示。 / p>


    //BAD CODE: the image we will try to save AFTER the original image has been cloned and disposed
    Image clonedImage;
    //load image from file, clone it then dispose
    using (var loadedFromDiskImage = Image.FromFile(filePath))
    {
         clonedImage = (Image) loadedFromDiskImage.Clone();
    } 

//you might think the new image can be saved given the original is disposed
 //but this doesn't seem to be the way Clone() works
 //expect GDI+ error in line below:
 clonedImage.Save(filePath);