473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ge tBytes(0, 0, null,
0, int.MaxValue))];
oDatareader.Get Bytes(0, 0, bytes, 0, bytes.Length);

System.Drawing. Image img =
System.Drawing. Image.FromStrea m(new MemoryStream(by tes));

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

byte[] Test =
System.Text.UTF 8Encoding.UTF8. GetBytes(ImageS tring);
Image x = Image.FromStrea m(new MemoryStream(Te st));

But no go.

Can anyone reccomend anything? I appreciate any help.

Thanks,

ed

Feb 14 '07 #1
2 7377
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.publi c.sqlserver
groups.


On Wed, 14 Feb 2007 22:28:44 +0100, Ed <ed*@nait.ab.ca wrote:
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.Ge tBytes(0, 0, null,
0, int.MaxValue))];
oDatareader.Get Bytes(0, 0, bytes, 0, bytes.Length);

System.Drawing. Image img =
System.Drawing. Image.FromStrea m(new MemoryStream(by tes));

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

byte[] Test =
System.Text.UTF 8Encoding.UTF8. GetBytes(ImageS tring);
Image x = Image.FromStrea m(new MemoryStream(Te st));

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.GetStr ing(1);

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

// Create a file to hold the output.
using (FileStream fs = new FileStream(@"Im ages\" +
pub_id + ".gif", FileMode.OpenOr Create, FileAccess.Writ e))
{
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.GetByt es(2, startIndex,
outbyte, 0, bufferSize);

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

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

// Write the remaining buffer.
if( retval 0 )
bw.Write(outbyt e, 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
7086
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 disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
4
7614
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 a text file, image file, etc so I can see if the data is stored in a way we can understand (we have been tasked to write an app and the app needs to read this field, but we don't know what it really contains). How would I go about reading the...
3
3631
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 application, while I am doing for web. I can only find Image from web forms control and HTML control. This may be the root cause of my problem. For read button, I converted his VB to the C#. But the compiler complains:
2
1496
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 to a byte array, write it to a temp file then refer to the temp file as the URL source for the Image control.
1
1442
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: Response.OutputStream.Write(fileData, 0, fileData.Length) it displays on the browser as an image, but I want to display it in an Image (web control image) so that I can manipulate its size. How can I do this?
13
2345
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 reached yet (I have gone all the way to page 50 on Google's results!!). Here are my requirements: • I have a DataGrid. Everything will be done from here. Everything. No exceptions. Everything will also be done in VB, without any code-behind to...
2
3745
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. At least when I execute the following code, I get some significant network traffic. When I check the database with query analyzer, I see 4 Hex Chars in the image colum. Like 0xe0 etc.
9
1844
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 suggetion that would help would be highly appreciated. -- Iyke
1
1821
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 figure out how to do, however, is to simply read out the image data and create an image object that WIA can recognize. The only solution I've seen online is to stream the OLE object to a file and then open up the file using WIA. This seems like a lot...
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.