上传图片并保存缩小后的图片,但是不保存原图能实现吗?
public bool ThumbnailCallback()
{ return false; }
private void SaveImage(string filePath)
{
string fileName = Path.GetExtension(upLoad.PostedFile.FileName).ToLower();
string _filename = DateTime.Now.ToString("yyMMddHHmmss"); System.Drawing.Image image = null, aNewImage = null; //下面是生成缩略图
int newwidth = 0, newheight = 0;
image = System.Drawing.Image.FromFile(filePath);
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); //取高和宽
int phWidth = image.Width;
int phHeight = image.Height; //设置缩略的宽度和高度
if (phWidth > 110)
{
//指定宽度
newwidth = 110;
//根据宽度算出高度
newheight = phHeight * newwidth / phWidth;
}
aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
//将缩略图重新命名并保存
aNewImage.Save(Server.MapPath("~/files/file/") + DateTime.Now.ToString("yyMMddHHmmss") + fileName);
image.Dispose();
}SaveImage(string filePath)里面的filePath可不可以用上传空间里面得到的upLoad.PostedFile.FileName值?
难道只能用服务器端的路径?
public bool ThumbnailCallback()
{ return false; }
private void SaveImage(string filePath)
{
string fileName = Path.GetExtension(upLoad.PostedFile.FileName).ToLower();
string _filename = DateTime.Now.ToString("yyMMddHHmmss"); System.Drawing.Image image = null, aNewImage = null; //下面是生成缩略图
int newwidth = 0, newheight = 0;
image = System.Drawing.Image.FromFile(filePath);
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); //取高和宽
int phWidth = image.Width;
int phHeight = image.Height; //设置缩略的宽度和高度
if (phWidth > 110)
{
//指定宽度
newwidth = 110;
//根据宽度算出高度
newheight = phHeight * newwidth / phWidth;
}
aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
//将缩略图重新命名并保存
aNewImage.Save(Server.MapPath("~/files/file/") + DateTime.Now.ToString("yyMMddHHmmss") + fileName);
image.Dispose();
}SaveImage(string filePath)里面的filePath可不可以用上传空间里面得到的upLoad.PostedFile.FileName值?
难道只能用服务器端的路径?
解决方案 »
- 如何使用 JS 获取带的CheckBox的 TreeView 最后一级节点的值..
- ************ 一条MYSQL 语句不知道哪里错了 **********
- Newtonsoft.json.jsonConvert问题
- session与cookie的区别是什么?
- 如何用visualstudio或dreamweaver导出站点文件列表
- Session的值如何才能转换到HashTable表?
- repeater里面的值如何递增?
- 请教关于COM组件调用
- 设定ASP.NET页面5分钟刷新一次,请教示例
- 创建asp.net项目的新问题
- 新手求dataview样式,,,,,,,
- silverlight 虚拟化 treeview
改成image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(upLoad.FileBytes));就可以了
private static System.Drawing.Bitmap ResizeImage(System.IO.Stream fileStream)
{
int maxW = Information.ReportPhotoMaxWidth;
int maxH = Information.ReportPhotoMaxHeight;
System.Drawing.Bitmap img = new System.Drawing.Bitmap(fileStream);
int w = img.Width;
int h = img.Height;
bool flag = false;
if (w > maxW)
{
flag = true;
h = maxW * h / w;
w = maxW;
}
if (h > maxH)
{
flag = true;
w = maxH * w / h;
h = maxH;
} return flag ? new System.Drawing.Bitmap(img, w, h) : img;
}调用方式
ResizeImage(HttpPostedFile.InputStream);