• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Clip an image section

Have a program that transfers images from a digital camera to a badging system.  The images need to be clipped and resized for best look.
HOW??  Straight VB or 3rd party OCX?  Need a workable  answer fast.  Thanks
0
Fonnie
Asked:
Fonnie
  • 6
  • 6
1 Solution
 
peterwestCommented:
Hi there,

You could quite easily perform the clipping and resizing functions by simply using the BitBlt API call.  This will take an area of a specified image and copy it to another picture control; once here it could be copied to the clipboard or even saved to file.  If you want me to i'll post code illustrating how to use the BitBlt API call.

Pete

0
 
FonnieAuthor Commented:
Pete,
 Yes please.  To add a little more info, picture is currently in a JPG file, I am displaying it in a picture box.  I need to have it saved in a .JPG file.   Haven't dealt with graphics since line drawing characters a long time ago.  Any and all help is appreciated.
  Fonnie

0
 
peterwestCommented:
Ok then,

The BitBlt API call allows you to take a section of an image from one picture box and paste it into another control, usually a picture box, although it can be any control that has a HDC.  This means that bitblt can be used to paste an image onto a form background - quite useful for sprucing up the display.

To use it you need to put the following in a seperate module (.BAS) file:

Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Const SRCCOPY = &HCC0020


The following lines of code give four different examples of using the BitBLt call; in these examples we're copying a quadrant of an image from the source control (picSource) to the destination picturebox (picDestination).  Bear in mind that when working with picture co-ordinates they need converting to twips before being used with the BitBlt call (hence the Screen.TwipsPerPixel bits).

    'Get Top Left Quadrant
    lRetVal = BitBlt(picDestination.hDC, 0, 0, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, picSource.hDC, 0, 0, SRCCOPY)
       
    'Get Top Right Quadrant
    lRetVal = BitBlt(picDestination.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, 0, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, picSource.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, 0, SRCCOPY)
       
    'Get Bottom Left Quadrant
    lRetVal = BitBlt(picDestination.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, picSource.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, SRCCOPY)
   
    'Get Bottom Right Quadrant
    lRetVal = BitBlt(picDestination.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, picSource.hDC, (picSource.Width / Screen.TwipsPerPixelX) / 2, (picSource.Height / Screen.TwipsPerPixelY) / 2, SRCCOPY)


Also, here's a description of the parameters needed by the API call - hopefully it'll give you a good idea how to use the function.  Note that the constants that can be used for dwRop can be found in the VB Constant file; they start with SRC.

hDestDC      Long—Destination device context
x, y      Long—Point describing the upper-left corner of the destination rectangle within the destination DC. This is specified in logical coordinates of the destination DC.
nWidth, nHeight      Long—Width and height of the image being transferred
hSrcDC      Long—Source device context. If the raster operation does not specify a source, this should be 0.
xSrc, ySrc      Long—Point describing the upper-left corner of the source rectangle within the source DC. This is specified in logical coordinates of the source DC.
dwRop      Long—Raster operation to use during the transfer. Refer to “Raster Operations,” earlier in this chapter.

Anyway, hope this helps you - let me know if you wanna know anything else...

Pete

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
peterwestCommented:
Hi again,

Just one small problem i've identified - if you're using the standard VB picturebox then you'll find that when you use the SavePicture command it'll save your original JPG as a BMP file - this is simply a limitation of the control.  The only way you'll get round it is to get hold of a propert image processing/display control.

Pete

0
 
FonnieAuthor Commented:
Working with your info.  Will re-post if I have any problems, but should have some kind of response by tomorrow.
Thanks
Fonnie

0
 
FonnieAuthor Commented:
Got the code running, have one final problem, SavePicture doesn't work.  The destination picture box is not updating the .Picture property after the BitBlt.  Any suggestions?

0
 
peterwestCommented:
Hi again,

You'll probably need to set the Autoredraw property for the destination picture box to be true.  This makes the picturebox store the image as a persistent bitmap and the savepicture method should work ok then - please remember that it will only save as a .BMP file.

Pete

0
 
FonnieAuthor Commented:
picDestination.AutoRedraw is True
After the BitBlt, picDestination.Picture=0, and SavePicture is
erroring.
 
 picSource.Picture=LoadPicture("Something.BMP")
 BitBlt (picSource -> picDestination)  ' Graphic now in both boxes

 Savepicture picDestination,"SomethingElse.BMP"
    ERROR - INVALID PICTURE

What did I do wrong?

0
 
peterwestCommented:
Hmm,

Not sure what's going on there - can you e-mail me the code?? (peterjwest@yahoo.com)

Thanks

Pete

0
 
FonnieAuthor Commented:
Adjusted points to 250
0
 
FonnieAuthor Commented:
Sorry it took so long to close this.  Thanks for all the help

0
 
peterwestCommented:
No problem - just one last idea though - you said you wanted the final image in  JPG format.  Well, if you can find a conversion programme that supports batch mode from the command line interface then you could always shell to that..

Good luck...

Pete

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now