21 de julio de 2006

Escalar y cortar con GDI+

Artículo original: SCALE AND SHEAR WITH GDI+
http://weblogs.foxite.com/vfpimaging/2006/02/07/rotate-flip-images-with-vfp9-and-gdi-
Autor: Cesar Ch.
Traducido por: Ana María Bisbé York


Ejecute el código que aparece debajo para cambiar el corte y la escala de una imagen.

Muchas gracias a Anatolyi Mogylevets, de www.news2news.com.

Una gran parte de este código es suyo, aunque como está incompleta _gdiplus.vcx, nos obliga a llamar directamente al API http://www.news2news.com/vfp/?example=479.

Para entender mejor como trabaja con GDI+, mire también estos enlaces:
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+
* --------------------------
* Muchas gracias a Anatolyi Mogylevets, de www.news2news.com
* Gran parte de este código es suyo, aunque como _gdiplus.vcx 
* está incompleta,nos obliga a llamar directamente a través de 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")

*** Ahora vamos a crear una imagen nueva con 
*** Create Method - Crea un objeto bitmap. 
*** Sintaxis: ? THIS.Create(tnWidth, tnHeight[, tnPixelFormat])
*** tnPixelFormat, opcional, una de las constantes GDIPLUS_PIXELFORMAT_*, 
*** predeterminadas para 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)
* crea un objeto Matrix 
* y aplica las transformaciones de la escala y corte 
= 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")

No hay comentarios. :

Publicar un comentario