Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

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?

Visit http://www.nico.gotdns.com

 

Now ONLINE!

Posted

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 wasn't 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?

Visit http://www.nico.gotdns.com

 

Now ONLINE!

Posted

Yes, i'm totally aware.

 

 

BUT!!!!!!

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

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

 

I've 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. It's also made transparant.

 

You can take a look at it here. It's 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 i'm creating

Visit http://www.nico.gotdns.com

 

Now ONLINE!

  • *Experts*
Posted

Am I missing something? Why can't use the ToBitmap() in conjunction with MakeTransparent()?

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Nope, just tested it. You see the horrible black things on the icon.

 

But I think it's logic. By putting the icon to .ToBitmap() YOU REMOVE ALL THE alpha values. So then it's logic you can't 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)

Visit http://www.nico.gotdns.com

 

Now ONLINE!

  • *Gurus*
Posted
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 you're running on xp. That's all you need to do.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted (edited)

Hi,

 

I've tested your project. It's okay. But I think that's because it's 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 I'm wrong. But I think it's good that we discuss this, because i've been reading many posts on newsgroups and so. Nobody seems to know the answer. Hopefully we can get out of it.

Edited by divil

Visit http://www.nico.gotdns.com

 

Now ONLINE!

  • *Gurus*
Posted

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 you're trying to assign the converted icon to the image property of a picturebox, but as I said, you're actually going to have to draw it manually on to there with the DrawIcon method.

 

Here's a suggestion, why not make an IconBox class, which just overrides OnPaint and draws the icon like that.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Yep, that's right. I've 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???)

Visit http://www.nico.gotdns.com

 

Now ONLINE!

Posted

The last code for the converter is this:

		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 we're coming closer to the solution. But now, some little change have to be made.

 

Can anyone help me?

 

Thanks

Visit http://www.nico.gotdns.com

 

Now ONLINE!

  • *Experts*
Posted

Create a working Bitmap object to work with, rather than just using

CreateBitmap. I'm not sure if it's the problem, but it potentially could

be. Just create a temp bitmap to use, and then use Return myTempBitmap.

  • *Experts*
Posted
Try this.
		Public Shared Function CreateBitmap(ByVal icon As Icon) As Bitmap
		Dim gDest As Graphics, gSrc As Graphics
		Dim tmp As Bitmap
		Dim hdcDest As IntPtr
		Dim hdcSrc As Integer, hdcSrc2 As IntPtr
		Dim hWnd As Integer = icon.Handle.ToInt32
		gSrc = gSrc.FromImage(icon.ToBitmap())
		tmp = New Bitmap(icon.Width, icon.Height)
		gDest = gDest.FromImage(tmp)
		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)

		Return tmp
	End Function

  • *Experts*
Posted

Why can't you just use the Icon.ToBitmap() function (or whatever it's called)?

It seems that this is what you're trying to implement.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...