我做了三个页面三个页面中都有个 IMAGE 控件. 现在我写了个 事件
protected void imgpre_show(object sender, EventArgs e)
{
string imgurl = this.Imgpre.ImageUrl;
Bitmap imagebmp;
char x = imgurl[0];
try
{
if (x.ToString() == "h")
{
WebRequest wr = WebRequest.Create(imgurl);
WebResponse res = wr.GetResponse();
imagebmp = new Bitmap(res.GetResponseStream());
}
else
{
imagebmp = new Bitmap(Server.MapPath(imgurl));
}
Single k = imagebmp.Width / imagebmp.Height; this.Imgpre.Width = imagebmp.Width;
this.Imgpre.Height = imagebmp.Height;
if (k > 1)
{
if (imagebmp.Width > 100)
{
this.Imgpre.Width = 100;
this.Imgpre.Height = Convert.ToInt32(100 / k);
}
}
else
{
if (imagebmp.Height > 100)
{
this.Imgpre.Height = 100;
this.Imgpre.Width = Convert.ToInt32(100 / k);
}
}
}
catch
{
}
}目的是在控件显示图像前根把图像按比例缩放到 100PX 之内. 在单独页面运行没什么问题.
现在的问题是, 三个页都有这个控件,所以我想把这个事件定义到 APP_CODE 里的CS文件里
运行时提示错误.1 不存在 this.Imgpre 的定义. 这个可以理解.毕竟不在页上.但是怎么改这一块?2 当前或上下文中 不存在 SERVER 是指这一句
Server.MapPath 在CS文件中不能调用 服务端方法么?求指点
protected void imgpre_show(object sender, EventArgs e)
{
string imgurl = this.Imgpre.ImageUrl;
Bitmap imagebmp;
char x = imgurl[0];
try
{
if (x.ToString() == "h")
{
WebRequest wr = WebRequest.Create(imgurl);
WebResponse res = wr.GetResponse();
imagebmp = new Bitmap(res.GetResponseStream());
}
else
{
imagebmp = new Bitmap(Server.MapPath(imgurl));
}
Single k = imagebmp.Width / imagebmp.Height; this.Imgpre.Width = imagebmp.Width;
this.Imgpre.Height = imagebmp.Height;
if (k > 1)
{
if (imagebmp.Width > 100)
{
this.Imgpre.Width = 100;
this.Imgpre.Height = Convert.ToInt32(100 / k);
}
}
else
{
if (imagebmp.Height > 100)
{
this.Imgpre.Height = 100;
this.Imgpre.Width = Convert.ToInt32(100 / k);
}
}
}
catch
{
}
}目的是在控件显示图像前根把图像按比例缩放到 100PX 之内. 在单独页面运行没什么问题.
现在的问题是, 三个页都有这个控件,所以我想把这个事件定义到 APP_CODE 里的CS文件里
运行时提示错误.1 不存在 this.Imgpre 的定义. 这个可以理解.毕竟不在页上.但是怎么改这一块?2 当前或上下文中 不存在 SERVER 是指这一句
Server.MapPath 在CS文件中不能调用 服务端方法么?求指点
string imgurl;
可以作为类的属性对待,用的时候赋值
----------这一段倒也罢了.主要是这里
this.Imgpre.Width = imagebmp.Width;
this.Imgpre.Height = imagebmp.Height;因为我算完后就对控件 的长宽赋值
如果非要当前页面才能赋值.就郁闷了.