A serious Icon display Error !

D

Dinesh Jain

Hi al,,
I have written a code which displays directory listing of files from FTP
server directory.I display all the files in a listview with its
associated icons. To display icons, I get the resouce file name and it's
index by searching from the Registry entries.
I use ExtractIconsEx() Win32API to extract icon from exe or dll by
specifying it's index.

I get this error more frequently when I minimize/maximize the window
which displays this list.

Error : "Failed to create a bitmap compatible with the screen to get
information about screen bitmap format."

Stacktrace:
" at
System.Windows.Forms.DibGraphicsBufferManager.bFillBitmapInfo(IntPtr
hdc, IntPtr hpal, BITMAPINFO_FLAT& pbmi)
at
System.Windows.Forms.DibGraphicsBufferManager.CreateCompatibleDIB(IntPtr
hdc, IntPtr hpal, Int32 ulWidth, Int32 ulHeight, IntPtr& ppvBits)
at System.Windows.Forms.DibGraphicsBufferManager.CreateBuffer(IntPtr
src, Int32 offsetX, Int32 offsetY, Int32 width, Int32 height)
at System.Windows.Forms.DibGraphicsBufferManager.AllocBuffer(Graphics
targetGraphics, IntPtr targetDC, Rectangle targetBounds)
at
System.Windows.Forms.DibGraphicsBufferManager.AllocBufferInTempManager(G
raphics targetGraphics, IntPtr targetDC, Rectangle targetBounds)
at System.Windows.Forms.DibGraphicsBufferManager.AllocBuffer(IntPtr
target, Rectangle targetBounds)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeM
ethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32
reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)...


What can be the problem ? no error occurs when I add,delete
icons to the listview.I get this error more frequently when I
minimize/maximize the window which displays this list.
 
K

Ken Tucker [MVP]

Hi,

Please post your code. Did you know you can use the shgetfileinfo
api to extract the icon from any type file.

Ken
 
D

Dinesh Jain

Hi Ken,
Thanks for the reply.
As I said, no error is getting while extracting icons
and adding it to listview. Also, just becuase of this, I am not posting
the code.
The error gets raised when we maximize/minimize the window which
contains this list. Not always but freuquntly.
About shGetFileInfo(), can we use it for extracting
icon related to file type? for example - can I get icon
associated with .jpg file or .htm file?
If yes, can u post some code for it?

Please help,
Thanks in advance,

-Regards,
Dinesh
 
H

Herfried K. Wagner [MVP]

* Dinesh Jain said:
I have written a code which displays directory listing of files from FTP
server directory.I display all the files in a listview with its
associated icons. To display icons, I get the resouce file name and it's
index by searching from the Registry entries.
I use ExtractIconsEx() Win32API to extract icon from exe or dll by
specifying it's index.

Maybe there is a bug in your declare for 'ExtractIconEx' or you are
using the function in a wrong way.

"Post code."
 
D

Dinesh Jain

Hi Herfried,
Thanks for the reply.
Is this declaration wrong?-

Public Declare Function ExtractIconEx Lib "shell32.dll" Alias
"ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Integer,
ByRef phiconLarge As IntPtr, ByRef phiconSmall As IntPtr, ByVal nIcons
As Integer) As IntPtr

Please help,
Thanks in advance,

-Regards,
Dinesh
 
K

Ken Tucker [MVP]

Hi,

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=80)> Public szTypeName As String

End Structure


Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class



Ken
 
D

Dinesh Jain

Hi Ken,
Thanks for the reply.
I am really not getting the way of calling the GetIcon()
method from ur class by specifying the file extention
and not the actual file name. I want to display icon for .jpg file
(for example). I will be very thankful to u if u can explain this with
the code u have posted for me, sorry for this trouble.

Please help,
Thanks in advance,

-Regards,
Dinesh
 
K

Ken Tucker [MVP]

Hi,

This procedure will add all the filenames with there icons to a
listview (lv). You also need and imagelist (imlIcon).

Private Function getLocalIcons(ByVal szFolderPath As String)

Dim dirInfo As New System.IO.DirectoryInfo(szFolderPath)

Dim di As System.IO.DirectoryInfo

Dim fi As System.IO.FileInfo

Dim lvitem As ListViewItem

Dim hIcon As Icon

Dim cIcon As New clsGetIcon

imlIcon.Images.Clear()

lv.Items.Clear()

lv.SmallImageList = imlIcon

For Each di In dirInfo.GetDirectories()

lvitem = lv.Items.Add(di.Name)

hIcon = cIcon.getIcon(di.FullName)

imlIcon.Images.Add(hIcon.ToBitmap)

lvitem.ImageIndex = imlIcon.Images.Count - 1

Next

For Each fi In dirInfo.GetFiles()

lvitem = lv.Items.Add(fi.Name)

hIcon = cIcon.getIcon(fi.FullName)

imlIcon.Images.Add(hIcon.ToBitmap)

lvitem.ImageIndex = imlIcon.Images.Count - 1

Next



End Function



Ken
 
D

Dinesh Jain

Hi Ken,
Again thanks for teh reply.
I don't want to display the icons assocaited wti any
specific file/folder. Actually I don't have the
files existing on my machine. I want to display icons
for the file names those I have retrieved from FTP server. In short, I
have to display icons associated with file types.

Please help,
Thanks in advance,

-Regards,
Dinesh
 

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