-1

I want to make restriction to the dimensions of the asp.net C# image.

I have image1:

ImageButton ib = (ImageButton)sender;
Image1.ImageUrl = ib.ImageUrl;

Also I have:

Image tempImage = new Image();
tempImage.ImageUrl = ib.ImageUrl;

next I am setting dimensions of image1

Image1.Width = Math.Max(tempImage.Width, 1000);

Here I get compiler error: math.max has bad parameters. So the aim is to not allow the width of image be bigger than 1000. How this can be done?

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Nurlan
  • 2,860
  • 19
  • 47
  • 64
  • Have you looked at MSDN for Math.Max Method have you thought about a much safer method Math.Min ...? be sure that you are also passing the same datatype for both params – MethodMan Aug 23 '12 at 17:55
  • i also have tried to compare two widthes if(tempImage.Width > tempImage2.Width), here the compiler error also gives an error saying tempImage.Width method returns System.Web.UI.WebControls.Unit – Nurlan Aug 23 '12 at 18:01
  • Can you show the full code where the method as well as the error is happening.. – MethodMan Aug 23 '12 at 18:04
  • Image1.Width = Math.Max(tempImage.Width, 1000); – Nurlan Aug 23 '12 at 18:20
  • You've got the answer: "tempImage.Width method returns System.Web.UI.WebControls.Unit". Look that up on MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.unit.value.aspx and find that you need to use the `Value` property to get the `double` value. – Heretic Monkey Aug 23 '12 at 19:11
  • look at this example it may give you a better idea of what you need to do.. http://www.blackbeltcoder.com/Articles/graph/programmatically-resizing-an-image – MethodMan Aug 23 '12 at 20:31

1 Answers1

0

Math.Max expects Double

try casting your params to (double) like this

  int maxValue = 1000;
  var convDouble = Convert.ToDouble((double)tempImage.Width,(double)maxValue);

for starters look at the code above Math.Max expects the following

   Math.Max(double , double);

also in this line in your code

Image tempImage = new Image();
tempImage.ImageUrl = ib.ImageUrl

why not create the tempImage then set the max width then call temp.ImageUrl = ib.Image();

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • compiler gets error: cannot convert tempImage.Width to double. You are right, but I think it doesnt' matter what is first and what is second. The program works anyway. – Nurlan Aug 23 '12 at 18:19
  • because you have to cast the Image Width to a Double.. quest is is it working have you tried what I have done in the example where I am using var convDouble..? – MethodMan Aug 24 '12 at 14:03