C#
  Home arrow C# arrow Page 3 - Basic Image Manipulation using GDI+ and C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Basic Image Manipulation using GDI+ and C#
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 16
    2006-10-24

    Table of Contents:
  • Basic Image Manipulation using GDI+ and C#
  • Image Manipulation using GDI : the Terminology
  • Image Manipulation Using GDI : Step By Step
  • Image Manipulation in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Basic Image Manipulation using GDI+ and C# - Image Manipulation Using GDI : Step By Step


    (Page 3 of 4 )

    Now let's have a look at the steps involved in using the GDI+ for image manipulation. There are four main steps:

    1. Overriding the Paint event of Form.
    2. Obtaining the Graphics object.
    3. Obtaining the Image object.
    4. Apply manipulation methods.

    The first two steps are required whenever one has to work with GDI+. However, the last two come into the picture (pun not intended) only when image manipulation has to be done. So here are the details:

    Overriding the Paint event of form

    In .Net applications, drawing is done on a Form's surface. This is same for both web and desktop applications. The function where the actual drawing code can be hooked in is the Paint method. So, the first step is to override this method to get a foothold into the draw process within the application. This can be achieved either by supplying a Paint handler to the Form's paint method or by overriding the OnPaint method. It is better to provide a Paint handler.

    Obtaining the Graphics object

    The next step is to obtain the object of the Graphics class, because it is only through the Graphics object that the drawing methods can be called. To show an image it has to be drawn onto the Form. That can only be done using the DrawImage method of an object of the Graphics class. An object of the Graphics class is associated with a form. So it can be obtained by one of three methods:

    • Using PaintEventArgs argument passed into the Paint handler.
    • Using PaintArgs argument passed into the OnPaint method.
    • Using CreateGraphics method of a Form.

    An example of the first method would be thus:

    private void form1_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    }

    where form1_Paint is the handler for Paint event of Form form1. The Graphics property of the PaintEventArgs returns a Graphics object associated with the Form object.  The second way can be exemplified as:

    protected override void OnPaint(PaintEvent { Graphics g = e.Graphics; }

    The main difference between the first and second examples is that in the second way, the OnPaint method is overridden. The example for the third way would be:

    Graphics g = this.CreateGraphics();

    Where this refers to the Form in which this statement has been embedded. The CreateGraphics() method returns a method object of the Form. The only drawback of this approach is that the Graphics object would have to be disposed of manually using the dispose method of the Graphics object. I prefer the first method; therefore I will be using it henceforth.

    Obtaining the Image object

    The image object can be obtained in three different ways using the static methods provided by the Image class:

    1. The FromFile method returns an Image from the file specified as an argument.
    2. FromHbitmap creates and returns an Image object from a window handle to a bitmap.
    3. FromStream creates an Image object from a stream of bytes (in a file or a database).

    Among these, the first method is the most commonly used if the image is available as a file, whereas the third method comes handy in web applications where the images would be stored as a column value of a table. The following statement creates an object of the Image class using the first method:

     Image curImage = Image.FromFile("flower.bmp");

    Apply manipulation methods

    For the sole purpose of imaging, the Image class provides many methods. The most common in imaging are Flip and Rotate. The Image class provides for these by the following method - RotateFlip. The parameter to this method defines whether flipping as well as rotating both have to be done or only a flip or rotation must be applied. It also defines through what degrees flipping or rotating must be done. The following table provides the most common arguments to the RotateFlip method as well as their descriptions. The arguments being passed are the members of the RotateFlipType.

     

    Member

    Description

    Rotate180FlipNone

    180-degree rotation without flipping

    Rotate180FlipX

    180-degree rotation with a horizontal flip

    Rotate180FlipXY

    180-degree rotation with horizontal and vertical flips

    Rotate180FlipY

    180-degree rotation with a vertical flip

    Rotate270FlipNone

    270-degree rotation without flipping

    Rotate270FlipX

    270-degree rotation with a horizontal flip

    Rotate270FlipXY

    270-degree rotation with horizontal and vertical flips

    Rotate270FlipY

    270-degree rotation with a vertical flip

    Rotate90FlipNone

    90-degree rotation without flipping

    Rotate90FlipX

    90-degree rotation with a horizontal flip

    Rotate90FlipXY

    90-degree rotation with horizontal and vertical flips

    Rotate90FlipY

    90-degree rotation with a vertical flip

    RotateNoneFlipNone

    No rotation and no flipping

    RotateNoneFlipX

    No rotation, with a horizontal flip

    RotateNoneFlipXY

    No rotation, with horizontal and vertical flips

    RotateNoneFlipY

    No rotation, with a vertical flip

    For example, to flip and rotate an image through 90 degrees on both the X- and Y- axes the statement would be:

    curImage.RotateFlip(RotateFlipType.Rotate90FlipXY);

    where curImage is an object of the type Image and the argument tells the RotateFlip method that the image has to be rotated through 90 degrees and needs to be flipped both vertically and horizontally.

    That brings us to the end of this section. In the next section I will develop an application that will load, flip and rotate the loaded image.

    More C# Articles
    More By A.P.Rajshekhar


       · GDI+ is a great progress over GDI in terms of API calls. In this article I have...
       · Hi,I've application to capture video of image and grab the still image. It runs...
     

    C# ARTICLES

    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT