Solved

Clip an image section

Posted on 1998-08-18
12
236 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:peterwest
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 250
0
 
LVL 1

Author Comment

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

0
 
LVL 2

Expert Comment

by:peterwest
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now