Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

A simple way to resize an image

Tuesday, July 28, 2009 10:13 AM

Here is a very simple way to resize an image using a bitmap as a canvas and the graphics object as a the resizing tool. Don’t forget to reference System.Drawing.

/// <summary>
/// Resizes an image from a source file to a destination file
/// the destination file will contain the exact image at the size specified
/// </summary>
/// <param name="sourceImagePath">the source image file that needs to be resized</param>
/// <param name="destinationImagePath">the destination path that the source 
/// image file needs to be resized to</param>
/// <param name="width">the new width</param>
/// <param name="height">the new height</param>
static void ResizeImage(string sourceImagePath, 
    string destinationImagePath, int width, int height)
{
    // Create an image object of the original image
    System.Drawing.Image 
        originalImage = System.Drawing.Image.FromFile(sourceImagePath, true);     

    // Create a new bitmap that we will use as a canvas to drawn on the new resized imaged
    Bitmap newBitmap = new Bitmap(originalImage, new System.Drawing.Size(width, height));

    // Using the graphics object draw the orginal image on the canvas that was created above
    using (Graphics g = Graphics.FromImage(newBitmap))
    {
        g.InterpolationMode = 
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(originalImage, 0, 0, width, height);
    }

    // Save the new resize image to a new path
    ((Image)newBitmap).Save(destinationImagePath);
}
DotNetKicks Image

Your Comments.

  • # re: A simple way to resize an image

    GravatarI really liked your method. thanks for sharing this:) hope many people will find it useful as I did. have read lots of articles on the topic downloaded by torrent search engine , but have never thought that could be so easy

    Left by Edward at 12/14/2009 7:00 AM
  • # re: A simple way to resize an image

    GravatarI think all code should be written with maintainability and readability in mind at all times, especially if it is in the team’s repository. I don’t believe it is feasible to do the above tasks you mentioned every time you need to pass on your work.
    http://www.crapsgameplay.net/

    Left by How to play the game of craps at 1/13/2010 5:28 AM
  • # re: A simple way to resize an image

    GravatarI think all code should be written with maintainability and readability in mind at all times, especially if it is in the team’s repository. I don’t believe it is feasible to do the above tasks you mentioned every time you need to pass on your work.
    How to play the game of craps

    Left by How to play the game of crap at 1/13/2010 5:30 AM
  • # re: A simple way to resize an image

    GravatarThe beauty of these blogging engines and CMS platforms is the lack of limitations and ease of manipulation that allows developers to implement rich content and 'skin' the site in such a way that with very little effort one would never notice what it is making the site tick all without limiting content and effectiveness.
    Web Design New Jersey

    Left by Sam at 1/22/2010 2:25 AM
  • # re: A simple way to resize an image

    GravatarHey I love your style I will subscribe for your feed please keep posting..
    SEO VA

    Left by ALina at 1/22/2010 3:53 AM
  • # re: A simple way to resize an image

    GravatarPretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon..
    Video Marketing Services

    Left by Marin at 1/22/2010 3:54 AM
  • # re: A simple way to resize an image

    GravatarEasy option to get useful information as well as share good stuff with good ideas and concepts..
    web design kent

    Left by will at 1/22/2010 3:56 AM
  • # re: A simple way to resize an image

    GravatarI like when people are trying to challenge me, and I appreciate that you maintain a fairly active dialogue with your readers. Couldn't agree any more. Your advice is great..
    Remote Infrastructure Management

    Left by marry at 1/22/2010 3:57 AM
  • # re: A simple way to resize an image

    GravatarVery interesting topic will bookmark your site to check if you write more about in the future. Miami Computer IT Support

    Left by ricky at 1/22/2010 3:57 AM
  • # re: A simple way to resize an image

    GravatarHi .. I just stumbled upon your post.. a gud view point.. Hey ur post left me quenching for more Your post really gives out useful knowledge.thanks..
    Accounting Software

    Left by warne at 1/22/2010 3:59 AM
  • # re: A simple way to resize an image

    GravatarThis is easier and surely gives comfort to internet users. Thanks for sharing. Post like this offers great benefit. Thank you!..
    Zend CMS

    Left by shane at 1/22/2010 3:59 AM
  • # re: A simple way to resize an image

    GravatarGood post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.
    Leaflets Kent

    Left by watson at 1/22/2010 4:00 AM
  • # re: A simple way to resize an image

    GravatarHi..thanks for the information… loved it simply Oh man! What an amazing thing to say..
    Used Cell Phones

    Left by johnty at 1/22/2010 4:03 AM
  • # re: A simple way to resize an image

    GravatarReally i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
    Dedicated Servers UK

    Left by Rhodes at 1/22/2010 4:06 AM
  • # re: A simple way to resize an image

    GravatarThank you, for your clear, concise and clean explanation, my head feels better already.
    Phone Systems va

    Left by wilson at 1/22/2010 4:08 AM
  • # re: A simple way to resize an image

    GravatarHi… that was great stuff.. I really like reading on this subject Could you tell me more on that… I love to explore.Zoekmachine optimalisatie

    Left by Nikole at 1/22/2010 4:09 AM
  • # re: A simple way to resize an image

    GravatarI was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
    Web Design India

    Left by karry desouza at 1/22/2010 4:10 AM
  • # re: A simple way to resize an image

    GravatarI have been using SQL 2008 some time and have been annoyed over the Intellisense that doesn't see any new schema changes and I need to do a refresh of Intellisense. How do you do that? (it took me a while before I got tired of this and searched the Internet to figure it out)
    Miami Web Design

    Left by jackhard at 1/25/2010 5:02 AM
  • # re: A simple way to resize an image

    GravatarEveryone knows that Sql server 2008 has an excellent feature for supporting the intellisense. This will surely fasten the development. Last week, I have created 3 tables in my Sql server 2008 and when I try to write the stored procedure based on those tables, surprisingly they weren't shown in the intellisense any more.
    Web Design London

    Left by jackhard at 1/25/2010 5:03 AM
  • # I wanted to thank..

    GravatarI wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post....
    Top SEO Companies India

    Left by Top SEO Companies India at 2/9/2010 5:00 AM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 8 and 6 and type the answer here:

Preview Your Comment.

 
Next entries »