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





 

* 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 Win32Api * 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