Issue With System.Drawing.Image.FromStream

M

Mchuck

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!!
 
K

Ken Tucker [MVP]

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.SelectedIndex)

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

-----------------
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!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top