C#
  Home arrow C# arrow Page 4 - Lossless Image Resizing in 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 
Actuate Whitepapers 
Moblin 
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#

Lossless Image Resizing in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-02-20

    Table of Contents:
  • Lossless Image Resizing in C#
  • Color Quantization
  • Getting Started
  • Let's Resize!
  • Completing the Project
  • Final Words

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Lossless Image Resizing in C# - Let's Resize!


    (Page 4 of 6 )

    We have finally covered the quantization process and we can theoretically perform lossless conversion and save images, excluding the GIF limitations. We cannot violate the nature of GIFs or spend countless hours developing ourselves a better quantization algorithm - GIFs are obsolete anyway, so let's get over it! Now it's time to architect and implement the resizing function into our application.

    First of all, as you'd expect, we are going to add new components to our form. I for one have added the following new components: another button called btnResize, two textboxes called txtHeight and txtWidth, a few additional labels such as lbResize, lblRatio, lbl_x (check the screenshot attached below to understand), and of course, another piece of saveFileDialog2, which is required when we want to save our destination file.

    As I mentioned, check out the screenshot below - it should definitely help you.

    (Form Design - Screenshot)

    The rest remained untouched, such as picThumbnail, btnQuit, btnSelectPicture, btnConvert, and of course the form remained clsMain. All that is left to modify is to Show and Hide the appropriate labels and buttons when needed. This means that before we load a picture, we hide all of the components excluding two buttons (Quit and Select Picture) and the picThumbnail. After we've chosen the source image, voila, the rest of the components also appear. Now we'll have a powerful application.

    I won't attach code snippets for the additions I described above because it's quite self-explanatory and it was explained thoroughly during the first part of this series. And even if you cannot do this, the components will always be visible - it simply doesn't look professional enough. We can afford that kind of possibility.

    Now let's write the resizing algorithms! We need to code the btnResize_Click and saveFileDialog2_FileOK methods. However, we are going to create another method as well as a new function. These are going to act as the foundation for our resizing action. These are ResizeImage() and GetDimensions. The first handles the resizing process while the latter calculates the new dimensions, because we maintain proportionality (ratio).

    public void ResizeImage(int MaxWidth, int MaxHeight, string FileName, string NewFileName, string NewFileExtension)

    {

    Bitmap OrigImg = (System.Drawing.Bitmap)Image.FromFile(FileName).Clone();

    Size ResizedDimensions = GetDimensions(MaxWidth, MaxHeight, ref OrigImg);

    Bitmap NewImg = new Bitmap(OrigImg, ResizedDimensions);

    saveFile(NewImg, NewFileName, NewFileExtension);

    }

    The above code snippet is our ResizeImage method. It clones the source image and then calls our next-to-be-written GetDimensions function to calculate the new, resized dimensions. After these we can finally call our saveFile method, which remained untouched since the quantization changes (previous page). So let's see GetDimensions!

    public static Size GetDimensions(int MaxWidth, int MaxHeight, ref Bitmap Img)

    {

    int Height; int Width; float Multiplier;

    Height = Img.Height; Width = Img.Width;

    if (Height <= MaxHeight && Width <= MaxWidth)

    return new Size(Width, Height);

    Multiplier = (float)((float)MaxWidth / (float)Width);

    if ((Height * Multiplier) <= MaxHeight)

    {

    Height = (int)(Height * Multiplier);

    return new Size(MaxWidth, Height);

    }

    Multiplier = (float)MaxHeight / (float)Height;

    Width = (int)(Width * Multiplier);

    return new Size(Width, MaxHeight);

    }

    Maybe it isn't as straightforward as I had hoped. Anyway, I'm going to explain how it works. Basically, we want to preserve proportionality (ratio). At first, we check whether the new sizes aren't smaller than the original ones (in this case, we return the original ones and basically do zero resizing). If this isn't the case, then we calculate the MaxWidth and Width ratio - basically MaxWidth stands for the explicitly specified width.

    After this, we're able to see whether we need to fiddle with the height of the image. In that case, we resize it using the multiplier we've just calculated to maintain the proportionality. Of course, after this we also need to reduce the size of the width, so we calculate the MaxHeight and Height ratio to accomplish this. Once all of this is done, we end this function by returning our new size.

    More C# Articles
    More By Barzan "Tony" Antal


       · Thanks for following this series. This is the second and last part of the "Lossless...
     

    C# ARTICLES

    - 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...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained





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