How to resize an Image C#

前端 未结 17 2412
暗喜
暗喜 2020-11-21 22:28

As Size, Width and Height are Get() properties of System.Drawing.Image;
How can I resize an Image object

17条回答
  •  情话喂你
    2020-11-21 23:26

    Use below function with below example for changing image size :

    //Example : 
    System.Net.Mime.MediaTypeNames.Image newImage = System.Net.Mime.MediaTypeNames.Image.FromFile("SampImag.jpg");
    System.Net.Mime.MediaTypeNames.Image temImag = FormatImage(newImage, 100, 100);
    
    //image size modification unction   
    public static System.Net.Mime.MediaTypeNames.Image FormatImage(System.Net.Mime.MediaTypeNames.Image img, int outputWidth, int outputHeight)
    {
    
        Bitmap outputImage = null;
        Graphics graphics = null;
        try
        {
             outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
             graphics = Graphics.FromImage(outputImage);
             graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight),
             new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
    
             return outputImage;
         }
         catch (Exception ex)
         {
               return img;
         }
    }
    

提交回复
热议问题