What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse the LoadImage API function to antialias an image and save the result into a file
DescriptionThis example shows how to use the LoadImage API function to antialias an image and save the result into a file in Visual Basic 6. When you use LoadImage to load a picture into a PictureBox, you cannot use the PictureBox's Picture property to save or copy the image. This example shows how to save the picture in a memory bitmap and then use BitBlt to copy it into the PictureBox.
Keywordsantialias, anti-alias, anti alias, alias, LoadImage, resize, LoadPicture, SavePicture
CategoriesGraphics, API
If you use the LoadIMage API function to load an image into a PictureBox and then you pass the control's Picture property to SavePicture, you get a picture containing the PictureBox's background. I don't know 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 temporary memory 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, _
    hgt = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, _
    hbm = LoadImage(ByVal 0&, file_name, _

    ' 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

Private Sub Form_Load()
Dim file_name As String

    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"

    ' Load using LoadPicture.
    imgUnaliased.Picture = LoadPicture(file_name & _

    ' Load using LoadWithLoadImage.
    LoadWithLoadImage picAliased, file_name & _

    ' Copy the picture to an Image control.
    imgCopy.Picture = picAliased.Picture

    ' Save the picture into a file.
    SavePicture picAliased.Picture, file_name & _
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.