0

I am resizing image in asp.net.I succeeded resizing image.but while converting it as stream .jpg images are not working.

here my code if i set image format as jpeg it is not working.Because in C# there is no image format for .jpg

 public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
    {
        var width=image.Width;
        var height=image.Height;

        var newWidth=0;
        var newHeight=0;
        var divisor=0;
        if (width > height) {
                    newWidth = maxWidth;
                    divisor = width / maxWidth;
                    if (divisor == 0)
                    {
                        divisor = 1;
                    }
                    newHeight = Convert.ToInt32(height /divisor);
                }
                else {
                    newHeight = maxHeight;
                    divisor = height / maxHeight;
                    if (divisor == 0)
                    {
                        divisor = 1;
                    }
                    newWidth = Convert.ToInt32(width / divisor);
                }


        var newImage = new Bitmap(newWidth, newHeight);

        Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;

    }

public static Stream ToStream(this System.Drawing.Image image, ImageFormat formaw)
    {
        var stream = new System.IO.MemoryStream();
        //stream.ReadTimeout = 100000;
        image.Save(stream, formaw);
        stream.Position = 0;
        //stream.ReadTimeout = 100000;
        return stream;
    }
4

2 回答 2

1

伙计们,System.Drawing如果在 ASP.NET 服务中使用所有类,则必须显式处置。这不是可选的。任何大量流量都会使服务器崩溃。

现在,您的代码依赖 GC 来释放GraphicsBitmap实例。不幸的是,GC 将这两个对象都视为微小的、低优先级的对象,并且不知道它们实际上占用了 100+ MB 的 RAM。

以下是一些如何安全地调整图像大小且质量更好的示例:

灯光调整大小

它是 MIT 许可的,因此无需担心使用限制。

于 2012-07-30T16:42:58.583 回答
0

您正在寻找的 ImageFormat 是ImageFormat.Jpeg

于 2012-07-19T14:30:19.093 回答