Issue dated - 19th April 2004

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
NEWS ANALYSIS
EVENTS
INDIA TRENDS
PRODUCT
SECURE SPACE
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
Openings At Jobstreet.com
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

The C# column

Transformation in GDI+ - II

In the last article we discussed various forms of transformations. We will now see how to perform these transformations programmatically.

Look at the following Paint event handler.

private void Form1_Paint ( object sender,
PaintEventArgs e )
{
Graphics g = e.Graphics ;
Image myimg = Image.FromFile (
“C:\\palace.jpg” ) ;
Matrix mymat = new Matrix( ) ;
mymat.Scale ( -1, 1 ) ;
mymat.Translate ( -350, 0 ) ;
g.Transform = mymat ;
g.DrawImage ( myimg, 50, 50 ) ;
g.ResetTransform( ) ;
}

This code creates a mirror image. Here we have used a Static method FromFile( ) of the Image class, which returns a reference to the image, which we have collected in myimg. Next, in order to create a transformation matrix we have first created an object of the Matrix class. Since the Matrix class is declared in the System.Drawing.Drawing2D namespace, we must declare this namespace in our program.

We have scaled the matrix by passing –1 as the first parameter to the Scale( ) method. Due to this all the x coordinates would be multiplied by –1. After multiplying this matrix with the coordinates of the image the resultant image would be the mirror image of the original. When we scale the coordinates of an image by multiplying them with –1 the resulting coordinates become negative. Hence they would not be visible as the top left corner of the form is (0, 0). To bring the image back in the form we have to translate it by some x and y values. Next we have assigned this resultant transformation matrix to the Transform property of the Graphics object. There onwards everything that gets drawn would be multiplied with the transformation matrix. For drawing the image we have used the DrawImage( ) method.

Next, we will create an application that would display the string “KICIT” and its shadow on a WinForm. In this program we shall draw the shadow of the text before drawing the text, so that the text overlaps the shadow making the shadow appear as if it is behind the text. We shall draw the shadow using the same font as the original text and shall use gray colour for it. The shadow size would then be required to keep half the size of the original text and also to be sheared in the direction.

private void Form1_Paint ( object sender, PaintEventArgs e )
{
    Graphics g = e.Graphics ;
    Matrix mymat = new Matrix( ) ;
    mymat.Shear ( -1.4f, 0f ) ;
    mymat.Scale ( 1, 0.5f ) ;
    mymat.Translate ( 236, 170 ) ;
    g.Transform = mymat ;
    Font myfont = new Font ( “Times New Roman”, 100 ) ;
    SolidBrush mybrush = new SolidBrush ( Colour.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.Colour = Colour.DarkMagenta ;
    g.DrawString ( “K”, myfont, mybrush, 50, 50 ) ;
    mybrush.Colour = Colour.FromArgb ( 150, 0, 255, 255 ) ;
    g.DrawString ( “I”, myfont, mybrush, 150, 50 ) ;
    LinearGradientBrush lgb = new
    LinearGradientBrush ( new Point ( 200, 50 ),
    new Point ( 350, 200 ),Colour.Brown, Colour.Yellow ) ;
    g.DrawString ( “C”, myfont, lgb, 200, 50 ) ;
    HatchBrush hb = new HatchBrush ( HatchStyle.DiagonalCross, Colour.Blue, Colour.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 ) ;
}

To create a transformation matrix we have firstly created an object of the Matrix class. This object represents an identity matrix of size 3 x 3. We intend to perform shearing, scaling and translation transformations. For this we have called the methods Shear( ), Scale( ) and Translate( ) using the mymat object. These methods manipulate the object by performing matrix multiplications as discussed earlier. At the end of these calls, mymat object represents the transformation matrix.

Next, we have set the Transform property of the Graphics class object g to mymat object. Now whenever a shape is be drawn, the corresponding shape’s coordinate matrix gets multiplied with the transformation matrix stored in mymat. Next we have selected a SolidBrush of gray colour and used the DrawString( ) method of the Graphics class to display the string. We have called ResetTransfrom( ) method that resets the world transformation matrix of this Graphics object to the identity matrix. The identity matrix represents a transformation with no scaling, rotation, or translation.

To display “K” we have used a solid brush of dark magenta colour. To display “I” we have used a transparent brush. A brush can be made transparent by using the FromArgb( ) method and passing an alpha component as its first parameter. The alpha component specifies how opaque or transparent a colour should be. The alpha value ranges from 0 (fully transparent) to 255 (fully opaque). The default value is 255 and hence if we do not pass any value the colour created is always opaque.

In our program we have passed 150 as the first parameter to the FromArgb( ) method. This results in a semi-transparent colour. The string “I” gets drawn with semi-transparent blue colour.

Next we have displayed “C” using a linear gradient brush. Here the y coordinate of the second point passed to the constructor of the LinearGradientBrush class does not match with the y coordinate of the first point and hence the gradient fill is not horizontal.

The “I” is displayed using a hatch brush and lastly to display “T”, we have used a texture brush. To the constructor of the TextureBrush class a reference to an image, which is to be filled, is passed. We have used the Static method FromFile( ) of the Image class to load the image. If you run the program, you will see the string “KICIT” written as shown below.

Yashavant Kanetkar, one of the first Express Computer columnists, is an established software expert, speaker and author with several best-sellers to his credit, including titles like “Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com
<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.