954,559 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to copy a section of an image to the clipboard

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?

Rombosia
Newbie Poster
21 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

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 .

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Hi,

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

Code

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
selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

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.

Rombosia
Newbie Poster
21 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You