NicoVB Posted January 1, 2003 Posted January 1, 2003 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? Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 1, 2003 Author Posted January 1, 2003 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? Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Gurus* divil Posted January 1, 2003 *Gurus* Posted January 1, 2003 Are you aware that the Icon class actually has a .ToBitmap() method to do this? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
NicoVB Posted January 1, 2003 Author Posted January 1, 2003 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 Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 1, 2003 Author Posted January 1, 2003 (edited) Here are the files:projects.zip Edited August 22, 2004 by PlausiblyDamp Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 2, 2003 Author Posted January 2, 2003 Hope you can solve the problem now. Seems the me that it is a question about details in the code. Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Experts* Nerseus Posted January 2, 2003 *Experts* Posted January 2, 2003 Am I missing something? Why can't use the ToBitmap() in conjunction with MakeTransparent()? -ner Quote "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
NicoVB Posted January 2, 2003 Author Posted January 2, 2003 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) Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Gurus* divil Posted January 2, 2003 *Gurus* Posted January 2, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
NicoVB Posted January 2, 2003 Author Posted January 2, 2003 Pfffff, so it works not in my application. I'm 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 Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 3, 2003 Author Posted January 3, 2003 Can you send me a very simple one? I will try to prove it has less quality!! Thanks NicoVB Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Gurus* divil Posted January 3, 2003 *Gurus* Posted January 3, 2003 It has full quality, why shouldn't it? If I remember, I'll post a sample app when I get home. I think you'll find I've invested even more hours in getting this right than you have :) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
NicoVB Posted January 3, 2003 Author Posted January 3, 2003 I hope you'll surprise me!!!! :D Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Gurus* divil Posted January 4, 2003 *Gurus* Posted January 4, 2003 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.explorericons.zip Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
NicoVB Posted January 4, 2003 Author Posted January 4, 2003 (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 January 4, 2003 by divil Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Gurus* divil Posted January 4, 2003 *Gurus* Posted January 4, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
NicoVB Posted January 4, 2003 Author Posted January 4, 2003 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???) Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 5, 2003 Author Posted January 5, 2003 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 Quote Visit http://www.nico.gotdns.com Now ONLINE!
NicoVB Posted January 8, 2003 Author Posted January 8, 2003 Can anyone help me, pleaaaaaaaaaaaaaaaaassseeeee!!! Nico Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Experts* Volte Posted January 8, 2003 *Experts* Posted January 8, 2003 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. Quote
NicoVB Posted January 10, 2003 Author Posted January 10, 2003 I don't understand really what you mean. Could you change the code? Thanks Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Experts* Volte Posted January 10, 2003 *Experts* Posted January 10, 2003 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 Quote
NicoVB Posted January 11, 2003 Author Posted January 11, 2003 Unfortunately this gives the same result. It returns an empty bitmap!!!! Are there no real API mans here on the forum?? Quote Visit http://www.nico.gotdns.com Now ONLINE!
*Experts* Volte Posted January 11, 2003 *Experts* Posted January 11, 2003 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. Quote
NicoVB Posted January 11, 2003 Author Posted January 11, 2003 Quality reasons, more information in this topic on the first page Quote Visit http://www.nico.gotdns.com Now ONLINE!
Recommended Posts