I have the code here from my class: IconConverter. If I use this class in conjunction with a form it works (but then with GetWindowDC function in the line with hdcSrc). But here is the code where I try to get an Image out of an icon in the function.
But I get nothing. I retrieve nothing. So what is the problem here?
Code:
Public Class IconConverter
Public Class Win32
Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer
Public Const SRCCOPY As Integer = &HCC0020
End Class
Public Class Hardcopy
Public Shared Function CreateBitmap(ByVal icon As Icon) As Bitmap
Dim gDest As Graphics
Dim hdcDest As IntPtr
Dim hdcSrc As Integer
Dim hWnd As Integer = icon.Handle.ToInt32
CreateBitmap = New Bitmap(icon.Width, icon.Height)
gDest = gDest.FromImage(CreateBitmap)
hdcSrc = icon.Handle.ToInt32
hdcDest = gDest.GetHdc
Win32.BitBlt(hdcDest.ToInt32, 0, 0, icon.Width, icon.Height, hdcSrc, 0, 0, Win32.SRCCOPY)
gDest.ReleaseHdc(hdcDest)
Win32.ReleaseDC(hWnd, hdcSrc)
End Function
End Class
Public Function ConvertIconToBitmap(ByVal icon As Icon, ByVal transparant As Boolean) As Bitmap
If Not icon Is Nothing Then
Dim bmp As Bitmap
bmp = Hardcopy.CreateBitmap(icon)
bmp.MakeTransparent()
Return bmp
End If
End Function
But I get nothing. I retrieve nothing. So what is the problem here?