472,985 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 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 8012
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.