请教高手 GDI+ 如何设置缩略图的图片质量?这段代码应该如何改?

lijing3333 2011-10-27 03:51:02

public void CreateMinImage(string originalImagePath, string thumbnailPath, int width, int height, int isThumbnail)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));

int towidth = width;
int toheight = height;

int ow = originalImage.Width;
int oh = originalImage.Height;

if (isThumbnail.Equals(1))
{

if (originalImage.Width / originalImage.Height >= width / height)
{
if (originalImage.Width > width)
{
towidth = width;
toheight = (originalImage.Height * width) / originalImage.Width;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
else
{
if (originalImage.Height > height)
{
toheight = height;
towidth = (originalImage.Width * height) / originalImage.Height;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
}

//新建一个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.HighQualityBicubic;

//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight));

try
{
//以jpg格式保存缩略图
string FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}

}


这是我现在用的。。 但是图片缩略的太小了。。 才8kb 导致图片质量很不好。。。 这段代码如何修改 让缩略的图片质量高一点? 我看网上他们的缩略图片效果都挺好的 也才20kb 请教了!!!
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2011-10-27
  • 打赏
  • 举报
回复
http://blog.csdn.net/kongwei521/article/details/6871515
么看有什么差别。是不是你设置的生成宽高 小了
liukaizxc 2011-10-27
  • 打赏
  • 举报
回复

/// <summary>
/// asp.net上传图片并生成缩略图
/// </summary>
/// <param name="upImage">HtmlInputFile控件</param>
/// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>
/// <param name="sThumbExtension">缩略图的thumb</param>
/// <param name="intThumbWidth">生成缩略图的宽度</param>
/// <param name="intThumbHeight">生成缩略图的高度</param>
/// <returns>缩略图名称</returns>
public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight, string BigName)
{
string sThumbFile = "";
string sFilename = "";
if (upImage.PostedFile != null)
{
HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return "没有选择上传图片";
//获取upImage选择文件的扩展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判断是否为图片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return "图片格式不正确";
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
//检查当前文件夹下是否有同名图片,有则在文件名+1
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + extendName;
}
System.IO.FileStream newFile
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
//以上为上传原图
try
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + BigName)))
{
//原图宽度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight;
//获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
smallWidth = intThumbWidth;
smallHeight = intThumbWidth * height / width;
}
else
{
smallWidth = intThumbHeight * width / height;
smallHeight = intThumbHeight;
}
//判断缩略图在当前文件夹下是否同名称文件存在
file_append = 0;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + extendName;
}
//缩略图保存的绝对路径
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (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(Color.Transparent);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//绘制缩略图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
catch
{
//出错则删除
System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
return "图片格式不正确";
}
//返回缩略图名称
return sThumbFile;
}
return "没有选择图片";
}

我用的是这个缩略图代码
蝶恋花雨 2011-10-27
  • 打赏
  • 举报
回复
你的宽高多少?
lijing3333 2011-10-27
  • 打赏
  • 举报
回复
lijing3333 2011-10-27
  • 打赏
  • 举报
回复
沉的这么快啊!!! 没人啊

62,046

社区成员

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

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

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

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