Results 1 to 7 of 7

Thread: using streamreader[resolved]

  1. #1

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Question 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...

  2. #2
    Junior Member
    Join Date
    Mar 2004
    Posts
    22
    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.

  3. #3

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    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:
    1. Public Function readimage(ByVal idfirma As Integer) As Image
    2.         Dim img As Image
    3.         Dim ImageByte As Byte()
    4.         OleDbConnection1.Open()
    5.         ComandaSQL.CommandText = "select logo from firme where idfirma =" & idfirma  'comandasql is defined  as oledbcommand
    6.         ImageByte = ComandaSQL.ExecuteScalar()
    7.         OleDbConnection1.Close()
    8.         Try
    9.             Dim streamx As New MemoryStream()
    10.             streamx.Write(ImageByte, 0, ImageByte.GetUpperBound(0))
    11.             img = Image.FromStream(streamx)
    12.             streamx.Close()
    13.         Catch ex As Exception
    14.             callMsg(ex.ToString) 'callMsg is my custom MsgBox
    15.         End Try
    16.         Return img
    17.     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:
    1. Private Sub SaveImage(ByVal imageX As Image, ByVal idfirma As Integer)
    2.         Dim ImageStream As New MemoryStream()
    3.         imageX.Save(ImageStream, imageX.RawFormat)
    4.         Dim FileByteArray(CInt(ImageStream.Length)) As Byte
    5.         ImageStream.Read(FileByteArray, 0, CInt(ImageStream.Length))
    6.         Try
    7.             OleDbConnection1.Open()
    8.             ComandaSQL.CommandText = "update firme set logo=  ? where idfirma =" & idfirma.tostring
    9.             ComandaSQL.Parameters.Add("Logo", System.Data.OleDb.OleDbType.Binary, CInt(ImageStream.Length)).Value = FileByteArray
    10.             ComandaSQL.ExecuteNonQuery()
    11.             OleDbConnection1.Close()
    12.         Catch ex As Exception
    13.             callMsg(ex.ToString)
    14.         End Try
    15.         ImageStream.Close()
    16.     End Sub
    ehmm...

  4. #4
    Junior Member
    Join Date
    Mar 2004
    Posts
    22

    Post

    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.

  5. #5

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    thanks i'll keep an eye on this thread.
    if i get there first i'll post too.
    ehmm...

  6. #6
    Junior Member
    Join Date
    Mar 2004
    Posts
    22
    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

  7. #7

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    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.
    ehmm...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width