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
 
 
 
 
 
TitleCopy part of an image onto another part of the image in VB .NET
KeywordsDrawImage, copy picture, PaintPicture, VB.NET
CategoriesGraphics, VB.NET
 
Make Bitmap objects representing the source and destination images. (Note that these Bitmap objects must be distinct.) Create a Graphics object attached to the destination Bitmap. Create two Rectangle objects representing the part of the source image to copy and the destination location. Then use the Graphics object's DrawImage method to copy the picture.

This example then draws a rectangle around the result. Notice how it uses a Rectangle object to define the rectangle and the destination for the copied image. Using the same Rectangle object ensures that the two match.

 
' Copy part of an image onto another part of the image.
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    ' Get the Bitmaps and a Graphics object
    ' for the destination Bitmap.
    Dim fr_bm As New Bitmap(picImage.Image)
    Dim to_bm As New Bitmap(picImage.Image)
    Dim gr As Graphics = Graphics.FromImage(to_bm)

    ' Get source and destination rectangles.
    Dim fr_rect As New Rectangle(240, 20, 130, 100)
    Dim to_rect As New Rectangle(10, 10, 130, 100)

    ' Draw from the source to the destination.
    gr.DrawImage(fr_bm, to_rect, fr_rect, _
        GraphicsUnit.Pixel)
    gr.DrawRectangle(Pens.Red, to_rect)

    ' Display the results.
    picImage.Image = to_bm
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated