Results 1 to 8 of 8

Thread: [RESOLVED] Icon -> ByteArray -> Icon

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    [RESOLVED] Icon -> ByteArray -> Icon

    I need to store and retrieve icons in my database (SQL Server 2000, Image field). I have a function to convert the icon to a byte array so it can be stored in the database:
    VB Code:
    1. Public Function GetByteArrayFromIcon(ByVal icoSource As Icon) As Byte()
    2.     Dim msIcon As System.IO.MemoryStream
    3.  
    4.     msIcon = New System.IO.MemoryStream
    5.  
    6.     icoSource.Save(msIcon)
    7.  
    8.     Return msIcon.ToArray
    9. End Function
    But I'm having trouble converting a byte array back into an icon. I've got this so far...but how do I complete it?
    VB Code:
    1. Public Function GetIconFromByteArray(ByVal bytSource As Byte()) As Icon
    2.     Dim msIcon As System.IO.MemoryStream
    3.  
    4.     msIcon = New System.IO.MemoryStream(bytSource)
    5.  
    6.     Return ?????
    7. End Function
    Last edited by simonm; Jan 3rd, 2007 at 06:40 AM.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: [2005] Icon -> ByteArray -> Icon

    I think this will do the trick.
    Problem is, that I don't know what would happen if the icon contains an alpha channel.

    VB Code:
    1. Dim bmp As Bitmap = CType(Bitmap.FromStream(msIcon), Bitmap)
    2.         Dim icIcon As Icon = Drawing.Icon.FromHandle(bmp.GetHicon)
    3.         b.Dispose()
    4.         Return icIcon
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: [2005] Icon -> ByteArray -> Icon

    By alpha channel, you mean the bit that contains the transparancy information I presume? I hope it does retain it because that's precisely why I need an icon and am not using a bitmap.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Icon -> ByteArray -> Icon

    Have you tried this code?
    VB Code:
    1. Public Function GetByteArrayFromIcon(ByVal icoSource As Icon) As Byte()
    2.         Dim byteArray As Byte()
    3.         Using msIcon As New System.IO.MemoryStream
    4.             icoSource.Save(msIcon)
    5.             byteArray = msIcon.ToArray
    6.         End Using
    7.         Return byteArray
    8.     End Function
    9.  
    10.     Public Function GetIconFromByteArray(ByVal bytSource As Byte()) As Icon
    11.         Using msIcon As New IO.MemoryStream(bytSource)
    12.             Dim myIcon As New Icon(msIcon)
    13.             Return myIcon
    14.         End Using
    15.         Return Nothing
    16.     End Function
    Edit**

  5. #5

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: [2005] Icon -> ByteArray -> Icon

    Thanks VBDT, that does the trick!
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [RESOLVED] Icon -> ByteArray -> Icon

    You need to close the MemoryStream in the "GetByteArrayFromIcon" function. By using "Using" key word it will do it automaticly. I updated the code with two functions.

  7. #7
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: [2005] Icon -> ByteArray -> Icon

    Quote Originally Posted by VBDT
    Have you tried this code?
    VB Code:
    1. ...
    2.             Dim myIcon As New Icon(msIcon)
    3. ...
    I can't believe I totally missed the constuctor

    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  8. #8
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: [RESOLVED] Icon -> ByteArray -> Icon

    Did this solve the alpha info in the icon.
    And what did you do to get the icon path... where in code is this set?


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