C#缩略图问题

yongfang1986 2010-11-11 06:37:34
test.aspx文件:

<img src='/cenep/biad/img.aspx?attachPath=/imgUpload/img_jnt.jpg&maxwidth=100' border='0'/>

我想实现的是将/imgUpload/img_jnt.jpg这个图片进行缩小后再再这个位置显示,img.aspx里面代码已经实现将这个图片缩小,并生成缩略图放到新的目录下。怎么可以显示出来?按照我的代码,这个位置引不进图片。
下面是img.aspx.cs文件

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 获取图片路径
string attachPath = Request.QueryString["attachPath"].ToString();
// 初始化最大宽度和高度
float maxwidth = 0;
float maxheight = 0;
// 获取传过来的最大宽度
if (Request.QueryString["maxwidth"] != null)
{
maxwidth = float.Parse(Request.QueryString["maxwidth"].ToString());
}
// 获取传过来的最大高度
if (Request.QueryString["maxheight"] != null)
{
maxheight = float.Parse(Request.QueryString["maxheight"].ToString());
}
// 调用图片出来函数
BreviaryImage(Server.MapPath(attachPath), maxwidth, maxheight);
}
}

/// <summary>
/// 图片处理
/// </summary>
/// <param name="ImagePath">图片服务器路径</param>
/// <param name="maxwidth">最大宽度</param>
/// <param name="maxheight">最大高度</param>
protected void BreviaryImage(string ImagePath, float maxwidth, float maxheight)
{
// 获取图片对象
using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath))
{
// 取得图片名字
string imageName = ImagePath.Substring(ImagePath.LastIndexOf("\\") + 1);
float width = image.Width;
float height = image.Height;
// 图片宽高比例
float widthheight = width / height;
// 图片高宽比例
float heightwidth = height / width;
// 新图片的宽高
float newwidth = 0;
float newheight = 0;
if (maxwidth == 0)
{
maxwidth = width;
}
if (maxheight == 0)
{
maxheight = height;
}
// 宽超过设置的最大宽度时
if (width > maxwidth)
{
newwidth = maxwidth;
newheight = maxwidth / heightwidth;
}
// 高超过设置的最大高度时
if (height > maxheight)
{
newheight = maxheight;
newwidth = maxheight / widthheight;
}
// 缩略图目录
string FilePath = "/BreviaryImage/";
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
// 保存缩略图
using (System.Drawing.Image aNewImage = image.GetThumbnailImage(Convert.ToInt32(newwidth), Convert.ToInt32(newheight), null, IntPtr.Zero))
{
aNewImage.Save(Server.MapPath(FilePath) + imageName);
}
}
}


希望各位大侠帮忙看看,谢谢
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yongfang1986 2010-11-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aofengdaxia 的回复:]
汗,你引用的是'/cenep/biad/img.aspx这个页面,把这个页面当做图像了。但是你在这个页面什么都没做,只是在一个新的地方生成了缩略图。
你可以在img标签中直接引用缩略图的位置。也可以在img.aspx中通过Response把图片的内容在内存中直接输出。
[/Quote]
谢谢了。可以显示了。
rczjp 2010-11-11
  • 打赏
  • 举报
回复
string FilePath = "/BreviaryImage/";

你的不是放在这里面吗,那显示的时候的路径也是这个才对啊
aofengdaxia 2010-11-11
  • 打赏
  • 举报
回复
Response.WriteFile
可以用使用这个。
aofengdaxia 2010-11-11
  • 打赏
  • 举报
回复
汗,你引用的是'/cenep/biad/img.aspx这个页面,把这个页面当做图像了。但是你在这个页面什么都没做,只是在一个新的地方生成了缩略图。
你可以在img标签中直接引用缩略图的位置。也可以在img.aspx中通过Response把图片的内容在内存中直接输出。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧