Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何获得图像控件的宽度和高度_Asp.net_Image_Image Resizing - Fatal编程技术网

Asp.net 如何获得图像控件的宽度和高度

Asp.net 如何获得图像控件的宽度和高度,asp.net,image,image-resizing,Asp.net,Image,Image Resizing,我想在asp.net中按比例更改图像的大小,问题是我无法获得从数据库加载的图像的实际大小。代码如下: imgAvatar.ImageUrl = "~/Modules/FileViewer.ashx?id=" + o.EventID; double r = imgAvatar.Width.Value / 300.00; imgAvatar.Width = new Unit(300, UnitType.Pixel);

我想在asp.net中按比例更改图像的大小,问题是我无法获得从数据库加载的图像的实际大小。代码如下:

imgAvatar.ImageUrl = "~/Modules/FileViewer.ashx?id=" + o.EventID;                      
        double r = imgAvatar.Width.Value / 300.00;
        imgAvatar.Width = new Unit(300, UnitType.Pixel);
        imgAvatar.Height = new Unit(imgAvatar.Height.Value / r, UnitType.Pixel);
但是
imgAvatar.Width.Value始终为0.0。

您有什么建议吗?

不要设置宽度和高度。渲染的IMG标签的大小将与下载图像的大小相同

但是,如果图像太大,则可能会出现问题。在这种情况下,请使用CSS设置最大值:

max-width: 300px;
max-height: 300px;

考虑到我上面的回答,我可能误解了这个问题。无论如何,我认为这样做的方式与此类似:

System.Drawing.Image image = System.Drawing.Image.FromFile(this.Server.MapUrl("~/image path here"));

// sorry if the above line doesn't compile; writing from memory, use intellisense to find these classes/methods

// image.Width and image.Height will work here

使用位图获取图像的大小,并调用下面的函数来调整大小

Bitmap myBitmap;
string fileName = "foreverAlone.jpg";
myBitmap = new Bitmap(fileName);

Size newSize = NewImageSize(myBitmap.Height, myBitmap.Width, 100);//myBitMap.Height and myBitMap.Width is how you take the original size
在这里检查位图类

此代码返回图像的新大小,并且图像质量保持不变-不减少-,
FormatSize
参数决定新大小

 public Size NewImageSize(int OriginalHeight, int OriginalWidth, double FormatSize)
        {
            Size NewSize;
            double tempval;

            if (OriginalHeight > FormatSize && OriginalWidth > FormatSize)
            {
                if (OriginalHeight > OriginalWidth)
                    tempval = FormatSize / Convert.ToDouble(OriginalHeight);
                else
                    tempval = FormatSize / Convert.ToDouble(OriginalWidth);

                NewSize = new Size(Convert.ToInt32(tempval * OriginalWidth), Convert.ToInt32(tempval * OriginalHeight));
            }
            else
                NewSize = new Size(OriginalWidth, OriginalHeight); 

            return NewSize;
        } 

如果我设置最大宽度和最大高度,它会按比例限制大小吗?第二个选项无效,因为我无法在MapUrl中指定我的图像的url,它将导致“Ilegal字符异常”,因为我的图像url包含“?”#1是的,大小将成比例;它将小于或等于您设置为最大值的任何大小#2,如果您想这样做,请从路径中去掉额外的字符:
int idx=pathString.IndexOf('?')
,然后
if(idx>0)pathString.Substring(0,idx)
@Andron抱歉,没有必要用标点符号大喊大叫。我在这里用标点符号编辑我的帖子。@RoboLover不要冒犯我,我只是想强调一下。让我们看看我能做些什么,正如另一个答案中提到的,启动位图时有个问题,因为我有一个处理程序,可以给我图像,例如,我不能使用MapUrl@Andron
Server.MapPath
会有帮助吗?我不确定是否会出现同样的问题。没有Server.MapPath在这种情况下不起作用,它会导致“Ilegal字符异常”。您应该在FileViewer.ashx中调整大小,就我所见,处理程序为图像服务,所以为什么不在那里调整它的大小?