【问题标题】:compare dimensions of image asp.net比较图像 asp.net 的尺寸
【发布时间】:2012-08-19 08:51:59
【问题描述】:

我们有两张图片。

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);

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

那么如何比较图片的宽度,找到宽度较大的图片呢?

【问题讨论】:

    标签: c# asp.net image compare dimensions


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这对我有用:

      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);
      }
      

      祝你好运!

      【讨论】:

        【解决方案3】:

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

          int width1 = image1.Width.Value;
          int width2 = image2.Width.Value;
        
         if(width1 < width2){
          //apply code   }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-19
          • 1970-01-01
          • 1970-01-01
          • 2012-08-19
          • 1970-01-01
          • 1970-01-01
          • 2014-06-05
          • 2014-04-24
          相关资源
          最近更新 更多