Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



CROP IMAGES WITH GDI+

This one goes to Malcolm Greene...  Big Smile [:D]

Cropping an image is a simple task for Gdiplus.

One of the ways that this can be done is using the Gdi+ function GdipCloneBitmapAreaI , that unfortunately was not included in _gdiplus.vcx, but can be easily accessed. 

Original Image 

Top Left  

                                Center 

                                                   Bottom Right 

Run the code below, selecting any image, and you will see the image cropped in three ways: the top-left part of the image, the bottom-right part, and the center.

LOCAL lcSource
lcSource = GETPICT()

IF EMPTY(lcSource)
   RETURN
ENDIF
* Load Image to Gdiplus
LOCAL loImage AS GpImage OF HOME() + ffc/_gdiplus.vcx
loImage = NEWOBJECT("GpImage",HOME()+"ffc/_gdiplus.vcx")
loImage.CreateFromFile(lcSource)
lnWidth = loImage.ImageWidth
lnHeight = loImage.ImageHeight

* Crop Image
LOCAL loCropped AS GpBitmap OF HOME() + ffc/_gdiplus.vcx

* Crop TopLeft
loCropped = Crop(loImage, 0, 0, lnWidth / 2, lnHeight /2)
loCropped.SavetoFile("Crop-TopLeft.png","image/png")
RUN /N explorer.EXE Crop-TopLeft.png

* Crop BottomRight
loCropped = Crop(loImage, lnWidth / 2, lnHeight /2, lnWidth /2, lnHeight /2)
loCropped.SavetoFile("Crop-BottomRight.png","image/png")
RUN /N explorer.EXE Crop-bottomright.png

* CropCenter
loCropped = Crop(loImage, lnWidth / 4, lnHeight /4, lnWidth /2, lnHeight /2)
loCropped.SavetoFile("Crop-Center.png","image/png")
RUN /N explorer.EXE Crop-CENTER.png

RETURN


PROCEDURE Crop(toImage, x, Y, tnWidth, tnHeight, tnPixelFormat)
   IF VARTYPE(tnPixelFormat) = "L"
      tnPixelFormat = toImage.PixelFormat
   ENDIF
   * GpStatus WINGDIPAPI GdipCloneBitmapAreaI
   * (INT x, INT y, INT width, INT height, PixelFormat format,
   * GpBitmap *srcBitmap, GpBitmap **dstBitmap)
   DECLARE LONG GdipCloneBitmapAreaI IN GDIPLUS.dll ;
      LONG x, LONG Y, LONG nWidth, LONG nHeight, ;
      LONG PixelFormat, LONG srcBitmap, LONG @dstBitmap
   LOCAL lnNewBitmap
   lnNewBitmap = 0
   = GdipCloneBitmapAreaI(x, Y, tnWidth, tnHeight, ;
      tnPixelFormat, toImage.GetHandle(), @lnNewBitmap)
   LOCAL loNewImage
   loNewImage = NEWOBJECT("GpBitmap",HOME()+"ffc/_gdiplus.vcx")
   loNewImage.SetHandle(lnNewBitmap)
   RETURN loNewImage
ENDPROC

To achieve this, I created a small procedure, "crop", that receives the following parameters retrieved from MSDN :

toImage - Image or Bitmap object

X  - Integer that specifies the x-coordinate of the upper-left corner of the rectangle that specifies the portion of this bitmap to copy.

Y  - Integer that specifies the y-coordinate of the upper-left corner of the rectangle that specifies the portion of this bitmap to copy.

Width - Integer that specifies the width of the rectangle that specifies the portion of this bitmap to copy.

Height - Integer that specifies the height of the rectangle that specifies the portion of this image to copy.

Format - optional, Integer that specifies the pixel format of the new bitmap. The PixelFormat data type and constants that represent various pixel formats are defined in Gdiplus.h. For more information about pixel format constants, see Image Pixel Format Constants.

Return - This method returns a pointer to the new Bitmap object containing the cropped image.

Published Thursday, May 25, 2006 5:59 PM by cesarchalom
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Malcolm Greene said:

Cesar!

Imagine my surprise when logged into Foxite tonight! :)

As you know, I've often wondered how cropping could be done in VFP.

Your Crop() function makes it a cake walk!

Thank you!!!

Malcolm
May 26, 2006 12:22 AM
 

Ana María Bisbé said:

Versión en Español de este artículo en / Spanish version at http://www.portalfox.com/article.php?sid=2223    
July 24, 2007 6:50 PM
 

Kishore Kumar said:

Wonderful!

I am using third party tool to do this image cropping.
Now it is possible to do this in VFP itself!

Good work Cesar, and thank you very much for sharing your work !!

Kishore
September 12, 2007 10:20 AM
 

foxpro.catalyst ?? » Blog Archive » Cesar Chalom: Crop images with GDI+ said:

October 8, 2007 9:48 PM

What do you think?

(required) 
(optional)
(required)