2006-05-25

CROP IMAGES WITH GDI+

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.

 

1000.107.1582.Ronaldinho

Original image


 

 

1000.107.1586.Crop-TopLeft 1000.107.1584.Crop-Center 1000.107.1583.Crop-BottomRight

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 GDI+
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.

3 comments:

  1. 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

    ReplyDelete
  2. Versión en Español de este artículo en / Spanish version at http://www.portalfox.com/article.php?sid=2223    

    ReplyDelete
  3. 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

    ReplyDelete