Posts filed under ‘Graphics’

Graphics with .net (Part -3)

Till now we got idea of how to create image and edit existing image. Now we are moving to add text data to our existing image or creating image where text is to be inserted as copyright logo or company’s initial.

To add text to any image, following things are required.

àObject of Graphics

àObject of Font

àIf require Object of Brush

àCall Graphics.DrawString method and pass font and brush object.

Yes its very simple to add text to our image. Now let’s check out how to create Font object and how to write in image

àCreate object of Font

Font f = new Font(“Arial”,10,FontStyle.Bold);

Apart from above constructor Font class has more than 12 constructor, we can create FontFamily object and pass it to Font Constructor.

FontFamily ffm = new FontFamily(“Arial”);

Font f = new Font(ffm,10);

If we need to read type of font, we can do with help of FontConverter class. But their errors prone, as we don’t get any error at compile time. We don’t come to know any error till it throws Runtime exception.

FontConverter converter = new FontConverter();

Font f = (Font)converter.ConvertFromString(“Arial, 12pt”);

àWrite text To Image

Once font is created, we need to create object of brush to fill text with that color, or we can use Brushes if we don’t wish to create object of brush.

Graphics g = this.CreateGraphics();

Font f = new Font(“Arial”, 40, FontStyle.Bold);

g.DrawString(“My Test String!”, f, Brushes.Blue, 10, 10);

Above code will draw “My Test String!” in form with Blue color and bold style with Arial font family.

With help of StringFormat we can format string like its alignment, FormatFlags, LineAlignment, Traimming etc.

Alternatively, to add text to image saved in disk, load it by using any of the bitmap or image class then follow step to draw string on it.

After that call save method of image or bitmap class, that will store new modified image in disk.

May 26, 2010 at 8:50 am Leave a comment

Graphics with .net (Part -2)

As we discussed in previous post, we required to modify existing image file or have to create new image, chart, edit image and back save to file.

Image and Bitmap class provides ability to modify existing image file or create new image file.

Image is an abstract class, but we can have instance of it by Image.FromFile or Image.FromStream methods. FromFile accept any image file, FromStream accepts stream of Image file. We can use whatsoever we have at hand when we are going to play with Image.

Bitmap class is inherited by Image, For still image we can use Bitmap class, For animated image we can use System.Drawing.Imaging.MetaFile. Bitmap class is widely used for image editing and creation compared to MetaFile.

àDisplay Image in background.

To display image in the back ground of form or any control we have one or more techniques, we can choose any of them.

Take a picturebox control on the form, or create instance of picture box and set BackGroundImage property.

Image backImage = Image.FromFile(“@myImage.jpg”);

myPictureBox.BackGroundImage = backImage;

Here we have placed picturebox control and set its name myPictureBox. myImage.jpg is file available in our current directory else we can specify whole path of my image file.

In another method, we can set background image using Graphics.DrawImage method.

Bitmap backImage = new Bitmap(“@myImage.jpg”);

Graphics g = this.CreateGraphics();

g.DrawImage(backImage,1,1,this.Width,this.Height);

DrawImage methods has 30 around overloaded method, we can use any of them.

àCreate and Save Image

To create or edit any image first create object of Bitmap class, edit it using Graphics then with Bitmap.Save method, save it back to disk.

Bitmap bm = new Bitmap(600, 600);

Graphics g = Graphics.FromImage(bm);

Brush b = new LinearGradientBrush(new Point(1, 1), new Point(600, 600),     Color.White, Color.Blue);

Point[] points = new Point[]  {new Point(10, 10),  new Point(77, 500), new Point(590, 100),  new Point(250, 590),   new Point(300, 410)};

g.FillPolygon(b, points);

bm.Save(“bm.jpg”, ImageFormat.Jpeg);

In above code we have created a polygon and filled it with Blue-while Gradient Brush. Once it done, we save it to disk by bm.jpg to our current directory.

To edit an image, just use another over load of Bitmap constructor that takes file as parameter.

àDisplay Icon

Icon are transparent bitmap image with specific size and used to convey status to windows system. .Net have provided built in Icons with 40-40 size for Questions, Exclamation, Information etc.

To add Icon to Form or control just call Graphics.DrawIcon method, it will do rest for us.

Graphics g = this.CreateGraphics();

g.DrawIcon(SystemIcons.Information, 40, 40);

Above code will draw 40*40 Information Icon on our form.

May 25, 2010 at 9:00 am Leave a comment

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);

g.DrawLine(p,10,10,100,100);

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

May 24, 2010 at 9:17 am Leave a comment


Archives

Categories

 

May 2012
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Blog Stats

  • 1,999

Follow

Get every new post delivered to your Inbox.