C# Graphics 生成缩略图

wangxiaofeiwuqiao 2009-03-06 10:06:54
我在页面放一个fileupload控件,ID: File2,一个Button控件,点击Button控件上传图片,目的:上传一个原图后,根据原图生成缩略图,问题:上传是成功了,也生成了缩略图,只有10k,但是缩略图没有图象,请教高手!代码:
protected void Button2_Click(object sender, EventArgs e)
{
//原图1
Random ran = new Random();
string san = ran.Next(1000, 9999).ToString();
string File2_filePath = File2.PostedFile.FileName; //获取上传文件物理路径
string File2_ExtName = File2_filePath.Substring(File2_filePath.LastIndexOf(".") + 1);
string File2_FileName = san + File2_filePath.Substring(File2_filePath.LastIndexOf("\\") + 1);

if (File2.PostedFile.FileName != "")
{
if ((File2_ExtName == "jpg" || File2_ExtName == "jpeg"))
{
//原图
string File2_Path = Server.MapPath(@"~\images\"); //保存到指定路径(展示图片)
File2.PostedFile.SaveAs(File2_Path + File2_FileName); //保存到服务器(展示图片)

//生成缩略图
System.Drawing.Bitmap objNewBitMap = new Bitmap(File2_Path + File2_FileName);

//从指定的 Image 对象创建新 Graphics 对象
Graphics objGraphics = Graphics.FromImage(objNewBitMap);
//清除整个绘图面并以透明背景色填充
objGraphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制 原图片 对象
objGraphics.DrawImage(objNewBitMap, new Rectangle(0, 0, 100, 120));
objNewBitMap.Save(File2_Path + "sl_" + File2_FileName);

}

string str = "insert into tb_Image(ImageBit,ImageBig) values('" + (@"~\images\" + ("sl_" + File2_FileName)) + "','" + (@"~\images\" + (File2_FileName)) + "')";
Base.SqlCom(str);
Response.Write("<script>alert('添加成功')</script>");
}
}

问题可能出在:objGraphics.Clear(Color.Transparent); 但是试了很多方法不行。
...全文
1498 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
jidubushuang 2011-05-05
  • 打赏
  • 举报
回复
violinacl 2009-03-10
  • 打赏
  • 举报
回复
帮顶
wangxiaofeiwuqiao 2009-03-10
  • 打赏
  • 举报
回复
我缩略的图片也有40-50K那么大,是不是也是正常的?
oranrry 2009-03-10
  • 打赏
  • 举报
回复
你如果对图片的质量没有太大的要求的话
你把
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
objGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
不要设成High不就行了
wangxiaofeiwuqiao 2009-03-06
  • 打赏
  • 举报
回复
那位哥哥做过这方面的,帮帮忙。
wangxiaofeiwuqiao 2009-03-06
  • 打赏
  • 举报
回复
谢谢5,6楼,不过如果能在我那里的代码改下最好。
Roc_Lee 2009-03-06
  • 打赏
  • 举报
回复
帮顶,关注
wangxiaofeiwuqiao 2009-03-06
  • 打赏
  • 举报
回复
GetThumbnailImage这个方法我会,效果不好,图片比较模糊,还比较大,所以我考虑用Graphics
the_pain 2009-03-06
  • 打赏
  • 举报
回复

/// <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;

if (ow < towidth && oh < toheight)
{
originalImage.Save(thumbnailPath);
}
else
{

switch (mode.ToUpper())
{
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;
case "AUTO": //自动适应高度
if (ow > oh)
{
//newwidth = 200;
toheight = (int)((double)oh / (double)ow * (double)towidth);
}
else
{
//newheight = 200;
towidth = (int)((double)ow / (double)oh * (double)toheight);
}
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
{

bitmap.Dispose();
g.Dispose();
}
}
originalImage.Dispose();
}
the_pain 2009-03-06
  • 打赏
  • 举报
回复

/// <summary>
/// 上传文件
/// </summary>
/// <param name="fulFile">上传控件 </param>
/// <param name="DirectionName">文件所放的父文件夹 </param>
/// <returns> </returns>
[WebMethod]
public void FileUpLoad(FileUpload fulFile,string DirectionName)
{
string FileName;
string FileSavePath;
FileName = fulFile.FileName;
FileSavePath = Server.MapPath("~/File/");

if (fulFile.HasFile)
{
string name = fulFile.PostedFile.FileName;
FileInfo file = new FileInfo(name);
string newFileName = GetRandomString(10) + file.Extension;
//FileSavePath += DirectionName;
//DirectoryInfo direction = new DirectoryInfo(FileSavePath);
if (!File.Exists(FileSavePath+"\\"+DirectionName))
{
Directory.CreateDirectory(FileSavePath+"\\"+DirectionName);
}
FileSavePath += DirectionName+"\\"+ newFileName;

//string path = Server.MapPath(FileSavePath);
if (!File.Exists(FileSavePath))
{
try
{
fulFile.SaveAs(FileSavePath);
}
catch (Exception Error)
{
HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
return;
}
}
else
{
HttpContext.Current.Response.Write(" <script>alert('该文件已存在') </script>");
return;
}
}
else
{
HttpContext.Current.Response.Write(" <script>alert('请选择要上传的文件') </script>");
return;
}
HttpContext.Current.Response.Write(" <script>alert('文件上传成功') </script>");
}
wanghao3616 2009-03-06
  • 打赏
  • 举报
回复
public void Page_Load(object sender, EventArgs e)
{

System.Drawing.Image image = default(System.Drawing.Image);
System.Drawing.Image aNewImage = default(System.Drawing.Image);
int width = 0;
int height = 0;
int newwidth = 0;
int newheight = 0;
System.Drawing.Image.GetThumbnailImageAbort callb = default(System.Drawing.Image.GetThumbnailImageAbort);

//生成缩略图
image = System.Drawing.Image.FromFile(Server.MapPath("classpic/" + "rs1.jpg"));
width = image.Width;
height = image.height;
if (width > height) {
newwidth = 110;
newheight = image.height / image.Width * newwidth;
}
else {
newheight = 110;
newwidth = image.Width / image.height * newheight;
}

aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
aNewImage.Save(Server.MapPath("smallpic/" + "rs1.gif"));
image.Dispose();

}

给你个生成缩略图的
BossFriday 2009-03-06
  • 打赏
  • 举报
回复
这样的代码,网上很多.
注意点:
1,原图作水印logo的处理
2,对于图片的操作最好在内存中完成,这样对于server的HD耗损会小很多
3,对于图片,最好分析头文件,防止用户改改文件的扩展名,就能上传其他乱七八糟的东西,这样对于站点的安全很重要.

目前我这边写的上传部分,就是考虑这3点.
BossFriday 2009-03-06
  • 打赏
  • 举报
回复
这样的代码,网上很多.
注意点:
1,原图作水印logo的处理
2,对于图片的操作最好在内存中完成,这样对于server的HD耗损会小很多
3,对于图片,最好分析头文件,防止用户改改文件的扩展名,就能上传其他乱七八糟的东西,这样对于站点的安全很重要.

目前我这边写的上传部分,就是考虑这3点.
wangxiaofeiwuqiao 2009-03-06
  • 打赏
  • 举报
回复
那要怎么改?
wanghao3616 2009-03-06
  • 打赏
  • 举报
回复
//清除整个绘图面并以透明背景色填充
objGraphics.Clear(Color.Transparent);


你都清除了 还怎么有啊
Micao_tong 2009-03-06
  • 打赏
  • 举报
回复
可以參考:
http://dotnet.aspx.cc/article/45e7e33c-f149-450e-b5d5-832958c20538/read.aspx

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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