[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:
Public Function GetByteArrayFromIcon(ByVal icoSource As Icon) As Byte()
Dim msIcon As System.IO.MemoryStream
msIcon = New System.IO.MemoryStream
icoSource.Save(msIcon)
Return msIcon.ToArray
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:
Public Function GetIconFromByteArray(ByVal bytSource As Byte()) As Icon
Dim msIcon As System.IO.MemoryStream
msIcon = New System.IO.MemoryStream(bytSource)
Return ?????
End Function
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:
Dim bmp As Bitmap = CType(Bitmap.FromStream(msIcon), Bitmap)
Dim icIcon As Icon = Drawing.Icon.FromHandle(bmp.GetHicon)
b.Dispose()
Return icIcon
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.
Re: [2005] Icon -> ByteArray -> Icon
Have you tried this code?
VB Code:
Public Function GetByteArrayFromIcon(ByVal icoSource As Icon) As Byte()
Dim byteArray As Byte()
Using msIcon As New System.IO.MemoryStream
icoSource.Save(msIcon)
byteArray = msIcon.ToArray
End Using
Return byteArray
End Function
Public Function GetIconFromByteArray(ByVal bytSource As Byte()) As Icon
Using msIcon As New IO.MemoryStream(bytSource)
Dim myIcon As New Icon(msIcon)
Return myIcon
End Using
Return Nothing
End Function
Edit**
Re: [2005] Icon -> ByteArray -> Icon
Thanks VBDT, that does the trick! :)
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.
Re: [2005] Icon -> ByteArray -> Icon
Quote:
Originally Posted by VBDT
Have you tried this code?
VB Code:
...
Dim myIcon As New Icon(msIcon)
...
:o I can't believe I totally missed the constuctor :o
:wave:
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?
:)