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

Creating Image file background colour problem

P: n/a
Hi guys,

I have a problem when trying to create an image file. Basically my
code takes any file and gets it's system icon using SHGetFileInfo and
then saves the file either by converting it into a bmp and saving to
disk or by saving the icon using a filestream. In both cases saving
the file works fine. However, when I open the newly created file from
explorer I notice something peculiar. The file has a black background.
I would have expected the background to be transparent and I can't
understand why vb.net has given it a black bg. Is there any way to
save the file without having the black bg? Any help would be
appreaciated. Code below.

Waqas

//Declare SHGetFileInfo library
Private Declare Auto Function SHGetFileInfo Lib
"shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As
Int32, _
ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Int32, ByVal uFlags As
Int32) As IntPtr

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)_
Private Structure SHFILEINFO
Public hIcon As Int32
Public iIcon As Int32
Public dwAttributes As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)_
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)_
Public szTypeName As String
End Structure

Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1

//Procedure for getting the icon from the filename
Private Sub getIcon(ByRef location As String)
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO()
Dim hImgSmall As IntPtr

hImgSmall = SHGetFileInfo(location, 0, shinfo, Len(shinfo),
SHGFI_ICON Or SHGFI_SMALLICON)

//Get the image icon from the file and then sae it as a bmp
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Dim bm As Bitmap = myIcon.ToBitmap()
bm.Save("C:\typeIcon.bmp",
System.Drawing.Imaging.ImageFormat.Bmp)

//Alternative method for saving image as an icon
'Dim file As IO.FileStream
'file = New FileStream("C:\typeIcon.icoo",
FileMode.OpenOrCreate)
'myIcon.Save(file)
'file.Close()
End Sub

Jul 21 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.