470,586 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Issue With System.Drawing.Image.FromStream

I've seen several newsgroup topics everywhere concerning
this, as well as a couple of articles from the MSDN
website, but this error still baffles me.

It has to do with using the Image.FromStream(...) function
to load an image into a PictureBox control from a byte
array received from an SQL Server database. Here's a
snippet of what I've been trying to achieve:

----------------------------
Dim my_image() As Byte = CType(MyDataTable.Rows(0).Item
("DBImage"), Byte()) 'This is the picture returned back
from the database as array of values. Works fine.

Dim my_stream As New System.IO.MemoryStream
(my_image) 'This also works fine.

picPictureBox.Image = System.Drawing.Image.FromStream
(my_stream) 'This is the line of code that's driving me
CRAZY!!
----------------------------

When that line of code is executed, an unhandled exception
error pops up stating exactly the following:

"An unhandled exception of type 'System.ArgumentException'
occurred in system.drawing.dll

Additional information: Invalid parameter used"

What baffles me is why this error even occurs.
The "FromStream" function takes in a System.IO.Stream
parameter, which I'm giving it, and I've even seen posts
on newsgroups where people have tried the exact same code
and had no error at all, while others still get it.
I'm trying to get away from having to temporarily save the
database picture to the harddrive, and then loading it
using System.IO.FileStream (which, of course, works
without problems) Do I have a bug-ridden version of
System.Drawing, or am I cursed by some hidden coding
demon!?!! Any help at all with this matter would be
AWESOME!!
Nov 21 '05 #1
1 7620
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1). Please note the northwind database offsets the image by 78.
Dim ds As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlConnection

Dim daCustomer As SqlClient.SqlDataAdapter

ds = New DataSet

' strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

'strConn &= "Data Source = Northwind.mdb;"

strConn = "Server = " + Environment.MachineName + "\VSdotNet;"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlConnection(strConn)

daCustomer = New SqlClient.SqlDataAdapter("Select * from Categories", conn)

ds = New DataSet

daCustomer.Fill(ds, "Categories")

ListBox1.DataSource = ds.Tables("Categories")

ListBox1.DisplayMember = "CategoryName"

End Sub

Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedValueChanged

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

End Sub

Ken

-----------------
"Mchuck" <mc****@hotmail.com> wrote in message
news:67****************************@phx.gbl...
I've seen several newsgroup topics everywhere concerning
this, as well as a couple of articles from the MSDN
website, but this error still baffles me.

It has to do with using the Image.FromStream(...) function
to load an image into a PictureBox control from a byte
array received from an SQL Server database. Here's a
snippet of what I've been trying to achieve:

----------------------------
Dim my_image() As Byte = CType(MyDataTable.Rows(0).Item
("DBImage"), Byte()) 'This is the picture returned back
from the database as array of values. Works fine.

Dim my_stream As New System.IO.MemoryStream
(my_image) 'This also works fine.

picPictureBox.Image = System.Drawing.Image.FromStream
(my_stream) 'This is the line of code that's driving me
CRAZY!!
----------------------------

When that line of code is executed, an unhandled exception
error pops up stating exactly the following:

"An unhandled exception of type 'System.ArgumentException'
occurred in system.drawing.dll

Additional information: Invalid parameter used"

What baffles me is why this error even occurs.
The "FromStream" function takes in a System.IO.Stream
parameter, which I'm giving it, and I've even seen posts
on newsgroups where people have tried the exact same code
and had no error at all, while others still get it.
I'm trying to get away from having to temporarily save the
database picture to the harddrive, and then loading it
using System.IO.FileStream (which, of course, works
without problems) Do I have a bug-ridden version of
System.Drawing, or am I cursed by some hidden coding
demon!?!! Any help at all with this matter would be
AWESOME!!
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Toby Mathews | last post: by
2 posts views Thread by Josema | last post: by
2 posts views Thread by Deasun | last post: by
2 posts views Thread by Alvin Bruney - ASP.NET MVP | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.