OleDbDataAdapter
(1)
Common.gPic2LabPath
(1)
File.ReadAllBytes
(1)
OleDbCommand
(1)
Graphics.DrawImage
(1)
Graphics.FromImage
(1)
ReadAllBytes
(1)
DataColumn
(1)

Store image (jpg) in datatable

Asked By MS
12-Mar-10 09:32 AM
I am storing an image in a datatable using the following code:

the dataset is created using oledb:
cmd = new OleDbCommand("SELECT * FROM StudentData " +

dbConn);

adapter = new OleDbDataAdapter(cmd);

dtStudent = new DataTable("Student");

adapter.Fill(dtStudent);

cmd = null;

adapter = null;

cmd = null;

adapter = null;

//define/add the column
DataColumn columname = dtStudent.Columns.Add("Image01", typeof(Image));

Put the image in the datatable
Bitmap bmp = new Bitmap(Common.gPic2LabPath + "Cropped\\" +
row["ImageName"].ToString().Trim());

dtStudent.Rows[0]["Image01" = bmp.Clone();

bmp.Dispose();


Dispose the datatable

dtStudent.Dispose();

If I try to rename the the image (on disk), I am told that another process
has it open.
Is there something else I need to do to release the image?

MS wrote:Normally, disposing the original Bitmap instance would do that.

Peter Duniho replied to MS
12-Mar-10 11:48 AM
Normally, disposing the original Bitmap instance would do that.  I
have not tried, but maybe the Clone() method copies _everything_,
including the dependency on the original file.  You might try instead
copying the Bitmap instance by using the Bitmap(Bitmap) constructor
overload instead, and if that does not work, by just creating a new
Bitmap the same size and copying by using Graphics.FromImage() and
Graphics.DrawImage() to draw the original into the new.

If none of that works, post a concise-but-complete code example that not
only compiles but reliably demonstrates the problem.

Pete

You create a Bitmap and then put it in the database without doing anythingelse.

Jeff Johnson replied to MS
12-Mar-10 11:57 AM
You create a Bitmap and then put it in the database without doing anything
else. I have to assume that the underlying column Image01 is some sort of
binary data type since I am unaware of any DBMSes which have a "Graphic
Image" data type, so why not just create that column with type byte[] and
read the bytes in the file without turning it into a Bitmap? In other words,

DataColumn columname = dtStudent.Columns.Add("Image01", typeof(byte[]));

dtStudent.Rows[0]["Image01"] = File.ReadAllBytes(Common.gPic2LabPath +

(I also question naming a variable of type DataColumn "columnname". That
variable name suggests a string to me, not a whole DataColumn. I would have
called it "imageColumn" myself.)

Don't use clone, it caused me problems for years until somebody in thisgroup

Jeff Gaines replied to MS
12-Mar-10 12:07 PM
Don't use clone, it caused me problems for years until somebody in this
group pointed me in the right direction. You need to do something like this:

//	Make a copy to avoid file locking/resource problems
imgReturn = new Bitmap(imgTemp, 16, 16);

In your case:
dtStudent.Rows[0]["Image01" = new Bitmap(bmp, 16, 16);

Then dispose the original (as you are doing):
bmp.Dispose();

--
Jeff Gaines Dorset UK
All those who believe in psychokinesis raise my hand.
Jeff is correct.
zooots replied to Jeff Johnson
12-Mar-10 01:13 PM
Jeff is correct.
Normally,image data is read from or written to database as byte stream.
ImageColume should be defined as OleDbType.Binary.
Gaines wrote:The original thread is here if you want the detail:http://groups.
Jeff Gaines replied to Jeff Gaines
12-Mar-10 05:50 PM
The original thread is here if you want the detail:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/8e0716585902a9d7

--
Jeff Gaines Dorset UK
I can please only one person per day. Today is not your day.
Tomorrow, is not looking good either.
Post Question To EggHeadCafe