2012-08-23 73 views
0

我们有两个图像。比较图像的尺寸asp.net

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

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

我想比较这些图像的widthes,发现图像具有更大的宽度:

我尝试以下操作:

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

编译器不能转换宽度增加一倍。

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

回答

3

你得到的错误,原因是图像的宽度属性是Unit structure类型,而不是一个标量并没有因为它没有实施比较操作。

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

会的工作,但比较严格的唯一有效的,如果单位的Type是一样的。在你的示例中,它默认为像素,但在更一般的情况下,你需要确保你正在比较同一单元的值。

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

祝你好运!

0

我会把宽度放入一个变种,然后比较它。

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

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

错误:无法将宽度转换为int – Nurlan