Monday, 25 July 2011

Drawing in ASP.NET 2.0 and VB.NET


This tutorial will show you how to draw pie and font in ASP.NET and VB.NET 2.0 by using the classes of Graphics.

The class of Graphics provide the method of drawing to display device. For instance, the Rectangle, point  and others GDI+ basic components that can be assembled.  The class of Pen is used for drawing line and curve, while the classes derived from the abstract class Brush can be used for fill the shape. The class of FontFamily have the similar design, but have a little bit of differences on modality.

First, you will need to import the System.Drawing, System.Drawing.Imaging, System.Drawing.Text namespace.

The namespace of system.Drawing provides the access to basic GDI+ graphic function. In the namespace of System.Drawing.Drawing2D, System.Drawing.Imaging and System.Drawing.Text , .Net provides more advanced functionalities.

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Create canvas and fill the background

Dim objBitmap As New System.Drawing.Bitmap(400, 440)
Dim objGraphics As System.Drawing.Graphics
objGraphics = System.Drawing.Graphics.FromImage(objBitmap)
objGraphics.Clear(Drawing.Color.White)

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Draw the pie and fill

Dim p As New Drawing.Pen(Drawing.Color.Yellow, 0)
Dim rect As New Drawing.Rectangle(10, 10, 280, 280)
objGraphics.DrawEllipse(p, rect)

Dim b1 As New Drawing.SolidBrush(Drawing.Color.Red)
Dim b2 As New Drawing.SolidBrush(Drawing.Color.Green)
Dim b3 As New Drawing.SolidBrush(Drawing.Color.Blue)
objGraphics.FillPie(b1, rect, 0.0F, 90.0F)
objGraphics.FillPie(b2, rect, 90.0F, 60.0F)
objGraphics.FillPie(b3, rect, 150.0F, 210.0F)

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Draw font

Dim fontfml As New Drawing.FontFamily(Drawing.Text.GenericFontFamilies.Serif)
Dim font As New Drawing.Font(fontfml, 14)
Dim brush As New Drawing.SolidBrush(Drawing.Color.Blue)
objGraphics.DrawString("Draw Graphics", font, brush, 100, 300)

Export and save to picture

objBitmap.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Gif)
objBitmap.Save(Server.MapPath("x.jpg"), Drawing.Imaging.ImageFormat.Jpeg)

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

End drawing

objBitmap.Dispose()
objGraphics.Dispose()

Looking for the C#.NET 2005 Version? Click Here!


Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment