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?

Dani AI

Generated

Nice question, . Both paths suggested are valid: ’s PaintPicture is the simplest way to crop, while ’s GDI route (BitBlt/StretchBlt) gives you more control when scaling or targeting the Windows clipboard directly.

Two quick things that trip folks up in VB6:

  • Units: controls default to Twips. If you are thinking in pixels, set both source and destination PictureBox ScaleMode = vbPixels (or convert Twips to pixels with ScaleX/ScaleY) so X, Y, W, H align with image pixels.
  • Persistence: set the destination AutoRedraw = True, draw, then assign Picture = Image before sending to the clipboard.

If you prefer to keep Twips in your UI but pass pixel-accurate rectangles to PaintPicture, convert like this and reuse your existing copy code:

' Convert a Twips-based rectangle to pixels before cropping
Dim px As Long, py As Long, pw As Long, ph As Long
px = ScaleX(twipsX, vbTwips, vbPixels)
py = ScaleY(twipsY, vbTwips, vbPixels)
pw = ScaleX(twipsW, vbTwips, vbPixels)
ph = ScaleY(twipsH, vbTwips, vbPixels)
' Now plug px,py,pw,ph into your PaintPicture call and destination sizing

Clipboard tip: be explicit about the format for best compatibility with other apps.

Clipboard.Clear
Clipboard.SetData picDest.Image, vbCFBitmap

Going the API route that mentioned is great when you want to scale with better quality or bypass an intermediate PictureBox. Use BitBlt for a 1:1 copy, or StretchBlt for resizing; if you do use StretchBlt, set the stretch mode to HALFTONE for nicer results. Also, if you place a bitmap on the clipboard via SetClipboardData(CF_BITMAP, hBmp), do not delete that HBITMAP afterward; the clipboard owns it.

Recommended Answers

All 3 Replies

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.

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
commented: Fantastic! Keep up the great work. +10

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.