Help when saving images to bitmap files

G

Guest

I currently have a picture which is stored in a "System.Drawing.Image"
variable and I want to save it as a bitmap file.
I have tried 2 different approaches and both give me the following error:

An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.drawing.dll

Additional information: A generic error occurred in GDI+.


The code I have tried is as follows

METHOD 1:

Dim flfile As New System.IO.FileStream(strFileName,
IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
picImage.Image.Save(flfile, System.Drawing.Imaging.ImageFormat.Bmp)
flfile.Close()

METHOD 2:

picImage.Image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Bmp)


In both cases strFileName is the path and name of the file to write to and
picImage is a picturebox currently displaying the image in my application.

Does anyone know what is causing the error and how I can get around it?
 
M

Matthew.Gertz

Hi, MKeeton,
I tried out both of your code snippets below (I'm using VS2005), and they both worked fine for me. I added a GIF called "MyImage" to my resources, assigned the value of My.Resources.MyImage to picImage, and then saved it as a BMP in the same way as you tried below. I used c:\foo.bmp as the place to save it to.

This makes me wonder:

(1) What the value is of picImage. Stupid question, and I apologize for asking it, but are you sure it's been assigned the right thing, and that the image hasn't been disposed or anything like that?
(2) Any chance that the place you're saving it to has odd permissions?

Unfortunately, nothing else obvious comes to mind. :-( Let us know...

--Matt Gertz--*
VB Compiler Dev Lead

-----Original Message-----
From: MKeeton
Posted At: Monday, November 14, 2005 6:29 AM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Help when saving images to bitmap files
Subject: Help when saving images to bitmap files


I currently have a picture which is stored in a "System.Drawing.Image"
variable and I want to save it as a bitmap file.
I have tried 2 different approaches and both give me the following error:

An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.drawing.dll

Additional information: A generic error occurred in GDI+.


The code I have tried is as follows

METHOD 1:

Dim flfile As New System.IO.FileStream(strFileName,
IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
picImage.Image.Save(flfile, System.Drawing.Imaging.ImageFormat.Bmp)
flfile.Close()

METHOD 2:

picImage.Image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Bmp)


In both cases strFileName is the path and name of the file to write to and
picImage is a picturebox currently displaying the image in my application.

Does anyone know what is causing the error and how I can get around it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top