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 GdiPlusX

Cropping an image is super simple using GdiPlusX

Prerequisites
Visual FoxPro 9 and the GdiPlusX library from VFPX 

Please make sure that you have the latest version, because this sample may be using some functions that were added or fixed recently.
http://www.codeplex.com/VFPX/Wiki/View.aspx?title=GDIPlusX&referringTitle=Home

 

The cropping is done by the fuction "Clone()" from the Bitmap class. All we need is to pass a Rectangle object containing the X, Y, Width and Height of the desired image to be cropped.

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, lnWidth, lnHeight
lcSource =
GETPICT
()
IF EMPTY
(lcSource)
   RETURN
ENDIF

DO LOCFILE
("System.App")

WITH _SCREEN.System.Drawing
 
* Load Image to GdiplusX
LOCAL loBmp AS
xfcBitmap
loBmp = .
Bitmap
.FromFile(lcSource)
lnWidth = loBmp.
Width
lnHeight = loBmp.Height

* Crop Image
LOCAL loCropped AS xfcBitmap

* Crop Top-Left
LOCAL loRect as xfcRectangle
loRect = .Rectangle.New(0, 0, lnWidth / 2, lnHeight /2)
loCropped = loBmp.Clone(loRect)
loCropped.
Save("c:\Crop-TopLeft.png", .Imaging.ImageFormat.Png)
RUN /N explorer.EXE c:\Crop-TopLeft.png
 
* Crop Bottom-Right
* Now, the Rectangle region will be created inside the Clone function
loCropped = loBmp.Clone(.Rectangle.New(lnWidth / 2, lnHeight /2, lnWidth /2, lnHeight /2))
loCropped.
Save("c:\Crop-BottomRight.png", .Imaging.ImageFormat.Png)
RUN /N explorer.EXE c:\Crop-bottomright.png
 
* Crop Center
loCropped = loBmp.Clone(.Rectangle.New(lnWidth / 4, lnHeight /4, lnWidth /2, lnHeight /2))
loCropped.
Save("c:\Crop-Center.png", .Imaging.ImageFormat.Png)
RUN /N explorer.EXE c:\Crop-Center.png
 
ENDWITH
RETURN
Published Sunday, May 11, 2008 3:38 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

 

Kishore said:

This cropping method can really help me. Thank you for this nice post Cesar!
BTW, how to resize the images ?

Hi,

I'm glad it was helpful to you. At the link below there are some techniques for resizeng. Enjoy !

VFP IMAGING : Resizing images with GdiPlus X

http://weblogs.foxite.com/vfpimaging/archive/2006/12/29/3068.aspx

May 21, 2008 6:45 AM

What do you think?

(required) 
(optional)
(required)