I need to use the LoadImage pinvoke in a project. I need to use this so that I get a transparent BMP.The following code works fine, but requires a path.
Dim hbitmap As IntPtr = NativeMethodsEX.LoadImage(Nothing, "C:\Users\username\Desktop\mickey32.bmp", NativeMethodsEX.IMAGE_BITMAP, 0, 0, NativeMethodsEX.LR_LOADFROMFILE Or NativeMethodsEX.LR_CREATEDIBSECTION)
My question is can I do the same thing but with a file that is embedded in my project? Or is the only way that at some stage I copy the file out into the file system for it to be used in the LoadImage method.
Edit: what I'm trying to avoid is to have an image file in the file system, which for example could be deleted by a user.
EDIT - What am i doing? I am inserting a menu item into an existing windows hmenu as I indicate in this question of mine.. I want to add an image with a transparent background.
EDIT - Why am I not using .net to get my image?
It is well documents in this forum that the following does not work. Further I cannot get it to work.
Dim bm As Bitmap = My.Resources.mickey32.bmp
Dim hbitmap As IntPtr = nbm.GetHbitmap
After the comments below in this question as to why I am not using .NET I have even tried the following suggestions which included a format.. Still no luck.
Dim bm As Bitmap = My.Resources.mickey32.bmp
Dim bm1 As Bitmap = New Bitmap(bm)
Dim nbm As Bitmap = bm1.Clone(New Rectangle(0, 0, bm1.Width, bm1.Height), Imaging.PixelFormat.Format32bppArgb)
Dim hbitmap As IntPtr = nbm.GetHbitmap
Edit Using a PNG
This code which uses a PNG file. It loads fine but the transparent background is made white.
Dim bm As Bitmap = My.Resources.mickey32.png
Dim hbitmap As IntPtr = bm.GetHbitmap
My Solution or what I did in the end
My main issue was to get an image that had a transparent background. I thought the only way to do this was to use the LoadImage API with a bitmap image. Thanks to the extensive comments below I revisited trying this with the .net methods and a png file (I incorrectly thought this did not work). I then found this forum question. The solution there is to use this;
hbitmap = bm.GetHbitmap(Color.Black)
And now I have it displayed correctly.
In my searching I also found this forum answer as well. The accepted answer says;
GDI+ has a number of problems related to alpha blending when doing interop with GDI (and Win32). In this case, the call to bmp.GetHbitmap() will blend your image with a black background.
I did not test the code but it at least looks like it might also be a solution for me.