C#
  Home arrow C# arrow Page 4 - Thumbnails and Zooming with 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 
Dedicated Servers 
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#

Thumbnails and Zooming with GDI+ and C#
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-10-31

    Table of Contents:
  • Thumbnails and Zooming with GDI+ and C#
  • Image Manipulation: Thumbnail, Zooming and Saving
  • Image Manipulation in the Real World
  • Image Manipulation in the Real World, continued

  • 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


    Thumbnails and Zooming with GDI+ and C# - Image Manipulation in the Real World, continued


    (Page 4 of 4 )

    Next comes the handler for mnu200Zoom. But before that certain things have to be done. First add the following line to the class at the application level:

    private double curZoom = 1.0;

    Then the mnuLoad (from previous part) has to be changed slightly. The added code is shown in bold:

    private void mnuLoad_Click(object sender,     System.EventArgs e) {    //Change the AutoScrollMinSize property this.AutoScrollMinSize = new Size ((int)(curImage.Width * curZoom), (int)(curImage.Height * curZoom)); // 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 added code multiples the image width and height with the zoom factor to render an image with the appropriate zoom setting. Next the paint event handler has to be changed. The DrawImage() method has to be changed into the following:

    g.DrawImage(curImage, new Rectangle     (this.AutoScrollPosition.X,     this.AutoScrollPosition.Y,     (int)(curRect.Width * curZoom),     (int)(curRect.Height * curZoom)));

    The image should have the correct height and width according to the zoom factor. For that the current width and height is multiplied with the current zoom factor represented by curZoom variable. The last step in zooming is the event handler for mnu200Zoom:

    private void mnu200_Click(object sender,

        System.EventArgs e)

    {

       if(curImage != null)

        {
            curZoom = (double)200/100;

            Invalidate();

        }

    }

    Finally we have the event handler for the mnuThumbNail:

    private void mnuThumbNail_Click(object sender,     System.EventArgs e) {     if(curImage != null)     {        // Callback         Image.GetThumbnailImageAbort tnCallBack =             new Image.GetThumbnailImageAbort(tnCallbackMethod);         // Get the thumbnail image         Image thumbNailImage = curImage.GetThumbnailImage             (100, 100, tnCallBack, IntPtr.Zero);         // Create a Graphics object         Graphics tmpg = this.CreateGraphics();         tmpg.Clear(this.BackColor);         // Draw thumbnail image         tmpg.DrawImage(thumbNailImage, 40, 20);         // Dispose of Graphics object         tmpg.Dispose();     } } // Must be called, but not used public bool tnCallbackMethod() {     return false; }

    It first creates a variable of type GetThumbnailImageAbort and assigns the tnCallbackMethod() to it by passing the method to the GetThumbnailImageAbort. Then it creates a new instance of the Image class to hold the image returned by the GetThumbnailImage method, which is then used to draw the thumbnail onto the screen.

    This brings us to the end of this discussion. In this part I discussed more advanced features of GDI+. I will continue along the same lines in next part. 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.

       · In the second part of the series I have discussed about the working of GDI+ as well...
     

    C# ARTICLES

    - 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#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway