.NET Framework - 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?
OleDbDataAdapter
(1)
Common.gPic2LabPath
(1)
File.ReadAllBytes
(1)
OleDbCommand
(1)
Graphics.DrawImage
(1)
Graphics.FromImage
(1)
ReadAllBytes
(1)
DataColumn
(1)
Peter Duniho replied to MS
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
Jeff Johnson replied to MS
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.)
Jeff Gaines replied to MS
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.
zooots replied to Jeff Johnson
Jeff is correct.
Normally,image data is read from or written to database as byte stream.
ImageColume should be defined as OleDbType.Binary.
Jeff Gaines replied to Jeff Gaines
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.
entire file into memory. I've read various articles suggesting how. Regardless of the approach (File.ReadAllBytes(), FileStream.Read(), etc.), it appears that the data is read into a byte array. However I'm curious if there is a more accomodating approach. .NET Framework Discussions OutOfMemoryException (1) File.ReadAllBytes (1) ReadAllBytes (1) FileStream (1) GetValue (1) Visual (1) VisualStudio (1) Barnett (1) Well, if entire file into memory. I've read various articles suggesting how. Regardless of the approach (File.ReadAllBytes(), FileStream.
con i dei dati provenienti da Access 07. Quando faccio il Fil con l'oggetto OleDbDataAdapter OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM FATTURE", conn); oda.Fill(DS); ho il seguente errore Sequenza di ordinamento selezionata capita sullo stesso DB nei sistemi Vista e Seven . . . . come posso risolverlo? C# - Italian Discussions OleDbDataAdapter (1) UGIdotNET (1) Vista (1) XP (1) Visual (1) LOLSe (1) DS (1) DataSet (1 con i dei dati provenienti da Access 07. Quando faccio il Fil con l'oggetto OleDbDataAdapter
NET Framework Bonjour, Je lis un fichier avec un tableau d'octets. byte[] fileContents; fileContents = File.ReadAllBytes(@fileName); Dans ce tableau, je cherche à obtenir une valeur représentant un entier. J'ai bytes description: Bonjour, Je lis un fichier avec un tableau d'octets. byte[] fileContents; fileContents = File.ReadAllBytes(@fileName); Dans ce tableau, je cherche à ob