A nearly() identical replacement to LoadPicture, but using GDI+ instead.
Important points: takes a filename, (dimensions), [(backcolor), (usealpha)] and returns a Picture(IPicture) object! Oh, it resizes with super-high-quality(bicubic).
I assume you'll be setting these into .Picture of objects.... Which will likely handle their destruction. Otherwise you should set them equal to nothing when you're done with them.
Enjoy.
Updated project with example code from my additional post.
WARNING: Known LIMITATIONS with ICO, CUR, EMF/WMF, and image control transparency.
Last edited by FireXtol; Jun 15th, 2010 at 06:02 PM.
Reason: NEW ATTACHMENT
If it is just a form and module wouldn't be better to post the code directly on the forum instead.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
FireXTol, some limitations you should be aware of:
1. Cannot handle any transparency (bitmaps, icons, wmf, gif, etc, etc). Try loading a transparent gif, icon, wmf, and/or png with transparency. However, supplying the optional backcolor parameter to your function does fake transparency. A good solution but not ideal especially if used in an image control that overlays gradient background or multi-color background or overlapping image controls.
2. With VB's LoadPicture, icons & wmf/emf original format is maintained, your function loses that information because the stdPicture object is loading a bitmap as all formats are converted to bitmap. In other words, .SavePicture will always produce a bitmap format, regardless which format was originally loaded.
3. GdipLoadImageFromFile does not load 32bpp alpha-bitmaps correctly, has issues loading many icons, especially 24 & 32 bit depths, and cannot load Vista icons with PNGs embedded into them; also .cur files don't seem to be supported. Try it.
4. Dimensions for WMF/EMF are incorrect-ish. Try loading a wmf with your function and with just VB's LoadPicture; scaling is required.
I can go on and list a few more known issues with GDI+, but I won't. Simply, GdipLoadImageFromFile is not the fix-all; it has so many holes in it.
Last edited by LaVolpe; Jun 14th, 2010 at 11:36 AM.
Insomnia is just a byproduct of, "It can't be done"
I can't get it to load cursors or icons. But it does work great for PNG, TIF, JPEG, GIF, and BMP.
1. That's kind of what UseAlpha is for(the alpha channel of the 'bitmap' will be preserved). Though I imagine you mean for controls, like the Image control? Yea, that's not supported. But it's possible manually:
vb Code:
Public Type BLENDFUNCTION
BlendOp As Byte
BlendFlags As Byte
SourceConstantAlpha As Byte
AlphaFormat As Byte
End Type
Public Declare Function AlphaBlend Lib "msimg32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal widthSrc As Long, ByVal heightSrc As Long, ByVal blendFunct As Long) As Boolean
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Sub AlphaBlt(ByVal dhDC As Long, ByVal dx As Long, ByVal dy As Long, ByVal dW As Long, ByVal dH As Long, ByVal shDC As Long, ByVal Sx As Long, ByVal Sy As Long, ByVal sW As Long, ByVal sH As Long)
Dim tmpHDC As Long, hBitmap As Long, bitmap As Long
To blt the loaded image with full 32 bit(including alpha). No pre-multiplication required(but requires 32 bit screen resolution, afaik).
2. I would suggest a GDI+ equivalent to SavePicture.
3. Actual BMPs? Yea, that's a problem beyond my control. I suggest PNG, or TIF for alpha channel support. 32 bpp bitmaps are quite rare, and practically unheard of(except in my video card's RAM).
4. Having never actually used a WMF/EMF.... I'd consider this a minor bug. I also do not have any, afaik.
Indeed, it's not a fix all, but it does offer a wider range of supported file formats, and I believe the advancements(alpha channels, bicubic resizing, modern formats(especially PNG)) make up for the shortcomings. Of course it certainly depends what you're trying to accomplish.
Last edited by FireXtol; Jun 24th, 2010 at 09:24 PM.
With my previous reply, I just wanted to point out some of GDI+ known issues when loading images.
Here are couple more tidbits.
1. When one renders a 32bpp image (or one with transparency) to a 32bpp DIB using GDI+, the DIB is already premultiplied by GDI+ during the render action. Original color values are permanently lost in the DIB, not the GDI+ handle, and is generally not an issue for most, but a "did you know" topic.
2. Regarding your useAlpha flag and VB's stdPicture object. Yes 32bit screen resolution is required. VB's stdPicture objects created with OleCreatePictureIndirect are based on screen resolution. So this can have adverse results on lower res settings. Try dropping your screen res to 16bit or lower & AlphaBlt won't work any longer.
Bottom line, VB's stdPicture object is just not suited for true 32bpp graphics. A custom usercontrol would be far more ideal IMO. As you are finding, in order to render transparency, you must fake it with a pre-defined backcolor or pass responsibility off to other functions; i.e., AlphaBlt.
Good idea, but with all workarounds, I think there should be warnings and limitations identified.
Regarding the shortcomings of icons, cursors, wmf, emf formats. I wouldn't call those minor issues if touted as a LoadPicture replacement. I can see this as a LoadPicture supplement (limitations understood and noted), but not replacement, as is.
Last edited by LaVolpe; Aug 10th, 2010 at 08:03 AM.
Reason: added italicized portion
Insomnia is just a byproduct of, "It can't be done"
This was originally conceived for AlphaBlend/AlphaBlt-based games. I did say "nearly () identical replacement"....
I certainly realized it's not a 100% compatible replacement, and I'm glad you have shared your knowledge of the incompatibilities, as I wasn't aware of most of them(I've never even tried it with an Image control before this! I'd probably of done it with event Paint). Good stuff to know.