Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the LoadImage API function to antialias an image and copy the result into an Image control
Keywordsantialias, anti-alias, anti alias, alias, LoadImage, resize
CategoriesGraphics
 
Luc found a problem with the HowTo Use the LoadImage API function to antialias an image. The code successfully loads an image into a PictureBox but you cannot assign the PictureBox's Picture property to another control's Picture property. I still don't know exactly why this is true but here's a workaround.

Rather than assigning the loaded image directly to the PictureBox, the program loads it into a memory temporary context and then uses BitBlt to copy the result into the PictureBox. Setting pic.Picture = pic.Image at the end makes the picture permanent. For example, if you use pic.Cls after this, the picture remains.

 
' Use LoadImage to load the picture.
Private Sub LoadWithLoadImage(ByVal pic As PictureBox, _
    ByVal file_name As String)
Dim hbm As Long
Dim wid As Long
Dim hgt As Long
Dim temp_dc As Long

    ' Load the image using LoadImage.
    wid = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, _
        vbPixels)
    hgt = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, _
        vbPixels)
    hbm = LoadImage(ByVal 0&, file_name, _
        IMAGE_BITMAP, wid, hgt, &H10)

    ' Make a device context to hold the picture.
    temp_dc = CreateCompatibleDC(0)
    SelectObject temp_dc, hbm

    ' Copy the picture into the PictureBox.
    BitBlt pic.hdc, 0, 0, wid, hgt, temp_dc, 0, 0, SRCCOPY

    ' Delete the DC and bitmap.
    DeleteDC temp_dc
    DeleteObject hbm

    ' Make the image permanent.
    pic.Picture = pic.Image
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated