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
 
 
 
 
 
 
TitleDraw text on a picture and save the results into a file
Keywordstext, picture, SavePicture
CategoriesGraphics
 
The cmdSaveWithoutText_Click event handler uses LoadPicture to load a picture. It then draws text on it and uses SavePicture to save the results into a file. Many programmers get confused here because the saved picture doesn't contain the text.

When you draw on a PictureBox, the control's Image property contains the image the PictureBox uses to redisplay itself (for example, if it gets covered by another window and then exposed). The Picture property stores the PictureBox's permanent value. After drawing text, lines, or other stuff on the picture, the program can make it permanent by setting the Picture property equal to the Image property.

The cmdSaveWithText_Click event handler loads the picture, draws text on it, sets the Picture property equal to the Image property, and saves the results to a file. The PictureBox looks the same in both cases but this time the text is saved in the file.

 
' Load a picture, draw text on it, and save the result.
Private Sub cmdSaveWithoutText_Click()
    ' Load the picture and draw text on it.
    DrawText

    ' Save the picture.
    SavePicture picCats.Picture, m_FilePath & _
        "cats_without_text.bmp"

    MsgBox "Ok"
End Sub

' Load a picture, draw text on it, and save the results
' including the text into a file.
Private Sub cmdSaveWithText_Click()
    ' Load the picture and draw text on it.
    DrawText

    ' Make the current image with the text
    ' a permanent part of the image.
    picCats.Picture = picCats.Image

    ' Save the picture.
    SavePicture picCats.Picture, m_FilePath & _
        "cats_with_text.bmp"

    ' Alternatively you can use save picCats.Image.
    ' Then you don't need the statement
    '     picCats.Picture = picCats.Image

    MsgBox "Ok"
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated