Mar 21, 2014

How to resizeImage and save using c#.Net

C# Code:

private Bitmap SetResizeImage(string strImg, string FileName, int Imagewidth, int ImageHeight)
{
    int nnx = Imagewidth;
    int nny = ImageHeight;
    System.Drawing.Image img = System.Drawing.Image.FromFile(strImg);
    Bitmap cpy = null;
    cpy = new Bitmap(nnx, nny);
    using (Graphics gr = Graphics.FromImage(cpy))
    {
        gr.Clear(Color.Transparent);
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        gr.DrawImage(img, new Rectangle(0, 0, nnx, nny), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
    }
   return cpy;

}
------------------------------------------------------------------------------------------------------------
Call method through –
string ImagePath ="D:\\images\\1.png";
    string Filename = Path.GetFileNameWithoutExtension(ImagePath); //get filename from imagepath
    string Extention = Path.GetExtension(ImagePath); //get fileextention from imagepath
    int ImageWidth = 100; //put your custom image width size as you like
    int ImageHeight = 100; //put your custom image height size as you like
    Bitmap image=SetResizeImage(ImagePath, Filename, ImageWidth, ImageHeight); // here call resize method
    image.Save(Application.StartupPath + "\\" + Filename+Extention); //save resized image to startuppath