2006-02-09

SCALE AND SHEAR WITH GDI+

Execute the code below to change shear and scale of an image.

Special thanks to anatolyi mogylevets, from www.news2news.com
A great part of this code comes from him, once _GdiPlus.vcx is incomplete, what obliges us to call directly through API http://www.news2news.com/vfp/?example=479

 

To better understand how scale and shear work with GDI+, take a look at these links as well:
http://www.vbaccelerator.com/home/vb/code/vbmedia/using_gdi_plus/scale__rotate__skew_and_transform_images/article.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/gdiplus/gdiplusreference/classes/matrixclass/matrixmethods/shear.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/gdiplus/gdiplusreference/classes/matrixclass/matrixmethods/shear.asp

 

1000.107.1231.Noronha2 1000.107.1232.Sheared_NORONHA2
Original Image After applying the Scale and Shear effect

 

* Scale and Shear with GDI+
* --------------------------
* special thanks to anatolyi mogylevets, from
www.news2news.com
* a great part of this code comes from him, once _gdiplus.vcx
* is still incomplete what obliges us to call directly through api
*
http://www.news2news.com/vfp/?example=479

declare integer gdipcreatematrix in gdiplus integer @matrix
declare integer gdipdeletematrix in gdiplus integer matrix
declare integer gdipshearmatrix in gdiplus;
        integer matrix, single shearx, single sheary, integer ord
declare integer gdipscalematrix in gdiplus;�
        integer matrix, single scalex, single scaley, integer ord
declare integer gdipsetworldtransform in gdiplus;�
        integer graphics, integer matrix

lcsource = getpict("jpg;gif;bmp")
lcdestination = addbs(justpath(lcsource))+ "sheared_" +;
   juststem(lcsource)+".jpg"
local loimage as gpimage of _gdiplus.vcx
loimage = newobject("gpimage", home() + "ffc/_gdiplus.vcx")
loimage.createfromfile(lcsource)

local lobitmap as gpbitmap of _gdiplus.vcx
lobitmap = newobject("gpbitmap", home() + "ffc/_gdiplus.vcx")
local lographics as gpgraphics of _gdiplus.vcx
lographics = newobject('gpgraphics',home() + "ffc/_gdiplus.vcx")

*** now we create a new image with
*** create method - creates a bitmap object.
*** syntax: ? this.create(tnwidth, tnheight[, tnpixelformat])
*** tnpixelformat, optional, one of gdiplus_pixelformat_* constants,
*** defaults to gdiplus_pixelformat_32bppargb.

local lnwidth, lnheight, lnpixelformat
lnwidth = loimage.imagewidth
lnheight = loimage.imagewidth
lnpixelformat = loimage.pixelformat

local matrix1, xscalefactor, yscalefactor, xshearfactor, yshearfactor
xscalefactor = 1.30
yscalefactor = 0.75

xshearfactor = 0.20
yshearfactor = 0.10

store 0 to matrix1�

lnnewwidth  = lnwidth  * xscalefactor * (1 + xshearfactor)
lnnewheight = lnheight * yscalefactor * (1 + yshearfactor)

lobitmap.create(lnnewwidth, lnnewheight, lnpixelformat)
lographics.createfromimage(lobitmap)

* create matrix object
* and apply scale and shear transformations
= gdipcreatematrix(@matrix1)
= gdipscalematrix(matrix1, xscalefactor, yscalefactor, 0)
= gdipshearmatrix(matrix1, xshearfactor, yshearfactor, 0)
= gdipsetworldtransform(lographics.gethandle(), matrix1)
lographics.drawimageat(loimage, 0, 0)
lographics.resettransform()
= gdipdeletematrix(matrix1)

lobitmap.savetofile(lcdestination, "image/jpeg")

3 comments:

  1. Wow! I tried to do the same using GdipDrawImagePoints but with no luck.

    Your implementation works very nicely.


    Thank you!

    Vassilis

    ReplyDelete
  2. Glad to know it helped you !

    Cesar

    ReplyDelete
  3. I am always excited to visit this blog in the evenings.Please churning hold the contents. It is very entertaining.

    ReplyDelete