fu是个上传控件,因为在后台里做了一个上传预览,所以fu是存在session中的,是想把上传的图片变成固定的大小
之前做过这样一个测试 记忆中可以实现
但现在上传之后的图片成了黑黑的一片
代码是这样的
System.Drawing.Image image = System.Drawing.Image.FromStream(fu.PostedFile.InputStream);
int width = image.Width;
int heigh = image.Height;
int max = heigh;
if (width != 358 || heigh != 358)
{
try
{
System.Drawing.Image newImg;
string n = DateTime.Now.Ticks.ToString();
fileName = "MuProductImages/" + n + geshi;
serverpath1 = serverpath1 + n + geshi; ; //如果存在这个图片就删除
if (File.Exists(serverpath1))
{
File.Delete(serverpath1);
}
if (width != heigh)
{
//保存为宽,高为358 的新图片
newImg = new Bitmap(image, 358, 358);
}
else
{
newImg = new Bitmap(358, 358);
}
//保存
newImg.Save(serverpath1,ImageFormat.Jpeg);
return true;
}
catch (Exception ex)
{
Response.Write("<script >alert('图片缩小失败!');</script>");
throw ex;
}
}
else
{
string n = DateTime.Now.Ticks.ToString();
fileName = "MuProductImages/" + n + geshi;
serverpath1 = serverpath1 + n + geshi;
fu.SaveAs(serverpath1);
return true;
}
在线等待
之前做过这样一个测试 记忆中可以实现
但现在上传之后的图片成了黑黑的一片
代码是这样的
System.Drawing.Image image = System.Drawing.Image.FromStream(fu.PostedFile.InputStream);
int width = image.Width;
int heigh = image.Height;
int max = heigh;
if (width != 358 || heigh != 358)
{
try
{
System.Drawing.Image newImg;
string n = DateTime.Now.Ticks.ToString();
fileName = "MuProductImages/" + n + geshi;
serverpath1 = serverpath1 + n + geshi; ; //如果存在这个图片就删除
if (File.Exists(serverpath1))
{
File.Delete(serverpath1);
}
if (width != heigh)
{
//保存为宽,高为358 的新图片
newImg = new Bitmap(image, 358, 358);
}
else
{
newImg = new Bitmap(358, 358);
}
//保存
newImg.Save(serverpath1,ImageFormat.Jpeg);
return true;
}
catch (Exception ex)
{
Response.Write("<script >alert('图片缩小失败!');</script>");
throw ex;
}
}
else
{
string n = DateTime.Now.Ticks.ToString();
fileName = "MuProductImages/" + n + geshi;
serverpath1 = serverpath1 + n + geshi;
fu.SaveAs(serverpath1);
return true;
}
在线等待
解决方案 »
- 关于怎样实现循环的问题,一定要帮我 急...
- aspnetpager分页Repeater 怎样区分不同的数据源
- 关于使用BackgroundWorker多条线程如何控制暂完成和暂停
- http://localhost无法解析 急急急!
- 怎样防止页面刷新重复的执行page_load函数,在线等,急!急!急!拜托了!!!
- 请问用风讯CMS(dotnetCMS)免费版 制作企业网站 被追究法律责任吗
- 请教,用OWC生成曲线图。怎么生成多根曲线图?
- 高分求救xmlhttprequest控制session的问题
- DATAGRID更新出错
- 请教各位大虾:怎样在 DataGrid 中用 CheckBox 进行批量删除?
- 如何将数据存入内存,asp.net每次访问直接到内存去取,而不用读数据库
- 问一下简单的异步handler
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "CUT"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
http://www.cnblogs.com/insus/articles/2060601.html
大神,我找到原因了,之前是从文件流里读取的,因为从session里读取的fileupload的stream没有值,所以创建图片都是空的或黑的,但是从物理文件读取并创建缩略图,图片的体积没变小,而且变大了2倍,这个太纠结了,有什么方法能让图片体积变小吗
你要么用我给你的方法调用。要么你自己配置下图片质量
System.Drawing.Drawing2D.InterpolationMode属性
System.Drawing.Drawing2D.SmoothingMode属性