How to resize an Image C#

前端 未结 17 2399
暗喜
暗喜 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:12

    public string CreateThumbnail(int maxWidth, int maxHeight, string path)
    {
    
        var image = System.Drawing.Image.FromFile(path);
        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);
        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);
        var newImage = new Bitmap(newWidth, newHeight);
        Graphics thumbGraph = Graphics.FromImage(newImage);
    
        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
        //thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
        image.Dispose();
    
        string fileRelativePath = "newsizeimages/" + maxWidth + Path.GetFileName(path);
        newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
        return fileRelativePath;
    }
    

    Click here http://bhupendrasinghsaini.blogspot.in/2014/07/resize-image-in-c.html

提交回复
热议问题