C# 在某些尺寸上生成动态图像失败

C# 在某些尺寸上生成动态图像失败,c#,asp.net,image-resizing,C#,Asp.net,Image Resizing,我使用以下代码以这种格式动态调整图像的大小: resize.aspx?img=test.jpg&w=120&h=120 这将从源图像中提取最大的正方形,并将其调整为所需的尺寸,但代码在某些宽度和高度上失败,例如,当主图像为399×399像素,我想将其调整为120×120时,代码失败,出现以下错误: 异常详细信息:System.OutOfMemoryException:内存不足 在这一行: 克隆=新位图(tempImage).Clone(rect,tempImage.PixelF

我使用以下代码以这种格式动态调整图像的大小:

resize.aspx?img=test.jpg&w=120&h=120
这将从源图像中提取最大的正方形,并将其调整为所需的尺寸,但代码在某些宽度和高度上失败,例如,当主图像为399×399像素,我想将其调整为120×120时,代码失败,出现以下错误:

异常详细信息:System.OutOfMemoryException:内存不足

在这一行:

克隆=新位图(tempImage).Clone(rect,tempImage.PixelFormat)

有趣的是,错误只出现在某些值上。下一个值是126*126,因此似乎将大小舍入为INT可能会导致问题。但我不知道为什么在没有被零或负值除的情况下它会失败

编辑:在所有计算之后,RealRatio等于MyRatio,在最终矩形上的值为(0,0120)


System.Drawing.Image oldImage、newImage、克隆、tempImage;
System.Drawing.rect矩形;
无效页面加载(对象发送方,事件参数e){
string strFileName=Convert.ToString(Request.QueryString[“img”]);
字符串路径=Server.MapPath(strFileName);
使用(oldImage=System.Drawing.Image.FromFile(路径)){
float w=(float)Convert.ToInt32(Request.QueryString[“w”]);
float h=(float)Convert.ToInt32(Request.QueryString[“h”]);
如果(w!=0&&h==0){h=(float)((oldImage.Height*w)/oldImage.Width);}
如果(h!=0&&w==0){w=(float)((oldImage.Width*h)/oldImage.Height);}
float RealRatio=((float)oldImage.Width/(float)oldImage.Height);
浮动比率=((浮动)w/(浮动)h);
浮动缩放高度=(浮动)((浮动)w/(浮动)oldImage.Width)*(浮动)oldImage.Height;
浮动缩放宽度=(浮动)((浮动)h/(浮动)oldImage.Height)*(浮动)oldImage.Width;

如果(realRatio您没有处理其中一个映像:
tempImage
oldImage
被处理了两次。我不知道这会有多大区别。您的进程是32位还是64位?有多少请求同时进行?我已在IIS应用程序池中启用了32位应用程序。@Crowcoderdebug它来找出它们是什么最后矩形上的值我已将矩形的输出值添加到问题中。它们是(0,0120)@Aristosy您没有处理其中一个映像:
tempImage
oldImage
被处理了两次。我不知道这会有多大区别。您的进程是32位还是64位?有多少请求同时进行?我已在IIS应用程序池中启用了32位应用程序。@Crowcoderdebug它来找出是什么最后一个矩形上的e值我把矩形的输出值加到这个问题上。它们是(0,0120)@Aristos
<%@ Page Language="C#" Debug="true"%>
<%@ import Namespace="System.Drawing" %>
<script runat="server">
System.Drawing.Image oldImage, newImage,cloned,tempImage;
System.Drawing.Rectangle rect;
void Page_Load(Object sender, EventArgs e) {
    string strFileName = Convert.ToString(Request.QueryString["img"]);
    string path=Server.MapPath(strFileName);

    using (oldImage = System.Drawing.Image.FromFile(path)){

        float w=(float)Convert.ToInt32(Request.QueryString["w"]);
        float h=(float)Convert.ToInt32(Request.QueryString["h"]);
        if (w!=0 && h==0){h=(float)((oldImage.Height*w)/oldImage.Width);}
        if (h!=0 && w==0){w=(float)((oldImage.Width*h)/oldImage.Height);}
        
        float RealRatio=((float)oldImage.Width/(float)oldImage.Height);
        float MyRatio=((float)w/(float)h);
        float ScaledHeight=(float)((float)w/(float)oldImage.Width)*(float)oldImage.Height;
        float ScaledWidth=(float)((float)h/(float)oldImage.Height)*(float)oldImage.Width;

        if (RealRatio<MyRatio)
            {
            tempImage = new Bitmap(oldImage,new Size((int)w,(int)ScaledHeight));
            rect= new Rectangle(0,(int)((ScaledHeight-h)/2),(int)w,(int)h);
            cloned = new Bitmap(tempImage).Clone(rect, tempImage.PixelFormat);
            newImage = new Bitmap(cloned);
            }
        else
            {
            tempImage = new Bitmap(oldImage,new Size((int)ScaledWidth,(int)h));
            rect= new Rectangle((int)((ScaledWidth-w)/2),0,(int)w,(int)h);
            cloned = new Bitmap(tempImage).Clone(rect, tempImage.PixelFormat);
            newImage = new Bitmap(cloned);
            }
        cloned.Dispose();   
        
        Response.ContentType = "image/jpeg";
        newImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        oldImage.Dispose();
        newImage.Dispose();
        oldImage = null;
        newImage = null;
    }
}
</script>