473,416 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Reading Image data from a Database

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

Feb 14 '07 #1
2 7351
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.


On Wed, 14 Feb 2007 22:28:44 +0100, Ed <ed*@nait.ab.cawrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 15 '07 #2
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();
}
}

Mar 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
4
by: Andy | last post by:
Hello All: I have a field in the database that is an Image. I have no idea how the data is stored in here (Image, compressed, encrypted, plain text, etc). I am trying to write the contents to...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
2
by: Denise Smith | last post by:
Hello, I'm wondering if anyone can help me out here? I want to be able to browse records in a database where one of the fields contains an image. I think I might have to extract the image...
1
by: Stephen | last post by:
Hi, I am using an Access database (OLE Object) and storing Image as a bytes after streaming. I am able to read it back as a stream and display it on the browser using the following code:...
13
by: Neo Geshel | last post by:
I have examined about 80+ different upload scripts on the 'net, both in VB and C#, and none seem to do what I need them to do. Perhaps someone here can point me somewhere that Google hasn't...
2
by: Chucker | last post by:
Hi Community, I think I can store Binary Data in SQL Server but when I try to retrieve it, I always only get one byte. I think I stored my Binary Data in SQL Server in a Colum of Type Image....
9
by: JuniorProgrammer | last post by:
Please This is queit a task av been trying to solve. Could anyone please tell me how to read a blob from an SQL using a dataset and stuffing this image into the Image control of a webform. Any...
1
by: senort01 | last post by:
Hi, I am trying to read an Access database field that contains an OLE image. I simply want to read in the image, and then be able to use the various filters available in WIA 2. What I can't...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.