hi guys,
if I have a form with, say, a picturebox in it. The picturebox is loaded with an image, and I need to copy just a section of it, say at rectangular points, X, Y, L and W to the clipboard, or to another picturebox. Is it possible in VB6?

8 Years
Discussion Span
Last Post by Rombosia

Yes... but it will require a noxious amount of API calls with GDI. I suppose a great start would be researching GetDC() api call, and then working on learning BitBlt. You could probably get the device context of your picturebox, then use bitblt to get only a certain area of the picture and copy it to a new picturebox (you may want StretchBlt instead of bitblt) and from there, you can use the standard clipboard methods (clipboard.setdata()), or you can get crazy, and use the Clipboard API Calls.



You can also use PaintPicture () to get the portion of image


Option Explicit
Dim iX As Integer, iY As Integer, iHeight As Integer, iWidth As Integer

Private Sub cmdCopy_Click()
   'Set Destination Picture box width and Height
   picDest.Width = iWidth
   picDest.Height = iHeight
   'Copy the Portion of picture from Source to Destination
   picDest.PaintPicture picSource.Picture, _
         0, 0, iWidth, iHeight, _
         iX, iY, iWidth, iHeight
   'Set drawn image to picture
   picDest.Picture = picDest.Image
   'Copy to Clipboard
   Clipboard.SetData picDest.Picture
End Sub

Private Sub Form_Load()
   'Picture Dimension
   iX = 1000   ' in twips
   iY = 1000
   iHeight = 2000
   iWidth = 2000
   picDest.AutoRedraw = True
End Sub
Votes + Comments
Fantastic! Keep up the great work.

selvaganapathy, that did the magic!!! And Comatose, I had dug well into BitBlt. I'm sure it'll help me one of these fine Image days. Thanks guys.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.