Your Ad Here

Friday 6 January 2012

Working with Images in .NET Framework

Friends i am B.TECH Engineer(I.T) . Hopefully these notes may help u.


Working with Images
The .NET Framework provides tools to work with a variety of image formats, enabling you to perform many common image-editing tasks.

The Image and Bitmap Classes
The System.Drawing.Image abstract class gives you the ability to create, load, modify, and save images such as .BMP files, .JPG files, and .TIF files.

The Image class is abstract, but you can create instances of the classing using the Image.FromFile (which accepts a path to an image file as a parameter) and Image.From- Stream (which accepts a System.IO.Stream object as a parameter) methods. You can also use two classes that inherit Image: System.Drawing.Bitmap for still images, and System.Drawing.Imaging.Metafile for animated images.

Bitmap is the most commonly used class for working with new or existing images. The different constructors allow you to create a Bitmap from an existing Image, file, or stream, or to create a blank bitmap of a specified height and width. Bitmap contains two particularly useful methods that Image lacks:

GetPixel Returns a Color object describing a particular pixel in the image. A pixel is a single colored dot in the image, consisting of a red, green, and blue component.

SetPixel Sets a pixel to the specified color.
How to Display Pictures

To display an image that is saved to the disk in a form, load it with Image.FromFile and create a PictureBox control, and then use the Image to define PictureBox.Background-Image

// C#
Image i = Image.FromFile(@"C:\windows\gone fishing.bmp");
pictureBox1.BackgroundImage = i;

Similarly, the following code accomplishes the same thing using the Bitmap class:
// C#
Bitmap b = new Bitmap(@"C:\windows\gone fishing.bmp");
pictureBox1.BackgroundImage = b;
Alternatively, you can display an image as the background for a form or control by using the Graphics.DrawImage method.

// C#
Bitmap bm = new Bitmap(@"C:\WINDOWS\Web\Wallpaper\Azul.jpg");
Graphics g = this.CreateGraphics();
g.DrawImage(bm, 1, 1, this.Width, this.Height);

How to Create and Save Pictures

To create a new, blank picture, create an instance of the Bitmap class with one of the constructors that does not require an existing image. You can then edit it using the Bitmap.SetPixel method, or you can call Graphics.FromImage and edit the image using the Graphics drawing methods.

To save a picture, call Bitmap.Save. This method has several easy-to-understand overloads.
Two of the overloads accept a parameter of type System.Drawing.Imaging.Image-Format, for which you should provide one of the following properties to describe the file type: Bmp, Emf, Exif, Gif, Icon, Jpeg, MemoryBmp, Png, Tiff, or Wmf. Jpeg is the most common format for photographs, and Gif is the most common format for charts, screen shots, and drawings.

For example, the following code creates a blank 600-by-600 Bitmap, creates a Graphics object based on the Bitmap, uses the Graphics.FillPolygon and Graphics.DrawPolygon methods to draw a shape in the Bitmap, and then saves it to a file named bm.jpg in the current directory. This code can run as a console application, and it requires the System.Drawing.Drawing2D and System.Drawing.Imaging namespaces.

// C#
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.Red);
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);
To edit an existing image, simply change the Bitmap constructor in the previous example to load a picture.

How to Use Icons
Icons are transparent bitmaps of specific sizes that are used by Windows to convey status. The .NET Framework provides standard 40-by-40 system icons as properties of the SystemIcons class, including icons for exclamation, information, and question. The simplest way to add an icon to a form or image is to call the Graphics.DrawIcon or Graphics.DrawIconUnstretched methods.

// C#
Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);

You can also edit system icons or load saved icons using the constructors built into the Icon class. Once you create an instance of the Icon class, call Icon.ToBitmap to create a Bitmap object that can be edited.

No comments:

Post a Comment