Results 1 to 7 of 7

Thread: LoadPicture Replacement with GDI+

  1. #1

    Thread Starter
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    LoadPicture Replacement with GDI+

    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.
    Attached Files Attached Files

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: LoadPicture Replacement with GDI+

    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

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: LoadPicture Replacement with GDI+

    Nice code, but it doesn't work with transparency.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: LoadPicture Replacement with GDI+

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: LoadPicture Replacement with GDI+

    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:
    1. Public Type BLENDFUNCTION
    2.     BlendOp As Byte
    3.     BlendFlags As Byte
    4.     SourceConstantAlpha As Byte
    5.     AlphaFormat As Byte
    6. End Type
    7.  
    8. 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
    9. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    10.  
    11. 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)
    12. Dim tmpHDC As Long, hBitmap As Long, bitmap As Long
    13. Dim Blend As BLENDFUNCTION, BlendLng As Long
    14.  
    15. Blend.AlphaFormat = 1
    16. Blend.SourceConstantAlpha = 255
    17. CopyMemory BlendLng, Blend, 4
    18.    
    19. AlphaBlend dhDC, dx, dy, dW, dH, shDC, Sx, Sy, sW, sH, BlendLng
    20. End Sub
    21. 'assuming a pic1 picturebox with scalemode to 3 and autosize to true
    22. Pic1.Picture = LoadPictureGDIPlus("C:\some32bpp.png", , , , , , True)
    23. AlphaBlt Form1.hdc, 0, 0, Pic1.ScaleWidth, Pic1.ScaleHeight, Pic1.hdc, 0, 0, Pic1.ScaleWidth, Pic1.ScaleHeight, , True

    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.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: LoadPicture Replacement with GDI+

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: LoadPicture Replacement with GDI+

    Indeed, thank you, LaVolpe.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width