T
Tony Lugg
I have an application with a document management form. When users add
documents to the form, I call the API function SHGetFileInfo to get
the associated large and small icons for the file. These icons are
added to two ImageList objects which are bound to a ListView control,
and everything looks great. I am saving the icons to a SQL Server
table by using Icon.Save to a stream and assigning the byte array to
the field, then loading them back when I open the form.
Here's the problem: the large icons load fine but the small ones give
an error stating "Invalid Parameter Used". Below are the two routines
for saving and loading the icons:
Public Function IconToBytes(ByVal icon As Icon) As Byte()
'Converts an Icon object into an array of bytes so
'it can be saved to a database field
Dim loMemoryStream As New IO.MemoryStream
icon.Save(loMemoryStream)
Dim laIcon(loMemoryStream.Length - 1) As Byte
loMemoryStream.Position = 0
loMemoryStream.Read(laIcon, 0, loMemoryStream.Length)
Return laIcon
End Function
Public Function DataFieldToImage(ByVal oField As Object) As Image
'Converts an array of bytes to an Icon object when read
'from a database field.
If Not IsDBNull(oField) Then
Dim laImage() As Byte = oField
Dim loMemoryStream As New IO.MemoryStream(laImage)
Return Image.FromStream(loMemoryStream)
Else
Return Nothing
End If
End Function
I could not figure out how to load directly into an Icon object, so it
is loading into an Image object. Does ImageFormat.Icon require 32x32
to create, and if so, why am I able to assign it using
Icon.FromHandle(SHI.hIcon) when I use SHGetFileInfo to retrieve the
icons; I suppose because the icon already exists in memory?
I tried using the routine below with various image formats to save in
something other than Icon format but the transparency is always lost,
and since I am loading Icons, I can't set the ImageList Transparent
color, as I need true transparency.
For now, I am using the large icon for both large and small image
lists and letting the small image list shrink the image to 16 x 16.
But, the image quality is not as good.
Any advise would be greatly appreciated. Thanks in advance.
documents to the form, I call the API function SHGetFileInfo to get
the associated large and small icons for the file. These icons are
added to two ImageList objects which are bound to a ListView control,
and everything looks great. I am saving the icons to a SQL Server
table by using Icon.Save to a stream and assigning the byte array to
the field, then loading them back when I open the form.
Here's the problem: the large icons load fine but the small ones give
an error stating "Invalid Parameter Used". Below are the two routines
for saving and loading the icons:
Public Function IconToBytes(ByVal icon As Icon) As Byte()
'Converts an Icon object into an array of bytes so
'it can be saved to a database field
Dim loMemoryStream As New IO.MemoryStream
icon.Save(loMemoryStream)
Dim laIcon(loMemoryStream.Length - 1) As Byte
loMemoryStream.Position = 0
loMemoryStream.Read(laIcon, 0, loMemoryStream.Length)
Return laIcon
End Function
Public Function DataFieldToImage(ByVal oField As Object) As Image
'Converts an array of bytes to an Icon object when read
'from a database field.
If Not IsDBNull(oField) Then
Dim laImage() As Byte = oField
Dim loMemoryStream As New IO.MemoryStream(laImage)
Return Image.FromStream(loMemoryStream)
Else
Return Nothing
End If
End Function
I could not figure out how to load directly into an Icon object, so it
is loading into an Image object. Does ImageFormat.Icon require 32x32
to create, and if so, why am I able to assign it using
Icon.FromHandle(SHI.hIcon) when I use SHGetFileInfo to retrieve the
icons; I suppose because the icon already exists in memory?
I tried using the routine below with various image formats to save in
something other than Icon format but the transparency is always lost,
and since I am loading Icons, I can't set the ImageList Transparent
color, as I need true transparency.
For now, I am using the large icon for both large and small image
lists and letting the small image list shrink the image to 16 x 16.
But, the image quality is not as good.
Any advise would be greatly appreciated. Thanks in advance.