[RESOLVED] Image Field in a DataSet?
I have added an Image field to a table (SQL Server 2000) against which I have a Typed DataSet defined. So I added a new column to my DataTable (in my dataset) of type System.Byte and when I attempted to fill the DataSet, I got an error:
"Inconvertible type mismatch between SourceColumn 'Icon_im' of Byte[] and the DataColumn 'Icon_im' of Byte."
I don't see how to defined my DataSet column as a byte array.
If I can't use a typed DataSet to retrieve the image data, how should I then retrieve it?
And while I'm here, how would I then convert this byte array into an image (to display in a picture box)?
Re: Image Field in a DataSet?
how are you defining the column? Would something like this work for you?
VB Code:
Dim MyByteArrayColumn As New DataColumn("Icon_im", GetType(Byte()))
Re: Image Field in a DataSet?
I defined my dataset in the dataset designer window. Perhaps that's my problem...
However, ommitting that column from my dataset, I've found that I can still retrieve that field from the database and access it with the .Item() property (of the DataRow object).
So my problem remains how to convert a ByteArray into an image...?
Re: Image Field in a DataSet?
just read the byte array field into a memory stream object. The memory stream can have bytes written to it.
Then you can create an image from the stream
VB Code:
Image.FromStream()
or
Bitmap.FromSteam()
if you need further help, post with what you come up with and I'll try to help you sort it out.
Re: Image Field in a DataSet?
Dear me...In order to test my image retrieval code, I need to get an image into the database first.
So now I need to do the reverse. I need to convert an image into a byte array. Presumably this needs to be put into a memory stream? Can I work out how to do that? No I can't...:(
Re: Image Field in a DataSet?
here are the 2 functions you will need for conversions
VB Code:
Public Function ImageToByteArray(ByVal Input As Image) As Byte()
Dim MS As New IO.MemoryStream
Input.Save(MS, System.Drawing.Imaging.ImageFormat.Bmp)
Return MS.ToArray
End Function
Public Function ByteArrayToImage(ByVal Input As Byte()) As Image
Dim MS As New IO.MemoryStream(Input)
Return image.FromStream(MS)
End Function
note in the first one, I specified a type of bmp in the input.save method, if you are using gif, or jpg exclusively, you may want to change that accordingly.
Re: Image Field in a DataSet?
Thanks very much. That does the job nicely. :)
Now, if I could only get the image column in my typed dataset, it would be perfect.
But who needs perfect?
Re: Image Field in a DataSet?
There may be other ways to do it with the typed set. Honestly I have never had the need to store images in a DB.
Even if you get it working "good enough" for now, you may want to put a little TODO comment in there to revisit it at some point and try to rework some of the code ;)
Re: Image Field in a DataSet?
Ahem...there still seems to be a little problem.
You see I need to display icons as well as bitmaps. Now I am happy to just convert these icons into bitmaps. And the following code seems to do ok loading an icon (from file) as a bitmap and displaying it in a picture box:
VB Code:
PictureBox.Image = System.Drawing.Bitmap.FromFile(strFileName)
The problem comes after I've converted it to a byte array and stored in the database and then retrieved it again. The background has then gone black (i.e. what used to be the 'transparant' part of the icon). Something's been lost along the way...:(
Re: Image Field in a DataSet?
Is this VB.NET 2003? I wish people would specify when posting. It's one click of a radio button.
Re: Image Field in a DataSet?
Sorry, I hadn't noticed you could select the VB version! :o
It's VB 2005.NET
Re: Image Field in a DataSet?
If you create a Data Source in VS 2005 then it will determine the appropriate type of each column from the database itself, without any intervention from you. That Data Source is then used to configure a typed DataSet, again without intervention from you. This would be, in my opinion, the advisable way to create your typed DataSet.
Re: Image Field in a DataSet?
ImageToByteArray and ByteArrayToImage are great... just saved my day
Re: Image Field in a DataSet?
Quote:
Originally Posted by jmcilhinney
If you create a Data Source in VS 2005 then it will determine the appropriate type of each column from the database itself, without any intervention from you. That Data Source is then used to configure a typed DataSet, again without intervention from you. This would be, in my opinion, the advisable way to create your typed DataSet.
Well, I used the new table adapter wizard to create my data table and it created the image field with a data type of Byte. And when I put that value back into my parameter for my update stored procedure, I got a data type conversion error (my parameter type was Image).
Re: Image Field in a DataSet?
It's not type Byte. It's type Byte(), i.e. an array of Bytes that represents the binary data stored in the field. Unless your IDE is broken it will be the same for you.
Re: Image Field in a DataSet?
Well then I guess my IDE is broken then (as well as everyone else's in my office) because it comes in as type Byte. There isn't even a type Byte() available in the drop down of data types for the column...?
Re: Image Field in a DataSet?
I can't speak for exactly what did or didn't happen when you did what you did because I don't know exactly what you did. What I can tell you is that I just created an MDF database in a new project and added a table with two columns; one type 'int' and one type 'image'. I then created a Data Source and the IDE configured a DataSet with a one DataTable with two DataColumns; one type System.Int32 and one type System.Byte(). I then changed the type of the second column by making a selection from the drop-down list. I then changed the type to System.Byte and manually typed the "()". The IDE automatically created a Byte array column and I was able to manually make one that type too.
Re: Image Field in a DataSet?
Oh, I was able to manually retype the datatype to be "Byte()". That seems to work. Cheers.