|
-
Mar 28th, 2004, 10:27 AM
#1
Thread Starter
Lively Member
using streamreader[resolved]
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
Last edited by mindloop; Mar 29th, 2004 at 06:57 PM.
ehmm...
-
Mar 28th, 2004, 10:51 PM
#2
Junior Member
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.
-
Mar 29th, 2004, 05:42 AM
#3
Thread Starter
Lively Member
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:
VB 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
without try...catch...end try the program stops at
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
-
Mar 29th, 2004, 11:51 AM
#4
Junior Member
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.
-
Mar 29th, 2004, 03:21 PM
#5
Thread Starter
Lively Member
thanks i'll keep an eye on this thread.
if i get there first i'll post too.
-
Mar 29th, 2004, 06:44 PM
#6
Junior Member
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
-
Mar 29th, 2004, 06:48 PM
#7
Thread Starter
Lively Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|