this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, May 24, 2010

Graphics with .net (Part -1)

As a developer, we passed through requirement when we want some customize user interface. We want to draw various shapes, various charts, various diagram to output.

Thanks to System.Drawing namespace, we can play with output design. Graphics includes drawing various shapes circle, rectangle, triangle, ellipse etc, also fill shapes with different colors, modify image as per need. Font can be changed, various charts can be constructed.

System.Drawing includes classes Bitmap, Brush, Brushes, ColorCoverter, ColorTranslator, Font, FontConverter, Graphics, Icon, Image, Pen, Pens, Region, SolidBrush, StringFormat, SystemBrushes, SystemColors and many other classes which can be used at various task of graphics drawing.

Apart from classes System.Drawing namespace have structures CharacterChange, Color, Point, PointF, Rectangle, RectangleF, Size, SizeF. Here F in structure name specifies floating point value support.

Location of control: with help of Point we can set location of control

btnOkay.Location = new Point(50,50) will set btnOkay at (50,50) point.

Set Color, with Color structure
btnOkay.ForeColor = Color.Red;
bnkOkay.BackColor = Color.Blue;

We can specify color from another method FromArgb and passing value of RGB.
btnOkay.ForeColor = Color.FromArgb(10,200,200);
bnkOkay.BackColor = Color. FromArgb(5,20,50);

Draw on surface of form or control.

To draw shapes first we need to create object of Graphics, create object of pen, and now call various method of Graphics to draw shape with pen created earlier.

Graphics g = this.CreateGraphics();

Pen p = new Pen(Color.Blue, 5);


Above code will draw a blue line with 5 pixel thick from point (10,10) to (100,100)

Pen’s style can be set with DashStyle enumeration.

p.DashStyle = DashStyle.Dot;

p.DashStyle = DashStyle.Dash;

p.DashStyle = DashStyle.DashDot;

p.DashStyle = DashStyle.Solid;

By default, pen draw with solid style. Pen.DashOffset and Pen.DashPattern used to customize dash pattern.

To control endcaps, create arrows or modify pen start cap and end cap, LineCap enumerations is helpful.

p.StartCap = LineCap.ArrowAnchor;
p.EndCap = LineCap.DiamondAnchor;

p.StartCap = LineCap.Flat;
p.EndCap = LineCap.Round;

Methods of Graphics which start with Draw needs to fill it with different brushes, Graphics also support fill methods, which does not create shape but also fill shape.

To fill shape, Brush is used. Brush class is abstract class so any of their child class’s instances can be initiated.

System.Drawing.Drawing2D.HatchBrush: A rectangular brush with a hatchstyle, a foreground color, and a background color

System.Drawing.Drawing2D.LinearGradientBrush: Encapsulates a brush with a lin-
ear gradient that provides a visually appealing, professional-looking fill

System.Drawing.Drawing2D.PathGradientBrush: Provides similar functionality to
LinearGradientBrush; however, you can define a complex fill pattern that fades
between multiple points

System.Drawing.SolidBrush: A brush of a single color

System.Drawing.TextureBrush: A brush made from an image that can be tiled across a shape, like a wallpaper design

No comments: