API bitmap question

NicoVB

Well-known member
Joined
Jan 2, 2002
Messages
160
Location
Belgium
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.

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?
 
Specification:
-----------------------
this works:
--------------
hdcSrc = Win32.GetWindowDC(hwnd)


BUT I saw that when I tried to write an abstract class without any form, that it picked up a few pixels from the screen. This wasnt at all what I tried to do!!!!


So I wrote:
--------------
hdcSrc = icon.Handle.ToInt32


BUT This is not the same I think.
In the first case I tried to get a Device Context. And in the second case (with the icon) I tried to get a HANDLE.


==> Here is were the error is probably!!!


So this question should be asked:
------------------------------------------------
How do you get a DC from an icon?
 
Are you aware that the Icon class actually has a .ToBitmap() method to do this?
 
Yes, im totally aware.


BUT!!!!!!
------------
With that function you get an image that has lost its alpha channel. So you see an image without ANY quality.

Ive a tool that is called IconConverter that holds the quality of an Icon. There are many threads about it on the newsgroups, but nobody seems to give the answer. But now with my tool, an icon is imported and is directly exported into a .PNG file without losing any quality. Its also made transparant.

You can take a look at it here. Its very briefly and made with also some API classes (the same like here above, but with some changes: GetWindowDC)


==> What I want to do is:
----------------------------------

Comprising this tool INTO one METHODE, which can be possible if you see the code, but with some changes to the API code.



Some Support for the file:
---------------------------------
You will see two projects: NicoLibrary and IconConverter
IconConverter: this is the tool you can use for the conversion
NicoLibrary: see in map IconConverter the class im creating
 
Nope, just tested it. You see the horrible black things on the icon.

But I think its logic. By putting the icon to .ToBitmap() YOU REMOVE ALL THE alpha values. So then its logic you cant make it transparant, I think


Although, the solution has to be found here:

How do you get a DC from an icon?(see posts above)
 
I can process icons with alpha channels just fine, I do it in many of my apps. Add them straight in to an imagelist with a colour depth of 32bits, and make sure your app has an manifest if youre running on xp. Thats all you need to do.
 
Pfffff, so it works not in my application. Im pretty sure that the quality is not as high as I can reach with my control IconBox.

So please send me a little app with your icon and with all its quality.

Thanks
 
It has full quality, why shouldnt it?

If I remember, Ill post a sample app when I get home. I think youll find Ive invested even more hours in getting this right than you have :)
 
Here ya go, this sample shows the contents of your Windows directory, with appropriate icons for files and folders. Icons retain their alpha channel just fine.
 

Attachments

Hi,

Ive tested your project. Its okay. But I think thats because its a listview control. I tested the same code with an imagelist and a picturebox. And you saw the black things behind it.

This is a very little sample with my picturebox and imagelist!!


Sorry if Im wrong. But I think its good that we discuss this, because ive been reading many posts on newsgroups and so. Nobody seems to know the answer. Hopefully we can get out of it.
 
Last edited by a moderator:
If you want to draw them yourself on to a surface like a picturebox, you have to keep them in icon format and use the .DrawIcon method of the Graphics class. Only then will they retain their icon alpha channel.

Oh, I removed your attachment because it had exe files in, but I did try it. I see youre trying to assign the converted icon to the image property of a picturebox, but as I said, youre actually going to have to draw it manually on to there with the DrawIcon method.

Heres a suggestion, why not make an IconBox class, which just overrides OnPaint and draws the icon like that.
 
Yep, thats right. Ive built the IconBox allready(I told you that).

I build these components for icons:
-----------------------------------------------------------

IconBox
IconList
and best of all: the iconConverter (form app)


But THE REASON why I am writing this topic is because I want to find a method to easily convert an icon to a bitmap in the code.

So I picked the same code as for my IconConverter(where I have a form where I draw first the icon). So this is API that works perfectly to convert the bitmaps into a file.

But now I want to convert an icon programmatically to a bitmap without the iconConverter.

So this is why you should take a look at my API code and change it so it works:

==> I think that the DC part of it is wrong. (How DC from icon without FORM???)
 
The last code for the converter is this:
Code:
		Public Shared Function CreateBitmap(ByVal icon As Icon) As Bitmap
			Dim gDest As Graphics, gSrc As Graphics
			Dim hdcDest As IntPtr
			Dim hdcSrc As Integer, hdcSrc2 As IntPtr
			Dim hWnd As Integer = icon.Handle.ToInt32
			gSrc = gSrc.FromImage(icon.ToBitmap())
					CreateBitmap = New Bitmap(icon.Width, icon.Height)
			gDest = gDest.FromImage(CreateBitmap)
			hdcSrc2 = gSrc.GetHdc
			hdcSrc = hdcSrc2.ToInt32()
			hdcDest = gDest.GetHdc
			Win32.BitBlt(hdcDest.ToInt32, 0, 0, icon.Width, icon.Height, hdcSrc, 0, 0, Win32.SRCCOPY)
			gDest.ReleaseHdc(hdcDest)
			gSrc.ReleaseHdc(hdcSrc2)
			Win32.ReleaseDC(hWnd, hdcSrc)
		End Function

This code gives no errors. But it returns a blank Bitmap. So were coming closer to the solution. But now, some little change have to be made.

Can anyone help me?

Thanks
 
Create a working Bitmap object to work with, rather than just using
CreateBitmap. Im not sure if its the problem, but it potentially could
be. Just create a temp bitmap to use, and then use Return myTempBitmap.
 
Back
Top