Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Clip an image section

Posted on 1998-08-18
12
Medium Priority
?
274 Views
Last Modified: 2010-04-30
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
Comment
Question by:Fonnie
  • 6
  • 6
12 Comments
 
LVL 2

Expert Comment

by:peterwest
ID: 1429739
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
 
LVL 1

Author Comment

by:Fonnie
ID: 1429740
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
 
LVL 2

Accepted Solution

by:
peterwest earned 1000 total points
ID: 1429741
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:peterwest
ID: 1429742
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
 
LVL 1

Author Comment

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

0
 
LVL 1

Author Comment

by:Fonnie
ID: 1429744
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
 
LVL 2

Expert Comment

by:peterwest
ID: 1429745
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
 
LVL 1

Author Comment

by:Fonnie
ID: 1429746
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
 
LVL 2

Expert Comment

by:peterwest
ID: 1429747
Hmm,

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

Thanks

Pete

0
 
LVL 1

Author Comment

by:Fonnie
ID: 1429748
Adjusted points to 250
0
 
LVL 1

Author Comment

by:Fonnie
ID: 1429749
Sorry it took so long to close this.  Thanks for all the help

0
 
LVL 2

Expert Comment

by:peterwest
ID: 1429750
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question