Load bitmap from resource in VB with GDI Api
-
Hi I have a ActiveX control in V.Basic and need to load a bitmap and obtain the handle to use with API functions eg: SelectObject,GetObject,TransparentBlt,etc. It´s work fine if the image is loaded from a file eg: hImageGray = LoadImage(App.hInstance, "c:\gray.bmp", IMAGE_BITMAP, 0, 0, LR_COLOR Or LR_LOADFROMFILE) The problem is when I try to load the bitmap from a resource file added in the ActiveX control project. I try from different methods: "ID_GRAY = 102 hImageGray = LoadImage(App.hInstance, CLng(ID_GRAY), IMAGE_BITMAP, 0, 0, LR_COLOR )" " LoadBitmap(App.hInstance, ID_GRAY)" "LoadBitmap(App.hInstance, "102") " Ever with the same result. No work". Also try to use the v.Basic method to load the image with LoadResPicture, returning a IPictureDisp with the image loaded. This solution have a problem: the handle property of IPictureDisp isn't a bitmap handle to use with the GDI API´s. Any ideas to solve this problem??? Thank´s (and sorry by my bad english! :-) MrSparc.
-
Hi I have a ActiveX control in V.Basic and need to load a bitmap and obtain the handle to use with API functions eg: SelectObject,GetObject,TransparentBlt,etc. It´s work fine if the image is loaded from a file eg: hImageGray = LoadImage(App.hInstance, "c:\gray.bmp", IMAGE_BITMAP, 0, 0, LR_COLOR Or LR_LOADFROMFILE) The problem is when I try to load the bitmap from a resource file added in the ActiveX control project. I try from different methods: "ID_GRAY = 102 hImageGray = LoadImage(App.hInstance, CLng(ID_GRAY), IMAGE_BITMAP, 0, 0, LR_COLOR )" " LoadBitmap(App.hInstance, ID_GRAY)" "LoadBitmap(App.hInstance, "102") " Ever with the same result. No work". Also try to use the v.Basic method to load the image with LoadResPicture, returning a IPictureDisp with the image loaded. This solution have a problem: the handle property of IPictureDisp isn't a bitmap handle to use with the GDI API´s. Any ideas to solve this problem??? Thank´s (and sorry by my bad english! :-) MrSparc.
-
Yes, thanks for you help. Now you are right, the variable does not get set to null at once, but if I try to create a new DC and select an old hBitmap in it, it return error...meaning a bitmap has been deleted successfully. Thanks again. .
Haha...sorry man, I clicked it by accident, I actually wanted to reply to your answer to me just two lines before yours in this table. Anyways, with respect to your question, I had the same problem and never succeeded in loading a bitmap from the resource. I perused all the books I had, Web sites, posted the same question at a several developer, sites...but nope, no answer. I guess it is not possible to explain to an API functions to use a bitmap from a VB resource. They made those wrapper functions to retreive an object from the resource, like LoadResPicture and alike, but it was never meant to be used by API functions. I also find it strange, for it is only logical if you use LR_LOADFROMFILE that you can use another constant to retreive a bitmap from the resource for example...but I don't think it is possible. Anyways, thanks for your answer to my inquiry, you were absolutelly right.
-
Haha...sorry man, I clicked it by accident, I actually wanted to reply to your answer to me just two lines before yours in this table. Anyways, with respect to your question, I had the same problem and never succeeded in loading a bitmap from the resource. I perused all the books I had, Web sites, posted the same question at a several developer, sites...but nope, no answer. I guess it is not possible to explain to an API functions to use a bitmap from a VB resource. They made those wrapper functions to retreive an object from the resource, like LoadResPicture and alike, but it was never meant to be used by API functions. I also find it strange, for it is only logical if you use LR_LOADFROMFILE that you can use another constant to retreive a bitmap from the resource for example...but I don't think it is possible. Anyways, thanks for your answer to my inquiry, you were absolutelly right.
This is THE solution: The solution to load the resource of bitmap in a StdPicture object is really effective!. I tried to use handle property of the IPictureDisp that returned the LoadResPicture but it failed because he is not handle of bitmap. Nevertheless, if we assigned the IPictureDisp returned by LoadResPicture in a StdPicture object, the StdPicture.handle is perfect bitmap handle!! This is the solution that I implemented : Dim tBitmap As BITMAP Dim iRes As Integer Dim picTemp As New StdPicture ' 102 is the ID of a Bitmap in the .res file. Set picTemp = LoadResPicture(102, vbResBitmap) hImageGray = picTemp.handle If hImageGray <> 0 Then GetObjectAPI hImageGray, Len(tBitmap), tBitmap ' bla bla bla bla..... ........ DeleteObject hImageGray ' I think that this DeleteObject could be cleared since bitmap ' would have to be destroyed when Set picTemp = Nothing, ' but by the doubts that Bill has forgotten to him.... :-) End If Set picTemp = Nothing This works excellent. Mike_spice, when I discovered the solution jumped in a leg. I hope that it serves so much to you as it served my, since in my case, it had a ActiveX and it was already thinking the revolting thing that it would be to distribute bitmap along with the OCX . MrSparc.