Solved

Clip an image section

Posted on 1998-08-18
12
241 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 250 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now