请教高手 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 请教了!!!
...全文
142 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
  • 打赏
  • 举报
回复
沉的这么快啊!!! 没人啊
源码直接下载地址: https://pan.quark.cn/s/a4b39357ea24 在信息技术领域中,开发一个模仿微信客服交流平台的界面是一项普遍的需求,尤其是在构建企业级沟通工具时。这个名为"仿微信客服交流界面(具备聊天信息本地数据库存储功能)"的项目,致力于为用户创造一个类似于微信的互动体验,并且已经完成了聊天信息在本地数据库中的保存功能,以此保障信息的安全性和可恢复性。另外,项目还包含了网络传输的代码,旨在帮助开发者更好地理解和将此功能整合进自己的应用程序中。 现在让我们深入探究聊天界面的构建过程。微信客服交流界面通常由以下几个核心组件构成:用户个人照片、昵称展示、消息展示气泡、时间标记、输入区域以及发送控制键。这些组件需要经过细致的排布,以确保界面既清晰又便于使用。在用户界面设计方面,一般会采用 Material Design 或者 iOS 的 Human Interface Guidelines 来设计符合平台标准的界面。源代码文件 ChatUIDemo 可能包含了这一界面实现的代码,开发者可以通过查看和调整这个文件来个性化自己的聊天界面。 聊天信息在本地数据库中的存储是一项核心功能。在该项目中,或许选用了SQLite作为轻量级数据库,因为它易于集成,支持事务处理,适合存储结构化的数据,例如用户标识符、接收者标识符、消息内容、发送时刻等。通过运用SQL指令,能够执行数据的增加、删除、修和查询操作,从而确保聊天记录的完整性和一致性。开发者可能需要关注如何将新接收到的消息添加到数据库中,以及如何从数据库中获取历史记录并在界面上进行展示。 在网络传输方面,可能通过HTTP或HTTPS协议来实现,并且使用了诸如AFNetworking(iOS)或O...
内容概要:本文围绕虚拟同步发电机(VSG)接入弱电网的序阻抗建模与稳定性分析开展研究,基于Matlab/Simulink平台搭建详细的仿真模型,系统复现并验证相关理论方法。研究重点包括VSG在弱电网条件下的正负序阻抗特性建模、基于小信号分析的扫频法建模流程、系统阻抗交互特性及潜在的失稳机理分析。通过具体仿真案例,深入探讨了VSG控制参数对系统稳定性的影响,旨在为新能源并网系统的稳定运行提供理论依据与技术支撑。该内容属于电力电子与电力系统稳定性交叉领域的前沿课题,具有重要的学术价值与工程应用前景。; 适合人群:具备电力系统分析、电力电子变换器控制等基础知识,熟悉Matlab/Simulink仿真环境,从事新能源并网、微电网控制、电力系统稳定性研究的研究生、科研人员及工程师;有志于复现高水平期刊论文中阻抗建模与稳定性分析方法的技术开发者。; 使用场景及目标:① 掌握虚拟同步发电机在弱电网中的序阻抗建模理论与实现方法;② 理解并实践基于扫频法的小信号稳定性分析全过程;③ 应用于构网型变流器、虚拟同步机等先进并网技术的稳定性研究与仿真验证。; 阅读建议:建议结合所提供的Simulink仿真模型与技术资料,按照文档结构循序渐进地学习,重点关注建模原理、仿真参数设置与结果分析过程,同时参考链接中的完整资源进行代码调试与深入探究。

62,272

社区成员

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

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

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

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