Graphics and Multimedia Topics include: GDI+, drawing 2D/3D, printing and Managed DirectX and the XNA Framework

Go Back  Xtreme .NET Talk > .NET > Graphics and Multimedia > Bitmap.Save() is not working?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2003, 02:57 AM
Optikal's Avatar
Optikal Optikal is offline
Regular
 
Join Date: Oct 2002
Location: Canada
Posts: 67
Optikal is on a distinguished road
Default Bitmap.Save() is not working?

It seems that when I load a System.Drawing.Bitmap object
from a disk file using the constructor (the one that
accepts only one string [the filename]), then try to do
a .Save() on it passing in a string representing the
filename, it will produce an exception if the filename I
pass to Save() is the same one I used to load the bitmap.

The exception raised is:
System.Runtime.InteropServices.ExternalException
Additional information: A generic error occurred in GDI+.

If I pass in a different filename to Save() it works fine, or if I
create a new Bitmap by just specifying width and height
(not filename) then .Save() it, it works fine.

What is perhaps the strangest thing of all, is that if
right before I try my .Save(), if I do a .ShowDialog() on
a SaveFileDialog control, the Bitmap.Save() will always
work. Even though I do absolutely nothing with the
result from the dialog, and it doesn't matter what file I
select in the dialog (or even if I press cancel).

My theory is that the Bitmap is somehow holding onto the
file that it loads itself from. And that when I try to
save it, it raises that exception because the file is not
ready to be overwritten. Now my questions would be, how
do I make it so that I can successfully Save my bitmap,
and what the heck is the SaveFileDialog.ShowDialog()
doing that makes Bitmap.Save() work?
Reply With Quote
  #2  
Old 02-08-2003, 08:33 AM
Bucky's Avatar
Bucky Bucky is offline
Contributor

Preferred language:
C#
 
Join Date: Dec 2001
Location: East Coast
Posts: 791
Bucky is on a distinguished road
Default

That's a good question, and I'm not exactly sure what the
ShowDialog is doing. I think it may be allowing more processing
time to other applications, in which case you can do the same
thing by calling Application.DoEvents() to allow other apps and the
OS to complete some commands. This may allow
Windows sufficient time to free up the bitmap so you can save to it.
__________________
"Being grown up isn't half as fun as growing up
These are the best days of our lives"
-The Ataris, In This Diary
Reply With Quote
  #3  
Old 02-08-2003, 07:43 PM
Optikal's Avatar
Optikal Optikal is offline
Regular
 
Join Date: Oct 2002
Location: Canada
Posts: 67
Optikal is on a distinguished road
Default

Don't think thats the case. I load and save the bitmap in separate events (triggered by menu items). So the application is idle between the 2 events. But just to make sure, I tried it with Application.DoEvents() right before the save, but as I suspected, it made no difference.

It has been suggested that the Bitmap locks the file while the bitmap is loaded, and that if I want to save it to the same file I have to copy the bitmap to another in-memory Bitmap, then call Save on that one (haven't tried it yet though)...still don't understand why the SaveFileDialog would make it work though.
Reply With Quote
  #4  
Old 11-22-2005, 09:48 AM
nickhow nickhow is offline
Newcomer
 
Join Date: Nov 2005
Posts: 2
nickhow is on a distinguished road
Default

The reason you have this problem is because the file you are trying to overwrite is "already in use".

If you find a way to make the file no longer in use, please let me know.

Nick
Reply With Quote
  #5  
Old 11-22-2005, 10:10 AM
nickhow nickhow is offline
Newcomer
 
Join Date: Nov 2005
Posts: 2
nickhow is on a distinguished road
Default

In fact, this snippet of code might be of some use for when you first open your image file...

strLocation = "Your File Location"
Dim objFileStream As New FileStream(strLocation, FileMode.Open)
mobjImage = Image.FromStream(objFileStream)
objFileStream.Close()
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
In VB.NET, how do I save a bitmap in a pixel-indexed format? CMousseau Graphics and Multimedia 0 03-16-2005 10:32 AM
Drag Drop Image Save As Bitmap SEVI Windows Forms 0 03-15-2004 09:06 PM
Move a small Bitmap over a large Bitmap romanhoehne DirectX 3 12-29-2003 12:34 AM
Problem with Bitmap.Save after Palette Changed _SBradley_ Graphics and Multimedia 1 08-08-2003 10:39 AM

Advertisement:

Powered by liquidweb