GDI Error When Saving to Text

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2058 Views - Last Post: 27 June 2014 - 08:09 AM Rate Topic: -----

#1 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

GDI Error When Saving to Text

Posted 23 June 2014 - 01:03 PM

I need some new eyes on this one. The code posted in the spoiler is for a text file that I am saving text in. I get a
0x80004005 GDI Error on the line in the second code block. Data.BadgeData is a string[,].

Spoiler

Here is where it breaks:
                                else
                                {
                                    System.IO.File.Delete(Temp);
    Break Here=>                                img.Save(Temp, ImageFormat.Jpeg);
                                }//End Delete Picture



What am I doing wrong at this point?

Is This A Good Question/Topic? 0
  • +

Replies To: GDI Error When Saving to Text

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 23 June 2014 - 06:27 PM

It took me a while to parse all that code. May I suggest less nesting, and writing smaller functions that call other small functions. Remember the Single Responsibility Principle. Anyway, I would have expected your code to work...

Did you see this reply on StackOverflow? http://stackoverflow...occurred-in-gdi
Was This Post Helpful? 0
  • +
  • -

#3 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 06:07 AM

I agree a little refactoring wouldn't hurt. Right I am just trying to get it to work.
Since I posted I switched back to saving it as an Jpg Image in a folder, but the same GDI error Pops. So maybe it is the EXIF block. I will continue to do some research. I am also thinking maybe the image is still open some where also.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 06:21 AM

If it was still open, the Delete() call should have thrown an exception.

You could try working around the issue. See Kerry Wong's blog post.
Was This Post Helpful? 0
  • +
  • -

#5 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 07:06 AM

Update
Ok so it seems to be when I access the file a second time for deletion.
Here is the Error
Spoiler

Here is the new Code:
Spoiler


Here is the Exception:
Spoiler


So I am on to Checking into File Processing.

This post has been edited by alapee: 24 June 2014 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 07:42 AM

Do you have anti-virus software running? (And by any chance is it McAffee? :) ) Any which way, try disabling it for that folder that you are using for the images.
Was This Post Helpful? 0
  • +
  • -

#7 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 08:20 AM

Update:

Code
Spoiler



Error
Spoiler




Exception
Spoiler


Got Rid of the File Problem now I am back to a creating an image problem, no GDI error this time. Seems disposing() the picturebox image helped. We run endpoint for Security, I have had no problems with other projects and similar syntax.

This post has been edited by alapee: 24 June 2014 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 09:40 AM

And what is the value of FileLocation since that is the likely invalid parameter?
Was This Post Helpful? 0
  • +
  • -

#9 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 09:57 AM

Full File Path From all Addendiums
System.Environment.CurrentDirectory + @"\Data\"+ @"ID\" + cmbName.Text.Replace(" ", "").Trim() + @".jpg"


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 12:07 PM

That is how you are computing the value, but what is the actual computed value? Are you sure there are no illegal characters in the value you get from the combo box.

As a quick aside, using Path.Combine() in conjuction with String.Format() may make things easier to read and understand.
Was This Post Helpful? 0
  • +
  • -

#11 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 12:43 PM

I seperate the illegal characters out in a different function. The path (at run time) works if you copy and paste and remove the extra \s. I pulls the image up just fine.
Was This Post Helpful? 0
  • +
  • -

#12 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: GDI Error When Saving to Text

Posted 24 June 2014 - 01:03 PM

The error message says that the file is opened exclusively by another process. In such cases, I find handles.exe from the SysInternals suite very useful in determining which processes are locking a file:

http://technet.micro...s/bb896655.aspx
Was This Post Helpful? 0
  • +
  • -

#13 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 25 June 2014 - 08:00 AM

Ok, So hitting this from a different Angle:
Here is what I know, I am creating a JPEG Image from a Memory Stream and saving it as a file (No problems here)
Then if the Picture Changes (IE, New ID Photo) that image should be delete from the file directory and a new one in its place should be created with the same name.(Where we are having problems)

So what would be the proper way to do this?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: GDI Error When Saving to Text

Posted 25 June 2014 - 08:29 AM

Did you close the file when you were saving it initially?

Do you have UI that displays the current pictures? If so, I believe the image's various Load() operations keep the file open until you dispose the image. You may need to implement a Stream class that doesn't lock the file open, but I don't know how well the Image class implementations will handle the underlying stream changing on them.
Was This Post Helpful? 0
  • +
  • -

#15 alapee   User is offline

  • Stressed Out Programmer

Reputation: 239
  • View blog
  • Posts: 2,536
  • Joined: 24-October 13

Re: GDI Error When Saving to Text

Posted 25 June 2014 - 09:13 AM

I was using a "Using statement"

Here is the current Code:
 private void PictureCreate(string FileLocation)
        {
            try
            {
                if (pbPicture.Image != null)
                {
                    //Exists 
                    using (Image img = pbPicture.Image)
                    {
                       // pbPicture.Image.Dispose();
                       // pbPicture.Image = null;
                        if (System.IO.File.Exists(FileLocation) ==false )
                        {
                            img.Save(FileLocation, ImageFormat.Jpeg);
 //This is where the JPEG is created, but if this statement as occured in the past
 //System.IO.File.Delete(FileLocation); Doesn't work, it through the Error previously Posted
                        }
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show("Error:\n" + ex.ToString()); Console.WriteLine(ex.ToString()); }
        }//End Picture Create [Update 6/24/2014]


The Data is pulled in from/ Out To a Text Document, the Stream Reader/Writer is also in a using statement.
My best guess is some where the image is being used, I have even tried clearing the PictureBox information after assigning it to an image, still a no go.

This post has been edited by alapee: 25 June 2014 - 09:21 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2