Nurlan
Nurlan

Reputation: 2960

compare dimensions of image asp.net

We have two images.

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

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

I want to compare the widthes of these images and find image with greater width:

I tried following:

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

Compiler gets an error: cannot compare these two values.

I tried following:

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

Compiler couldn't convert width to double.

So how to compare the width of images and find the image with bigger width?

Upvotes: 0

Views: 529

Answers (3)

Peerkon
Peerkon

Reputation: 65

I would put the width into a var first then compare it.

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

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

Upvotes: 0

Jim O&#39;Neil
Jim O&#39;Neil

Reputation: 23754

You're getting the error because the Width property of an Image is a Unit structure type, not a scalar and there is no comparison operator implemented for it.

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

will work, but that comparison is strictly only valid if the Type of the unit is the same. In your sample, it defaults to pixel, but in a more general case, you'd need to make sure you're comparing values of the same unit.

Upvotes: 3

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17370

This worked for me:

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

Good luck!

Upvotes: 1

Related Questions