Microsoft KB Archive/242018

= How To Copy a Section of a Bitmap to the Clipboard =

Article ID: 242018

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q242018



SUMMARY
This article shows how to select a region of a bitmap from a picture box and then copy only the selected region to the clipboard. This article assumes that you select from the top-left to the bottom-right with the mouse.



Step by Step Example
 Start a new Visual Basic Standard Exe project. Form1 is created by default. On the Project menu, select Add Module to add a new module to the existing project. Add two Picture Boxes to the form, name one Pic_Edit (the target), and name the other Pic_Dest (the destination.) Set the Picture property of Pic_Edit to a bitmap from which you want to select a region. Set the AutoRedraw property of Pic_Dest to True.  Add the following code to Module1: Public Const INVERSE = 6 Public Const DOT = 2 Public Const SOLID = 0

Public OrigX As Long Public OrigY As Long Public DestX As Long Public DestY As Long

Public Sub Draw_Selection_Rectangle ' Set drawing mode to INVERSE since this routine also used to erase ' the selection rectangle by simply drawing over the currently ' displayed rectangle With Editor.Pic_Edit .DrawMode = INVERSE .DrawStyle = DOT Editor.Pic_Edit.Line (OrigX, OrigY)-(DestX, DestY),, B       .DrawStyle = SOLID End With

End Sub

Public Sub Copy_Rectangle With Editor.Pic_Dest .Cls .Visible = True .Height = DestY - OrigY .Width = DestX - OrigX .PaintPicture Editor.Pic_Edit, 0, 0, (DestX - OrigX), _ (DestY - OrigY), OrigX, OrigY, (DestX - OrigX), _ (DestY - OrigY), vbSrcCopy End With ' Make sure the clipboard is clear, then copy the image: Clipboard.Clear Clipboard.SetData Editor.Pic_Dest.Image End Sub

  Add the following code to Form1: Private Sub Pic_Edit_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Pic_Edit.Refresh Pic_Dest.Visible = False OrigX = X   OrigY = Y    DestX = OrigX DestY = OrigY Call Module1.Draw_Selection_Rectangle End Sub

Private Sub Pic_Edit_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then DestX = X       DestY = Y        Pic_Edit.Refresh Call Module1.Draw_Selection_Rectangle End If End Sub

Private Sub Pic_Edit_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Check to see if mouse moved or goes the "wrong" way: If DestX <= OrigX Or DestY <= OrigY Then Pic_Edit.Refresh Exit Sub End If   If Button = 1 Then Call Copy_Rectangle End Sub

</li> Start the application and select an area of the bitmap with the mouse. When you release the mouse button, Pic_Dest appears with the selected region.NOTE: If you open up MS Paint, MS Word, or any other application that can take a pasted bitmap image, you are able to paste the selected portion of the image to that application. You can also view the contents of the clipboard by using the Clipboard Viewer.</li></ol>

<div class="references_section">