Reading Image data from a Database

  • Thread starter Thread starter Ed
  • Start date Start date
E

Ed

Hope someone can help me out...

I have been tasked to read some image data from an sql database and
save the files to flat files. OK, sounds easy as I'v used BLOBs
before. But this is an old database and I cannot get the image to
work.

The columns in the database are of type text. Here is one of the
images text (in full) in the database (I hope you can see it):

"GIF89a\0\0¢ÿ\0ÿÿÿÀÀÀ\0ÿÿ\0\0„\0\0\0\0\0\0\0\0\0\0\0\0!ù
\0\0\0,\0\0\0\0\0\0@BºÜî J@žÂ²ÚÃy\0(RÊ0thª2X¤
Â¥_(Æ’Å¡yµïªïüJüÀߪERÂÂÀé2jLÃ\aòXÚsÂ)¶Ç"


I have tried something along the lines of:

byte[] bytes = new byte[(oDatareader.GetBytes(0, 0, null,
0, int.MaxValue))];
oDatareader.GetBytes(0, 0, bytes, 0, bytes.Length);

System.Drawing.Image img =
System.Drawing.Image.FromStream(new MemoryStream(bytes));

Which works fine in my other apps. I also tried:

byte[] Test =
System.Text.UTF8Encoding.UTF8.GetBytes(ImageString);
Image x = Image.FromStream(new MemoryStream(Test));

But no go.

Can anyone reccomend anything? I appreciate any help.

Thanks,

ed
 
Hi Ed,

Storing binary data as text is very bad in .Net and unless converting to
base64 or similar you are bound to lose data. I assume the data is stored
in the database with a non .Net application, and from the text sample you
have shown it does look like binary data, but it is not readable as text
(the [] characters can't be displayed as text).

Your best bet would be to try to read it as a byte[], but the DataReader
may well destroy the data if it tries to read the data as text.

Compare the database data with the datareader data. Another possibility
might be to convert Text to Binary in the database, but it appears this
conversion is not allowed.

Reading the data as string, and use GetBytes will NOT work as the moment
you put the data inside a string object data loss is almost guaranteed.

You might try to ADONET newsgroup or one of the microsoft.public.sqlserver
groups.




Hope someone can help me out...

I have been tasked to read some image data from an sql database and
save the files to flat files. OK, sounds easy as I'v used BLOBs
before. But this is an old database and I cannot get the image to
work.

The columns in the database are of type text. Here is one of the
images text (in full) in the database (I hope you can see it):

"GIF89a\0\0¢ÿ\0ÿÿÿÀÀÀ\0ÿÿ\0\0„\0\0\0\0\0\0\0\0\0\0\0\0!ù
\0\0\0,\0\0\0\0\0\0@BºÜî J@žÂ²ÚÃy\0(RÊ0thª2X¤
Â¥_(Æ’Å¡yµïªïüJüÀߪERÂÂÀé2jLÃ\aòXÚsÂ)¶Ç"


I have tried something along the lines of:

byte[] bytes = new byte[(oDatareader.GetBytes(0, 0, null,
0, int.MaxValue))];
oDatareader.GetBytes(0, 0, bytes, 0, bytes.Length);

System.Drawing.Image img =
System.Drawing.Image.FromStream(new MemoryStream(bytes));

Which works fine in my other apps. I also tried:

byte[] Test =
System.Text.UTF8Encoding.UTF8.GetBytes(ImageString);
Image x = Image.FromStream(new MemoryStream(Test));

But no go.

Can anyone reccomend anything? I appreciate any help.

Thanks,

ed
 
Well, I was able to get it working after some searching. Here is what
I did in case someone else needs to do this (I found this code online,
but I cannot remember the site I found it, so sorry for no link).

string Data = myReader.GetString(1);

if (Data.IndexOf("GIF89a") == 0)
{

// Create a file to hold the output.
using (FileStream fs = new FileStream(@"Images\" +
pub_id + ".gif", FileMode.OpenOrCreate, FileAccess.Write))
{
BinaryWriter bw = new BinaryWriter(fs);

// Reset the starting byte for the new BLOB.
startIndex = 0;

// Read the bytes into outbyte[] and retain
the number of bytes returned.
retval = myReader.GetBytes(2, startIndex,
outbyte, 0, bufferSize);

// Continue reading and writing while there
are bytes beyond the size of the buffer.
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();

// Reposition the start index to the end
of the last buffer and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(2, startIndex,
outbyte, 0, bufferSize);
}

// Write the remaining buffer.
if( retval > 0 )
bw.Write(outbyte, 0, (int)retval - 1);

bw.Flush();

// Close the output file.
bw.Close();
}
}
 
Back
Top