By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

-----------------------------

"Tony Lugg" <to**@lugg.com> wrote in message
news:a1**************************@posting.google.c om...
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.

Nov 20 '05 #2

P: n/a
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?

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:<uf*************@TK2MSFTNGP12.phx.gbl>...
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

-----------------------------

"Tony Lugg" <to**@lugg.com> wrote in message
news:a1**************************@posting.google.c om...
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.

Nov 20 '05 #3

P: n/a
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.
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.