Results 1 to 3 of 3

Thread: Clipping out a piece of a BMP

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    Anyone know how I could clip out a rectangle area of a bitmap and display it in an image control?

    ------------------
    Micah Carrick
    http://micah.carrick.com
    [email protected]
    ICQ: 53480225


  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Here's an example which lets you drag a rectangle around an area in a Picture and it copies that section to a new Picturebox:

    Add 2 Pictureboxes to a Form, load the Picture to Select from in Picture1..
    Code:
    Private oX As Integer
    Private oY As Integer
    Private lX As Integer
    Private lY As Integer
    
    Private Sub Form_Load()
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        Picture2.AutoRedraw = True
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            With Picture1
                .DrawMode = vbInvert
                oX = X
                oY = Y
                lX = oX
                lY = oY
            End With
        End If
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            Picture1.Line (oX, oY)-Step(lX - oX, lY - oY), , B
            Picture1.Line (oX, oY)-Step(X - oX, Y - oY), , B
            lX = X
            lY = Y
        End If
    End Sub
    
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim iTmp As Integer
        Dim iW As Integer
        Dim iH As Integer
        If Button = vbLeftButton Then
            Picture1.Line (oX, oY)-Step(lX - oX, lY - oY), , B
            Picture2.Cls
            If X < oX Then
                iTmp = oX
                oX = X
                X = iTmp
            End If
            If Y < oY Then
                iTmp = oY
                oY = Y
                Y = iTmp
            End If
            If X = oX Then X = 1
            If Y = oY Then Y = 1
            iW = X - oX
            iH = Y - oY
            Picture2.Width = ScaleX(iW, vbPixels, vbTwips)
            Picture2.Height = ScaleY(iH, vbPixels, vbTwips)
            Picture2.PaintPicture Picture1.Image, 0, 0, iW, iH, oX, oY, iW, iH
        End If
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    Thank you ... that'll work perfect.

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