Bitmap.Save crash when ivoked for the second time ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

In a class I have to methods:

public void Download(string imageUrl)
{
using (WebClient client = new WebClient())
{
using (Stream stream = client.OpenRead(imageUrl))
{
this.FoundBitmap = new Bitmap(stream);
stream.Flush();
}
}
}

and...

public void SaveImage(string filename, ImageFormat format)
{
if (FoundBitmap != null)
{
try
{
FoundBitmap.Save(filename, format);
}
catch (Exception)
{
throw;
}
}
}

When I use the SaveImage method for the second time I get the exception "a
generic error occurred in GDI+"

I don't understand why this happens. Can someone please help me to solve
this problem.

Thanks a lot in advance,

Bart
 
When I use the SaveImage method for the second time I get the exception "a
generic error occurred in GDI+"

I don't understand why this happens. Can someone please help me to solve
this problem.

Thanks a lot in advance,

I believe that when you create a new Bitmap it locks the file. And it
doesn't release it until you Dispose the object.

Try calling 'bitmapObject.Dispose()' after you used it the 'first time'.
Maybe in a 'finally' block, see if that makes a difference.
 
Thanks for your answer. But it doesn't seem to help :(

Any further thougths ?

Thanks again

bart
 
Thanks for your answer. But it doesn't seem to help :(
Any further thougths ?

I'm pretty sure it's some kind of locking issue that causes it.

Any chance you can attach a cut-down/demo version of your project file?
 
public void Download(string imageUrl)
{
using (WebClient client = new WebClient())
{
using (Stream stream = client.OpenRead(imageUrl))
{
this.FoundBitmap = new Bitmap(stream);
stream.Flush();
}
}
}

For reference, there's no point in calling Flush() on a stream that's open
for READING.
public void SaveImage(string filename, ImageFormat format)
{
if (FoundBitmap != null)
{
try
{
FoundBitmap.Save(filename, format);
}
catch (Exception)
{
throw;
}
}
}

When I use the SaveImage method for the second time I get the exception "a
generic error occurred in GDI+"

What happens if when you call SaveImage() the second time you use a
different file name? (Or is that what you're already doing?)
 
I'm not sure if i am allowed to attach a file within this newsgroup.
Otherwise I think have no possibilities to get it to you.

Thanks again,

Bart
 
I'm not sure if i am allowed to attach a file within this newsgroup.
Otherwise I think have no possibilities to get it to you.

Yer, I'm not sure too. It's not grayed out on my client but who knows

first.last on my blog domain (in my sig) is my email.

I'd be happy to take a look if you like.
 
bart said:
Hi all,

In a class I have to methods:

public void Download(string imageUrl)
{
using (WebClient client = new WebClient())
{
using (Stream stream = client.OpenRead(imageUrl))
{
this.FoundBitmap = new Bitmap(stream);
stream.Flush();

Why not use Image.FromStream() instead? It's more flexible than using
the Bitmap constructor.

In either case, note the comment in the docs: "You must keep the stream
open for the lifetime of the Bitmap." (Replace the word "Bitmap" with
"Image" for the Image.FromStream() method).

In other words, as soon as you've got an Image or Bitmap instance, you
should disconnect from the stream by copying the object to a new object.
For example:

using (Bitmap bitmap = new Bitmap(stream))
{
this.FoundBitmap = new Bitmap(bitmap);
}

Also, as Jeff pointed out, the call to Flush() is pointless.

[...]
public void SaveImage(string filename, ImageFormat format)
{
if (FoundBitmap != null)
{
try
{
FoundBitmap.Save(filename, format);
}
catch (Exception)
{
throw;
}

Speaking of pointless, that try/catch block is too. :)
When I use the SaveImage method for the second time I get the exception
"a generic error occurred in GDI+"

I don't understand why this happens. Can someone please help me to solve
this problem.

Unfortunately, the image-related classes do that pretty much whenever
anything goes wrong. It makes it very hard to figure out what's
actually going wrong. But I suspect it could be related to you having
closed the original stream and then continuing to use the Bitmap
instance. Why it works the first time I don't know; I'd think it'd
break right away. But it's conceivable that the code gets lucky on the
first try, but eventually whatever the object needs the stream for later
causes a problem.

Pete
 
bart said:
I'm not sure if i am allowed to attach a file within this newsgroup.
Otherwise I think have no possibilities to get it to you.

You are allowed to post attachments, but please don't. Lots of people
will never see your message with an attachment, because ISPs block
messages with attachments, and other people won't see the attachment
even though they see your message, because ISPs strip attachments (this
includes Google Groups, which is the de facto primary archive of the
newsgroup).

Just copy and paste code into your message. If you think it's too much
code to include in a newsgroup post, then you haven't made the code
example concise enough. :)

Pete
 
Sorry it took some time to come back but it was a locking issue.

Thank you all for the help

Bart
 
Sorry it took some time to come back but it was a locking issue.

The question is: WHAT was locking it? Or to put it another way, could you
describe what you did to solve the problem? It may help someone who is
having the same issue in the future and finds this thread. (Plus I'm kind of
curious.)
 
The question is: WHAT was locking it? Or to put it another way, could you
describe what you did to solve the problem? It may help someone who is
having the same issue in the future and finds this thread. (Plus I'm kind
of curious.)

I have a wpf application in which i databind images. I think the databinding
created the lock.

Hope this helps,

Bart
 
I have a wpf application in which i databind images. I think the
databinding created the lock.

Hope this helps,

Bart
What did you change to cure the problem ?
 
Back
Top