|
-
Jan 3rd, 2007, 04:12 AM
#1
Thread Starter
Fanatic Member
[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
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. 
-
Jan 3rd, 2007, 05:19 AM
#2
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
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jan 3rd, 2007, 05:35 AM
#3
Thread Starter
Fanatic Member
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. 
-
Jan 3rd, 2007, 05:59 AM
#4
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**
Last edited by VBDT; Jan 3rd, 2007 at 06:40 AM.
-
Jan 3rd, 2007, 06:40 AM
#5
Thread Starter
Fanatic Member
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. 
-
Jan 3rd, 2007, 06:43 AM
#6
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.
-
Jan 3rd, 2007, 09:51 AM
#7
Re: [2005] Icon -> ByteArray -> Icon
 Originally Posted by VBDT
Have you tried this code?
VB Code:
...
Dim myIcon As New Icon(msIcon)
...
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...
-
Jun 28th, 2007, 07:50 PM
#8
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|