hi there,
I've got an image stored in a database as type OLEobject.
What i don't know is how to read this using streamreader and use as Image object in vb.net.
Anyone ?
thanks
Printable View
hi there,
I've got an image stored in a database as type OLEobject.
What i don't know is how to read this using streamreader and use as Image object in vb.net.
Anyone ?
thanks
Try the following. It might work for you. I am having problem using it for accessing data from Access database's memo field using oledb.
http://www.edneeis.com/tutorial.aspx?ID=7
This is from a similar quest of mine in a separate thread and provided by user edneeis.
thanks for your reply,
but, well the problem is still there. i spent some hours just to get where edneeis has got before me, basically i kinda got to the same code. but it never stops throwing exceptions and i'm kinda tired . here is my code:
without try...catch...end try the program stops atVB Code:
Public Function readimage(ByVal idfirma As Integer) As Image Dim img As Image Dim ImageByte As Byte() OleDbConnection1.Open() ComandaSQL.CommandText = "select logo from firme where idfirma =" & idfirma 'comandasql is defined as oledbcommand ImageByte = ComandaSQL.ExecuteScalar() OleDbConnection1.Close() Try Dim streamx As New MemoryStream() streamx.Write(ImageByte, 0, ImageByte.GetUpperBound(0)) img = Image.FromStream(streamx) streamx.Close() Catch ex As Exception callMsg(ex.ToString) 'callMsg is my custom MsgBox End Try Return img End Function
img = image.fromstream(streamx)
throwing this exception:
"An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll
Additional information: Invalid parameter used."
i compared the code with edneis'es code iand it's basically the same, just i use connection to db instead of datarow.
when pausing and placing cursor over imagebyte a tooltip shows me something like length={27569} , wich suggests the image was read.
for writing the image into database i use this code:
VB Code:
Private Sub SaveImage(ByVal imageX As Image, ByVal idfirma As Integer) Dim ImageStream As New MemoryStream() imageX.Save(ImageStream, imageX.RawFormat) Dim FileByteArray(CInt(ImageStream.Length)) As Byte ImageStream.Read(FileByteArray, 0, CInt(ImageStream.Length)) Try OleDbConnection1.Open() ComandaSQL.CommandText = "update firme set logo= ? where idfirma =" & idfirma.tostring ComandaSQL.Parameters.Add("Logo", System.Data.OleDb.OleDbType.Binary, CInt(ImageStream.Length)).Value = FileByteArray ComandaSQL.ExecuteNonQuery() OleDbConnection1.Close() Catch ex As Exception callMsg(ex.ToString) End Try ImageStream.Close() End Sub
I am afraid I can't help you much here. I used similar method, only going the db->bytes and get invalid cast error when reading from database.
I am going to experiment a little, don't know when.
Another option is customizing the image file:
image->memorstream->byte()-> string via stringbuilder->db.
and on the way out from db
db->string->stringbuilder->byte()->image.
I've made it work for image->string->image. If I can make it work for the entire route, I'll post it here.
Good luck.
thanks i'll keep an eye on this thread.
if i get there first i'll post too.
Using a memo field instead of OLE Object in Access Database was my problem. May be that's true in your case too. Check out the related thread.
http://www.vbforums.com/showthread.php?s=&postid=1659002#post1659002
i managed to solve this issue by saving the image to a temp file on hdd then using filestream to read the file
i was using from the begining oleobject
it works fine now for me
if you like i'l share the code just post and say so.