Problem reading small icon from database (repeat post PLEASE HELP)

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

Ken Tucker [MVP]

Hi,

Try this instead of return image.fromstream
Dim ico As New Icon(loMemoryStream, 16, 16) ' for large icons use 32, 32

Return ico

Ken
 
T

Tony Lugg

Thank you Ken, the 16x16 icons are now being displayed. However,
there is now another minor (hopefully) issue - the 16x16 icon's
transparent background displays as black (32x32 is fine). If I
display the associated icon directly, as I do when I first add a
document to the list, it displays properly, but after saving to the
database and reloading, the backgroud is black. Below is the
definition of the image list:

Me.imglstSmallIcons.ImageSize = New System.Drawing.Size(16, 16)
Me.imglstSmallIcons.TransparentColor =
System.Drawing.Color.Transparent

Any idea what might be causing this?
 
T

Tony Lugg

I found an alternate way to accomplish what I needed. Instead of
storing the large and small icons in a databse table, I was able to
set the USEFILEATTRIBUTES flag for the SHGetFileInfo function which
does not require access to the file itself - in fact I only need pass
in the file extention to retrieve the associated icons.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top