Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Resizing Image in C#
Resizing Image in C#
Resizing Image in C#


by Awadhendra Tiwari on 8/6/2011 8:26:12 PM

Views: 8616       Comments: 1

Resizing Image in C#

In this article we learn that how to resize image in c#. To complete this task firstly we design a user interface for resize image which is combination of some buttons, panels, textboxes, labels, picture box and numeric drop down. After designing your user interface for resize image demo it looks like following

Resizing Image in C#

1)      Now firstly creates some properties in your form class to store some information.

public Size OriginalImageSize { get; set; }        //Store original image size.

public Size NewImageSize { get; set; }             //Store new image size.

 

2)      Now click on Browse Image button and write down following code to browse image in picture box.

private void btnBrowseImage_Click(object sender, EventArgs e)

{

            OpenFileDialog openImage = new OpenFileDialog();

            DialogResult result = openImage.ShowDialog();

            if (result == DialogResult.OK)

            {

                //Load image in picture box and set size mode property of image as normal.

                picImageResize.Load(openImage.FileName);

                picImageResize.SizeMode = PictureBoxSizeMode.Normal;

 

                //Store source location of image in textbox.

                txtImageOrignalLocation.Text = openImage.FileName;

 

                //Retrive height and width of image and store in OriginalImageSize variable.

                int imgWidth = picImageResize.Image.Width;

                int imgHeight = picImageResize.Image.Height;

                OriginalImageSize = new Size(imgWidth, imgHeight);

 

                string imgSize = "Width " + imgWidth + " px  Height " + imgHeight+" px";

                lblOriginalSizeValue.Text = imgSize;

            }

}

This code will store description of image in some controls and load image in picture box.

3)      Create a method namedScaleByPercent(Image imgPhoto, int percent) which will resize an image and return Image object which is resized by a percent.

//This method will resize image by scale percentace.

        static Image ScaleByPercent(Image imgPhoto, int Percent)

        {

            float nPercent = ((float)Percent / 100);

 

            int sourceWidth = imgPhoto.Width;     //store original width of source image.

            int sourceHeight = imgPhoto.Height;   //store original height of source image.

            int sourceX = 0;        //x-axis of source image.

            int sourceY = 0;        //y-axis of source image.

 

            int destX = 0;          //x-axis of destination image.

            int destY = 0;          //y-axis of destination image.

            //Calcuate height and width of resized image.

            int destWidth = (int)(sourceWidth * nPercent);

            int destHeight = (int)(sourceHeight * nPercent);

 

            //Create a new bitmap object.

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight,

                                     PixelFormat.Format24bppRgb);

            //Set resolution of bitmap.

            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,

                                    imgPhoto.VerticalResolution);

            //Create a graphics object and set quality of graphics.

            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

 

            //Draw image by using DrawImage() method of graphics class.

            grPhoto.DrawImage(imgPhoto,

                new Rectangle(destX, destY, destWidth, destHeight),

                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),

                GraphicsUnit.Pixel);

 

            grPhoto.Dispose();  //Dispose graphics object.

            return bmPhoto;

        }

4)  On click event of OK button right down following code. This code will load resized image in picture box and you can see output.

private void btnOK_Click(object sender, EventArgs e)

        {

            //When we click on ok button then call scalebypercent method which return resize image and store it Image type variable.

            Image scaledImage = ScaleByPercent(picImageResize.Image, (int)numericUpDown1.Value);

            picImageResize.Image = scaledImage;        //Display that image in picture box.

 

            //Retrive width and height of image.

            string imgSize = "Width " + scaledImage.Width + " px  Height " + scaledImage.Height + " px";

            lblNewSizeValue.Text = imgSize;

        }

 

5)      Now our final steps to save our resized image in file. To Save resized image write down following code.

 

//When we click on save button then this image is saved in new location.

        private void btnSaveImage_Click(object sender, EventArgs e)

        {

            SaveFileDialog sfd = new SaveFileDialog();   //Create an object of SaveFileDialog class.

            sfd.DefaultExt = "jpeg";    //Set default extension of savefiledialog.

            if (DialogResult.OK == sfd.ShowDialog())

            {

                string fileName = sfd.FileName;    //Store file name in string variable.

                txtDestinationLocation.Text = fileName;   //Display saved file location in textbox.

                Bitmap imgImage = new Bitmap(picImageResize.Image);    //Create an object of Bitmap class/

                imgImage.Save(fileName, ImageFormat.Jpeg);   //Call save method of Bitmap class.

            }

        }

Output of the following code snippet is as follows

Resizing Image in C#

Click on browse image button and load image in picture box.

Resizing Image in C#

Enter percentage value in numeric up down and click on OK button.

Resizing Image in C#

Now finally click on save button and save resized image.

Report Abuse Form
Reason:    
 


Resizing Image in C#
by Uttam Misra 8/6/2011 9:00:16 AM
Nice Article..Well Explained..Keep writing.
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5110
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.