473,394 Members | 1,761 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,394 software developers and data experts.

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 8070
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
2
by: Josema | last post by:
Hi, I get from database a field of type binary (its a gif image) into byte until here no problem, cause i veryfied that the binary its stored into the byte variable (called arrayb in my...
2
by: Richard | last post by:
I each time get errors when I use an .bmp or ,.gif file as source but whenever I use a .jpg file it works perfect. Why is that? and how to fix it? I almost have no knowledge about the...
2
by: Deasun | last post by:
Heres the code I have: Dim objTheStream As MemoryStream = New MemoryStream Dim objPhoto As Image = pic_PhotoToUpload.Image() With objPhoto .Save(objTheStream,...
0
by: Taiwo | last post by:
When I use the method "System.Drawing.Image.FromFile(pictureFile)" where pictureFile is the path to a valid image file, the file is locked even minutes after the statement executes. This code is in...
2
by: Alvin Bruney - ASP.NET MVP | last post by:
I'm using C# and GDI+ to create dynamic images and show them on the web. I'm loading the main image which is a jpeg, then I'm drawing some text and shapes on that base image using Graphics class...
4
by: Darrel | last post by:
I'm grabbing a file from a file upload form field. This is a 'system.web.httppostedfile' I would like to modify the image (Cropping/scaling) using system.drawing.image. Is there anyway to go...
7
by: bookon | last post by:
I was running into the System.Drawing.Image.FromStream "parameter is not valid" on some of the images I was retrieving from a blob column in Sql Server. I thought there were corrupt images as...
3
by: Dave Keen | last post by:
Hi all. Hope you can help me. This should be easy but I can't make this work. In brief I am building a page of thumbnails using images held in a SQLServer 2000 database. I do this by creating...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.