473,581 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving an image from SQL Server using dataset

Does anybody have an actual example of retrieving an Image data type column
from a SQL Server table using a dataset (not a datareader)? I would like
to see the statements that would move the Image returned in the dataset to a
picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next

Thanks,
Dean Slindee
Nov 20 '05 #1
6 16112
* "Dean Slindee" <sl*****@charte r.net> scripsit:
Does anybody have an actual example of retrieving an Image data type column
from a SQL Server table using a dataset (not a datareader)? I would like
to see the statements that would move the Image returned in the dataset to a
picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next


The solution depends on how the image is stored in the database. If the
database contains the whole image files and provides them as a byte
array, you can cast 'dr(...)' to 'Byte()' using 'DirectCast', then write
the data to a 'MemoryStream' and instantiate the image from the data in
the memory stream using 'Image.FromStre am'. Notice that the stream must
be kept open as long as the image object is used.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2
Dean,

Cast the Image field to a ByteArray, create a MemoryStream from the
ByteArray, convert the MemoryStream to a Bitmap and assign the BitMap to the
Image property of the PictureBox.

This will only work if the image is a "raw" picture. If it has an OLE
wrapper around it won't work, so if you are trying to display images from
Access or the Pictures in the Northwind database, this won't work unless you
can figure out how to remove the OLE weapper.

It works fine if the image in the field is a raw image only.

Something like:

<code (VB.NET) >
Me.PictureBox1. Image = New Bitmap(New
System.IO.Memor yStream(DirectC ast(Me.DataSet1 1.Tables("Perso nPicture").Rows (
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charte r.net> wrote in message
news:10******** *****@corp.supe rnews.com...
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like
to see the statements that would move the Image returned in the dataset to a picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next

Thanks,
Dean Slindee

Nov 20 '05 #3
Thanks for the code Sam. I have used it below; syntax is correct. Still
get an "Invalid parameter used" message on the "picImage.I mage = ..."
statement. There is one row in the dataset, it gets selected and returned.
The image column can be either "binary" or "image" in SQL Server, same error
message. Loaded a .jpg and/or .gif file from C:/Program Files\Microsoft
Office\Clipart\ Pub60Cor\anyfil e.JPG. Also loaded an image that I personally
scanned myself, same error message. Care to hazard any guesses? Thanks,
Dean
ds = GetImageRowWith ImageID(ImageID )

For Each dr In ds.Tables(0).Ro ws

picImage.Image = New Bitmap(New
System.IO.Memor yStream(DirectC ast(ds.Tables(0 ).Rows(0)("Imag e"), Byte())))

Next

"Samuel L Matzen" <sm*****@slm.co m> wrote in message
news:Ou******** ******@TK2MSFTN GP12.phx.gbl...
Dean,

Cast the Image field to a ByteArray, create a MemoryStream from the
ByteArray, convert the MemoryStream to a Bitmap and assign the BitMap to the Image property of the PictureBox.

This will only work if the image is a "raw" picture. If it has an OLE
wrapper around it won't work, so if you are trying to display images from
Access or the Pictures in the Northwind database, this won't work unless you can figure out how to remove the OLE weapper.

It works fine if the image in the field is a raw image only.

Something like:

<code (VB.NET) >
Me.PictureBox1. Image = New Bitmap(New
System.IO.Memor yStream(DirectC ast(Me.DataSet1 1.Tables("Perso nPicture").Rows ( 0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charte r.net> wrote in message
news:10******** *****@corp.supe rnews.com...
Does anybody have an actual example of retrieving an Image data type column
from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the Image returned in the dataset

to a
picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next

Thanks,
Dean Slindee


Nov 20 '05 #4
Dean,

Best guess is that whatever you have in your database is not a raw picture.
I suspect it is either corrupted or has some sort of wrapper around it that
the picturebox doesn't understand.

-Sam Matzen
"Dean Slindee" <sl*****@charte r.net> wrote in message
news:10******** *****@corp.supe rnews.com...
Thanks for the code Sam. I have used it below; syntax is correct. Still
get an "Invalid parameter used" message on the "picImage.I mage = ..."
statement. There is one row in the dataset, it gets selected and returned. The image column can be either "binary" or "image" in SQL Server, same error message. Loaded a .jpg and/or .gif file from C:/Program Files\Microsoft
Office\Clipart\ Pub60Cor\anyfil e.JPG. Also loaded an image that I personally scanned myself, same error message. Care to hazard any guesses? Thanks,
Dean
ds = GetImageRowWith ImageID(ImageID )

For Each dr In ds.Tables(0).Ro ws

picImage.Image = New Bitmap(New
System.IO.Memor yStream(DirectC ast(ds.Tables(0 ).Rows(0)("Imag e"), Byte())))

Next

"Samuel L Matzen" <sm*****@slm.co m> wrote in message
news:Ou******** ******@TK2MSFTN GP12.phx.gbl...
Dean,

Cast the Image field to a ByteArray, create a MemoryStream from the
ByteArray, convert the MemoryStream to a Bitmap and assign the BitMap to the
Image property of the PictureBox.

This will only work if the image is a "raw" picture. If it has an OLE
wrapper around it won't work, so if you are trying to display images from
Access or the Pictures in the Northwind database, this won't work unless

you
can figure out how to remove the OLE weapper.

It works fine if the image in the field is a raw image only.

Something like:

<code (VB.NET) >
Me.PictureBox1. Image = New Bitmap(New

System.IO.Memor yStream(DirectC ast(Me.DataSet1 1.Tables("Perso nPicture").Rows (
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charte r.net> wrote in message
news:10******** *****@corp.supe rnews.com...
Does anybody have an actual example of retrieving an Image data type

column
from a SQL Server table using a dataset (not a datareader)? I would

like to see the statements that would move the Image returned in the

dataset to
a
picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next

Thanks,
Dean Slindee



Nov 20 '05 #5
Hi,

This will work with the northwind database. Note the northwind
database has an offset of 78 (see the ms.write line) you might be able to
replace the 78 with 0 depending on how it is saved in the database. Included
a link to sample program.

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

http://www.onteorasoftware.com/downl...windimages.zip

Ken

-----------------------

"Samuel L Matzen" <sm*****@slm.co m> wrote in message
news:Ou******** ******@TK2MSFTN GP12.phx.gbl...
Dean,

Cast the Image field to a ByteArray, create a MemoryStream from the
ByteArray, convert the MemoryStream to a Bitmap and assign the BitMap to the
Image property of the PictureBox.

This will only work if the image is a "raw" picture. If it has an OLE
wrapper around it won't work, so if you are trying to display images from
Access or the Pictures in the Northwind database, this won't work unless you
can figure out how to remove the OLE weapper.

It works fine if the image in the field is a raw image only.

Something like:

<code (VB.NET) >
Me.PictureBox1. Image = New Bitmap(New
System.IO.Memor yStream(DirectC ast(Me.DataSet1 1.Tables("Perso nPicture").Rows (
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charte r.net> wrote in message
news:10******** *****@corp.supe rnews.com...
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like
to see the statements that would move the Image returned in the dataset to a picture box, like:

For Each dr in ds.tables(0).ro ws
Pic.Image = dr("ImageColumn Name") 'not this simple!
Next

Thanks,
Dean Slindee


Nov 20 '05 #6
Hi Dean,

When you still strugling have than a look at this sample I once made.

It uses not the database itself however the dataset, but that is the same as
a database, it needs only an update or a select for whatever database

I hope this helps, the sample goes completly, it needs only a formproject, a
picturebox on it and 4 buttons. (When you use something else than a pic you
have to delete the picturebox part, that is by the way only for showing).

I hope this helps

Cor

Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
'Reading a pictur from file and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(f o.FileName, _
IO.FileMode.Ope n)
Dim br As New IO.BinaryReader (fs)
abyt = br.ReadBytes(CI nt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream (abyt)
Me.PictureBox1. Image = Image.FromStrea m(ms)
End If
End Sub

Private Sub Button2_Click(B yVal sender As System.Object, ByVal _
e As System.EventArg s) Handles Button2.Click
'writing a picture from a bytearray to a file
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(s f.FileName, _
IO.FileMode.Cre ateNew)
Dim bw As New IO.BinaryWriter (fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(B yVal sender As System.Object, ByVal _
e As System.EventArg s) Handles Button3.Click
'writing a bytearray to a dataset and than to disk
Dim ds As New DataSet
ds.Tables.Add(N ew DataTable("Phot o"))
ds.Tables(0).Co lumns.Add(New DataColumn("Sam ple"))
ds.Tables(0).Co lumns(0).DataTy pe =
System.Type.Get Type("System.By te[]")
ds.Tables(0).Ro ws.Add(ds.Table s(0).NewRow)
ds.Tables(0).Ro ws(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf. FileName, XmlWriteMode.Wr iteSchema)
End If
End Sub

Private Sub Button4_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button4.Click
'reading a picture from a dataset from disk and set it in the pic
box
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.F ileName)
End If
abyt = CType(ds.Tables (0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream (abyt)
Me.PictureBox1. Image = Image.FromStrea m(ms)
End Sub
Nov 20 '05 #7

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

Similar topics

7
11608
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an...
18
6498
by: Auto | last post by:
Hello, I would like to know how do display an image into a Gridview (ASP.NET 2.0) taken directly from a DataBase, NOT using an URL, like described in this article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/GridViewEx06.asp I have NO URL, i have the binary image inside the DB, like the field "Photo" in...
7
2052
by: Sirplaya | last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I have no problem with the images displaying, however, I am trying to wrap the image with an <A HREF ..." and each time I try, it acts like the link is not even on the image. What is the proper way to do this? Thanks.
1
9439
by: jimmyfo | last post by:
Hi, I recently wrote an ASP.Net web application in VS2005 and published (using VS2005 Publish feature) it to a relatively clean machine with ASP.Net 2.0 and MDAC 2.8 installed on it. However, when I try to create my SQL connection in the code-behind, I get the following error. I tried to register the DLL using regsvr32 but that errored out...
9
7969
by: matt | last post by:
hello, im doing my first ASP.NET app that inserts & retrieves files from Oracle (no need for a discussion on *that*!). i learned first-hand of the somewhat awkward technique for inserting binary data into an Oracle BLOB column via ADO.NET. since my files are larger than 33k, it seemed had to use this technique: ...
5
2639
by: Randy Smith | last post by:
Hi ALL, I wonder if anyone has been using n-tier to bind to a GridView control by using the ObjectDataSource. This is our first OOP web application, and we have no tables. Right now we are simply working with objects in memory. So, it appears as though Microsoft requires that our datamapper classes reside inside a folder called...
6
5842
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList ID="FldType_add" Runat="server" DataSource='< %#GetFieldType()%>' DataValueField="Type" DataTextField="Type" /> Oce the page is loaded all the...
1
2433
by: san123456789 | last post by:
Hi, Im using a MS SQL server to store images... Im creating a windows form application using C#. I need to store and retrieve images from the database. This is my code to store images: pictureBox.Image = new System.Drawing.Bitmap(this.openFileDialog.FileName);
0
1822
by: velmani | last post by:
hi , i have problem with Retrieving image from DB i have a table with image datatype i store a file by using code as follows -------------------------------------------------- string strType; int intLength,intStatus; Stream ipStream; intLength = File1.PostedFile.ContentLength; strType =...
0
7868
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
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...
0
8304
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...
1
5674
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...
0
5364
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...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.