比较图像asp.net的尺寸

时间:2012-08-23 19:06:02

标签: c# asp.net image compare dimensions

我们有两张照片。

Image tempImage = new Image();
tempImage.width = 500;

Image tempImage2 = new Image();
tempImage2.width = 1000;

我想比较这些图像的大小并找到宽度更大的图像:

我试过以下:

if (tempImage.Width < tempImage2.Width) Response.write("width of tempImage2 is bigger");
else Response.write("width of tempImage1 is bigger");

编译器出错:无法比较这两个值。

我试过以下:

    Image1.Width = (int)Math.Max(Convert.toDouble(tempImage.Width),Convert.toDouble(tempImage2.Width));
Response.Write("max width is " + Image1.Width);

编译器无法将宽度转换为double。

那么如何比较图像的宽度并找到宽度更大的图像?

3 个答案:

答案 0 :(得分:3)

您收到错误是因为Image的Width属性是Unit structure类型,而不是标量,并且没有为它实现比较运算符。

if (i.Width.Value < j.Width.Value) 

会起作用,但只有当单位的Type相同时,该比较才有效。在您的示例中,它默认为像素,但在更一般的情况下,您需要确保比较相同单位的值。

答案 1 :(得分:1)

这对我有用:

protected void Page_Load(object sender, EventArgs e)
{
    Image tmp1 = new Image();
    Image tmp2 = new Image();

    tmp1.Width = new Unit(500);
    tmp2.Width = new Unit(1000);

    Response.Write(tmp1.Width.Value < tmp2.Width.Value);
}
祝你好运!

答案 2 :(得分:0)

我先将宽度放入var然后进行比较。

  int width1 = image1.Width.Value;
  int width2 = image2.Width.Value;

 if(width1 < width2){
  //apply code   }