C#
  Home arrow C# arrow Page 4 - 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 
Mobile Linux 
App Generation ROI 
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 in the Real World


    (Page 4 of 4 )

    Starting from this part onwards, I will be developing an image manipulation application using C# and GDI+. The basic version that will be developed now will provide the following capabilities:

    • Loading an image file.
    • Rotating the loaded image 90 degrees.
    • Flipping the image along its X-axis.

    First the menus must be set up as shown in the figure below.

    The names of the menus are:

    • mnuLoad - the submenu that will show the Open dialog box and loads the file.
    • mnu90rotate - rotates the image by 90 degrees.
    • mnuXfilp- flips the image along X-axis.

    Here is the handler for loading the image:

    private void mnuLoad_Click(object sender,     System.EventArgs e) {     // Create OpenFileDialog     OpenFileDialog opnDlg = new OpenFileDialog();     // Set a filter for images     opnDlg.Filter =         "All Image files|*.bmp;*.gif;*.jpg;*.ico;"+        "*.emf;,*.wmf|Bitmap Files(*.bmp;*.gif;*.jpg;"+         "*.ico)|*.bmp;*.gif;*.jpg;*.ico|"+         "Meta Files(*.emf;*.wmf;*.png)|*.emf;*.wmf;*.png";     opnDlg.Title = "ImageViewer: Open Image File";     opnDlg.ShowHelp = true;     // If OK, selected     if(opnDlg.ShowDialog() == DialogResult.OK)     {         // Read current selected file name         curFileName = opnDlg.FileName;         // Create the Image object using         // Image.FromFile         try         {             curImage = Image.FromFile(curFileName);         }         catch(Exception exp)        {            MessageBox.Show(exp.Message);         }     }     // Repaint the form, which forces the paint     // event handler     Invalidate(); }

    The name of the file returned by the open file dialog is set as the current file name. Then it is used to load the file using the FromFile function of the Image class. The returned image is assigned to the curImage variable, which is of type Image, and its scope is of class level. The Next Invalidate() method is called so that a paint event is fired. Following the code, embedded in the Form's Paint method handler is this piece, which actually draws the image:

    private void Form1_Paint(object sender,     System.Windows.Forms.PaintEventArgs e) {           Graphics g = e.Graphics;           if(curImage != null)           {              // Draw image using the DrawImage method               g.DrawImage(curImage,                     AutoScrollPosition.X,                     AutoScrollPosition.Y,                     curImage.Width,                     curImage.Height );           } }

    The logic checks whether the current image is null or not. If it is not null, the DrawImage method of the Graphics class is given the curImage as the Image to be loaded. The Graphics object is obtained from the Graphics property of PaintEventArgs object. Next is the handler for rotating the image; it is embedded in the handler for the mnu90rotate menu item. Here is the code:

    // Rotate 90 degrees private void mnu90rotate_Click(object sender,     System.EventArgs e) {     if(curImage != null)     {        curImage.RotateFlip(             RotateFlipType.Rotate90FlipNone);         Invalidate();     } }

    The image is rotated using the RotateFlip method of the Image class. Since curImage is of the Image class type, the method can be directly called on the Image object. Next it calls the Invalidate() method to redraw the image.

    Last comes the flip logic. It is embedded in the handler for the mnuXflip menu item.

    // Flip X private void mnuXflip_Click(object sender,     System.EventArgs e) {     if(curImage != null)     {         curImage.RotateFlip(             RotateFlipType.RotateNoneFlipX);         Invalidate();     } }

    That's all for the example. This sets up the stage for image manipulation using GDI+ in C#. However certain aspects of the problem of image manipulation have been left out, including saving the image and how GDI+ works. These will be the topics that will be tackled in the next part of this series. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - 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...





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