Trasformations in GDI+

Transforming an object includes Translation, Rotation, Scaling and Shearing. We plan to apply these transformations to our text. We plan to do something like this

In this program we have drawn the shadow first and then the text, so that the text overlaps the shadow making the shadow appear as if it is behind the text. We have drawn the shadow using the same font as the original text and using gray color. The shadow should be half the size of the original text and must be sheared in the x direction.

The code for this program is

protected void Form1_Paint (object sender, System.WinForms.PaintEventArgs e)
{

Graphics g = e.Graphics ;
Font myfont = new Font ( "Times New Roman", 100 ) ;

Matrix mymat = new Matrix ( );
mymat.Shear ( -1.4f, 0f ) ;
mymat.Scale ( 1, 0.5f ) ;
mymat.Translate ( 236, 170 ) ;

g.Transform = mymat ;

SolidBrush mybrush = new SolidBrush ( Color.Gray ) ;
g.DrawString ( "K", myfont, mybrush, 50, 50 ) ;
g.DrawString ( "I", myfont, mybrush, 150, 50 ) ;
g.DrawString ( "C", myfont, mybrush, 200, 50 ) ;
g.DrawString ( "I", myfont, mybrush, 300, 50 ) ;
g.DrawString ( "T", myfont, mybrush, 350, 50 ) ;
g.ResetTransform ( ) ;

mybrush.Color = Color.DarkMagenta ;
g.DrawString ( "K", myfont, mybrush, 50, 50 ) ;

mybrush.Color = Color.FromArgb ( 150, 0, 255, 255 ) ;
g.DrawString ( "I", myfont, mybrush, 150, 50 ) ;

LinearGradientBrush lgb = new LinearGradientBrush ( new
Point ( 200, 50 ), new Point ( 350, 200 ), Color.Brown,
Color.Yellow ) ;
g.DrawString ( "C", myfont, lgb, 200, 50 ) ;

HatchBrush hb = new HatchBrush(
HatchStyle.DiagonalCross, Color.Blue, Color.Red ) ;
g.DrawString ( "I", myfont, hb, 300, 50 ) ;

Image myimg = Image.FromFile ( @"C:\test.bmp" );
TextureBrush tb = new TextureBrush ( myimg ) ;
g.DrawString ( "T", myfont, tb, 350, 50 ) ;

}

We have created an object of the Matrix class. Transformations are always applied using Matrices. Matrix addition and multiplication result in various transformations.

We have applied shearing by �1.4 in x direction using the Shear( ) method. Next we have scaled the matrix by half, using the Scale( ) method. Passing a 0.5 as the second coordinate results in reduction in the y direction by half. After doing all this, the coordinate column of the matrix also gets multiplied by some factor resulting in some different coordinates. These coordinates have to be brought to their actual positions. We did this using the Translate( ) method.

After applying all the transformations to the Matrix, we have set the Transform property of the Graphics class to this matrix. The Transform property of the Graphics class sets or gets the world transform for the Graphics object.

Under such a transformed scenario we have drawn the strings at the specified positions using the specified Brush and Font. Using the DrawString( ) method. This will fully create the shadow part.

The original text drawing is simple; we must get rid of the transformations applied. This is done by resetting the Transformation world by using the ResetTransform( ) method.

To draw a �K� we have used a solid brush with a DarkMegenta Color.To draw the �I� we have used a Transparent Brush. A Brush can be made Transparent by setting the alpha component of its color to a value less than 255 using the FromArgb( ) method.. The alpha value ranges from 0 (fully transparent) to 255(fully opaque). The default is opaque.We have drawn �C� with a GradientBrush, while �I� with a HatchBrush. To draw the �T�, we have used an image in a TextureBrush.

Posted bySumedh at 9:32 PM  

0 comments:

Post a Comment