?
Solved

WinForms - A generic error occurred in GDI+

Posted on 2009-04-26
4
Medium Priority
?
1,136 Views
Last Modified: 2013-12-17
I'm working in WINFORMS ... not the web ...
This line works if the BMP file does not exist

                    this.picturebox4.Image.Save("C:\txrt\bin\Debug\MatchImages\A.bmp", ImageFormat.Bmp);

If the BMP file does exist, I get:    A generic error occurred in GDI+

I need to LOAD then SAVE then LOAD then SAVE, same image BMP several times. I thought, maybe, loading the picturebox image from file left the file locked, so I load the image this way

        picturebox4.Image = ImageGetDisconnected("C:\txrt\bin\Debug\MatchImages\A.bmp");

        private Image ImageGetDisconnected(string DPFE)
        {                
            Image temp = new Bitmap(14,15);
            if (File.Exists(DPFE) == true)
            {
                FileInfo FI = new FileInfo(DPFE);
                if (FI.Length > 0)
                {
                    Stream s = File.Open(DPFE, FileMode.Open);
                    temp = Image.FromStream(s);
                    s.Close();
                }
            }
            return temp;
        }

Heck .... I have even tried deleting everything from the directory BEFORE trying to write the BMP file, like this

            string[] fileEntries = Directory.GetFiles("C:\txrt\bin\Debug\MatchImages\");
            foreach (string fileName in fileEntries)
            {
                System.IO.File.Delete(fileName);
            }

But even executing the DELETE didn't help .... so some how I'm missing something!

HELP!
0
Comment
Question by:volking
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24236376
Have you tried something like:
System.Drawing.Image temp = this.pictureBox4.Image.Clone();
 
this.pictureBox4.Image = null;
temp.Save(@"C:\txrt\bin\Debug\MatchImages\A.bmp", ImageFormat.Bmp);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24236390
Change line 1 to:
System.Drawing.Image temp = (System.Drawing.Image)this.pictureBox4.Image.Clone();

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24236437
Nevermind. The above attempt results in the same error.
0
 
LVL 5

Accepted Solution

by:
volking earned 0 total points
ID: 24238134
MY SOLUTION: A generic error occurred in GDI+
Figured this out myself ... thought others might enjoy, too.

I'm not sure "WHY" this works ... but it solved my problems. For me, problem was loading image from a file, then trying to write back to same file. For some reason, file was being held open by prior read. Anyways, this worked for me.
 
Caveat: Below uses [ImageFormat.Bmp] If you're reading/writing another format, you'll need to tweak.
 
Caveat: During the write operation a temporary file named [temporaryToDefeatGDIerror.bmp] is momentarily created, copied then deleted.
 
Here's a general Function class which does the real work.
------------------------------------------------------------------------------------
class fn
{
    public static void SaveImageToFile(string DrvPath, string FileExt, Bitmap bm)
    {
        string actualDPFEwanted = DrvPath + FileExt;
        if (File.Exists(actualDPFEwanted) == true) File.Delete(actualDPFEwanted);
        string tempDPFE = DrvPath + "temporaryToDefeatGDIerror.bmp";
        if (File.Exists(tempDPFE) == true) File.Delete(tempDPFE);
        bm.Save(tempDPFE, ImageFormat.Bmp);
        File.Copy(tempDPFE, actualDPFEwanted);
        File.Delete(tempDPFE);
    }
    public static Bitmap GetImageDisconnectedFromFile(string DrvPathFileExt)
    {
        Image Iret = (Image)new Bitmap(1,1);
        if (File.Exists(DrvPathFileExt) == true)
        {
            FileInfo FI = new FileInfo(DrvPathFileExt);
            if (FI.Length > 1)
            {
                Stream s = File.Open(DrvPathFileExt, FileMode.Open);
                Iret = Image.FromStream(s);
                s.Close();
            }
        }
        Bitmap BM = new Bitmap(Iret);
        Iret.Dispose();
        GC.Collect(0, GCCollectionMode.Forced);
        return BM;
    }
}
------------------------------------------------------------------------------------
and here's a sample of how I use each fn.call's
------------------------------------------------------------------------------------
private void ReadOne()
{
    string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
    if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
    if (File.Exists(DP + "a.bmp") == true) picturebox1.Image = fn.GetImageDisconnectedFromFile(DP + "a.bmp");
}
 
private void WriteOne()
{
    string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
    if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
    if (!(picturebox1.Image == null)) fn.SaveImageToFile(DP, "a.bmp", (Bitmap)picturebox1.Image);
}
------------------------------------------------------------------------------------
 
Enjoy y'all

Frederick Volking
http://PrivateArchive.org
http://GetLocalHelp.org
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question