Solved

Clip an image section

Posted on 1998-08-18
12
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

623 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