ASP.NET C#中的图像大小调整

时间:2013-02-22 06:37:31

标签: asp.net image-resizing

我想在ASP.NET C#中单击一键,将一个图像调整为多个图像,如thumb_image,Small_image,big_image。

请为我提供帮助或示例代码..

2 个答案:

答案 0 :(得分:1)

你可以这样做。

 var thumbNail = CreateThumbnail(100, 100, fullPath);

        public static Image CreateThumbnail(int maxWidth, int maxHeight, string path)
    {

        var image = 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.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        image.Dispose();
        return newImage;
    }

答案 1 :(得分:0)

我希望您使用库来执行此操作。有很多代码示例,但它们不是为服务器端使用而设计的,而ImageResizer

至少read this article on what pitfalls to avoid if you decide to go copy & paste route