473,399 Members | 3,401 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,399 software developers and data experts.

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).rows
Pic.Image = dr("ImageColumnName") 'not this simple!
Next

Thanks,
Dean Slindee
Nov 20 '05 #1
6 16100
* "Dean Slindee" <sl*****@charter.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).rows
Pic.Image = dr("ImageColumnName") '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.FromStream'. 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.MemoryStream(DirectCast(Me.DataSet11.Tab les("PersonPicture").Rows(
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charter.net> wrote in message
news:10*************@corp.supernews.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).rows
Pic.Image = dr("ImageColumnName") '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.Image = ..."
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\anyfile.JPG. Also loaded an image that I personally
scanned myself, same error message. Care to hazard any guesses? Thanks,
Dean
ds = GetImageRowWithImageID(ImageID)

For Each dr In ds.Tables(0).Rows

picImage.Image = New Bitmap(New
System.IO.MemoryStream(DirectCast(ds.Tables(0).Row s(0)("Image"), Byte())))

Next

"Samuel L Matzen" <sm*****@slm.com> wrote in message
news:Ou**************@TK2MSFTNGP12.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.MemoryStream(DirectCast(Me.DataSet11.Tab les("PersonPicture").Rows( 0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charter.net> wrote in message
news:10*************@corp.supernews.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).rows
Pic.Image = dr("ImageColumnName") '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*****@charter.net> wrote in message
news:10*************@corp.supernews.com...
Thanks for the code Sam. I have used it below; syntax is correct. Still
get an "Invalid parameter used" message on the "picImage.Image = ..."
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\anyfile.JPG. Also loaded an image that I personally scanned myself, same error message. Care to hazard any guesses? Thanks,
Dean
ds = GetImageRowWithImageID(ImageID)

For Each dr In ds.Tables(0).Rows

picImage.Image = New Bitmap(New
System.IO.MemoryStream(DirectCast(ds.Tables(0).Row s(0)("Image"), Byte())))

Next

"Samuel L Matzen" <sm*****@slm.com> wrote in message
news:Ou**************@TK2MSFTNGP12.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.MemoryStream(DirectCast(Me.DataSet11.Tab les("PersonPicture").Rows(
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charter.net> wrote in message
news:10*************@corp.supernews.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).rows
Pic.Image = dr("ImageColumnName") '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("Categories").Rows(ListBox1.SelectedInde x)

Dim ms As New System.IO.MemoryStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Picture")

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

bm = New Bitmap(ms)

PictureBox1.Image = bm

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

Ken

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

"Samuel L Matzen" <sm*****@slm.com> wrote in message
news:Ou**************@TK2MSFTNGP12.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.MemoryStream(DirectCast(Me.DataSet11.Tab les("PersonPicture").Rows(
0)("Picture"), Byte())))
</code>
-Sam Matzen
"Dean Slindee" <sl*****@charter.net> wrote in message
news:10*************@corp.supernews.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).rows
Pic.Image = dr("ImageColumnName") '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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

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

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset and than to disk
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(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
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...
18
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: ...
7
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...
1
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...
9
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...
5
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...
6
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...
1
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: ...
0
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; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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
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 project—planning, coding, testing,...

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.