【问题标题】:restriction to image dimension asp.net限制图像尺寸 asp.net
【发布时间】:2012-08-19 07:23:15
【问题描述】:

我想限制 asp.net C# 图像的尺寸。

我有 image1:

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

我也有:

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

接下来我正在设置 image1 的尺寸

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

在这里我得到编译器错误:math.max 有错误的参数。 所以目标是不允许图像的宽度大于 1000。 如何做到这一点?

【问题讨论】:

  • 您是否查看过 MSDN 的 Math.Max 方法,您是否想过更安全的方法 Math.Min ...?确保您还为两个参数传递相同的数据类型
  • 我也尝试比较两个宽度 if(tempImage.Width > tempImage2.Width),这里编译器错误也给出了一个错误,说 tempImage.Width 方法返回 System.Web.UI.WebControls.Unit
  • 您能否显示发生方法和错误的完整代码..
  • Image1.Width = Math.Max(tempImage.Width, 1000);
  • 您已经得到答案:“tempImage.Width 方法返回 System.Web.UI.WebControls.Unit”。在 MSDN 上查找:msdn.microsoft.com/en-us/library/… 并发现您需要使用 Value 属性来获取 double 值。

标签: c# asp.net image restriction image-size


【解决方案1】:

Math.Max 期望 Double

尝试像这样将你的参数转换为(双)

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

对于初学者来说,请看上面的代码 Math.Max 期望以下

   Math.Max(double , double);

也在你的代码中的这一行

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

为什么不创建 tempImage 然后设置最大宽度 然后打电话 temp.ImageUrl = ib.Image();

【讨论】:

  • 编译器出错:无法将 tempImage.Width 转换为 double。你是对的,但我认为什么是第一,什么是第二并不重要。该程序仍然有效。
  • 因为您必须将图像宽度转换为 Double.. 任务是否有效,您是否尝试过我在使用 var convDouble.. 的示例中所做的操作?
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 2012-08-19
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多