Generic error in GDI+

G

Guest

I'm loading thousands of images into a SQL Server 2000 database. I have a
..NET windows application that I'm working on written in C# that retrieves the
fileinfo of the directory and then loads all of the images into an image
field in the database. The problem I'm having is that I get an unhandled
exception after loading about 900 images that says "A generic error occured
in GDI+". I've determined that it has something to do with loading the
images. If I do everything but actually retrieve and load the images it runs
fine.
Here is the code that retrieves and loads the images to the database:

string strSQL = @"INSERT INTO page(page_image) VALUES (@page_image)";
SqlCommand sqlCommand_page = new SqlCommand(strSQL, sqlConnection_DocImage);
SqlParameterCollection pc_page = sqlCommand_page.Parameters;
pc_page.Add(new System.Data.SqlClient.SqlParameter("@page_image",
System.Data.SqlDbType.VarBinary, 2147483647, "page_image"));

Image img;

sqlConnection_DocImage.Open();

foreach(FileInfo row in jpgFileInfo)
{
using(MemoryStream imageMS = new MemoryStream())
{
img = Image.FromFile(row["fullname"].ToString());
img.Save(imageMS, ImageFormat.Jpeg);
pc_page["@page_image"].Value = imageMS.ToArray();
}
sqlCommand_page.ExecuteNonQuery();
}

sqlConnection_DocImage.Close();

The error message gives me no clue to diagnose what's happening. It sounds
like a resource isn't being disposed of.

Thanks in advance for any help.

HArlan Marshall
 
R

Robbe Morris [C# MVP]

Why are you loading them as images first? I would think you'd
load the binary file as is with the BinaryReader and save the byte
array and not bother with GDI+ at all until you get ready
to render the image.
 
G

Guest

Good point. I didn't know I could do it that way. The only example I could
find for writing images to a binary data field was done through an image. I
will look into the BinaryReader. I do load them as images for display as a
function of another part of the application, but maybe I don't need to go
through an image in the save process.

Thanks very much for pointing me the right way.

Harlan Marshall

Robbe Morris said:
Why are you loading them as images first? I would think you'd
load the binary file as is with the BinaryReader and save the byte
array and not bother with GDI+ at all until you get ready
to render the image.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx



HarlanM said:
I'm loading thousands of images into a SQL Server 2000 database. I have a
.NET windows application that I'm working on written in C# that retrieves
the
fileinfo of the directory and then loads all of the images into an image
field in the database. The problem I'm having is that I get an unhandled
exception after loading about 900 images that says "A generic error
occured
in GDI+". I've determined that it has something to do with loading the
images. If I do everything but actually retrieve and load the images it
runs
fine.
Here is the code that retrieves and loads the images to the database:

string strSQL = @"INSERT INTO page(page_image) VALUES (@page_image)";
SqlCommand sqlCommand_page = new SqlCommand(strSQL,
sqlConnection_DocImage);
SqlParameterCollection pc_page = sqlCommand_page.Parameters;
pc_page.Add(new System.Data.SqlClient.SqlParameter("@page_image",
System.Data.SqlDbType.VarBinary, 2147483647, "page_image"));

Image img;

sqlConnection_DocImage.Open();

foreach(FileInfo row in jpgFileInfo)
{
using(MemoryStream imageMS = new MemoryStream())
{
img = Image.FromFile(row["fullname"].ToString());
img.Save(imageMS, ImageFormat.Jpeg);
pc_page["@page_image"].Value = imageMS.ToArray();
}
sqlCommand_page.ExecuteNonQuery();
}

sqlConnection_DocImage.Close();

The error message gives me no clue to diagnose what's happening. It sounds
like a resource isn't being disposed of.

Thanks in advance for any help.

HArlan Marshall
 

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