随笔-38  评论-121  文章-10  trackbacks-3

[GDI+]如何制作出高质量的缩略图

如何制作出高质量的缩略图是个关键的因素,最近项目中遇到了类似需要解决的问题。
一般情况下,我们制作成的缩略图都会保存为占用空间比较小的Jpeg类型,在使用GetThumbnail方法制作成的缩略图质量感觉不理想,
如何才能保证在压缩比例最优化的情况下产生高质量的缩略图呢,经过查阅相关资料,
发现在Graphics 对象的 InterpolationMode 属性中可以产生不同质量模式的缩放图,看到这里了,不再是缩略图,而是缩放图,就是说放大的时候也可以使用。
Graphics 对象的 InterpolationMode 属性枚举定义了几种模式,列表如下:
NearestNeighbor
Bilinear
HighQualityBilinear
Bicubic
HighQualityBicubic
从名字上就可以识别NearestNeighbor 是质量最差的模式,HighQualityBicubic 是质量最好的模式了,我们借此属性看看生成的图片怎么样吧,下段代码摘自网络,大家可以把下面的函数拿去使用,这里采用了HighQualityBilinear 。

代码引用地址:http://www.bobpowell.net/highqualitythumb.htm
    Public Function GenerateThumbnail(original As Image, percentage As IntegerAs Image

     
If percentage < 1 Then

      
Throw New Exception("Thumbnail size must be aat least 1% of the original size")

     
End If

     
Dim tn As New Bitmap(CInt(original.Width * 0.01F * percentage), _

                          
CInt(original.Height * 0.01F * percentage))

     
Dim g As Graphics = Graphics.FromImage(tn)

     g.InterpolationMode 
= InterpolationMode.HighQualityBilinear 

     g.DrawImage(original, 
New Rectangle(00, tn.Width, tn.Height), _

                 
00, original.Width, original.Height, GraphicsUnit.Pixel)

     g.Dispose()

     
Return CType(tn, Image)

    
End Function
 

posted on 2005-01-26 16:46 张锋 阅读(3956) 评论(9) 编辑 收藏

评论:
#1楼 2005-01-26 17:26 | mikespook      
呵呵,和原来API中的SetStretchBltMode异曲同工啊~~~不错,不错~~~
 回复 引用 查看   
#2楼 2005-01-26 17:40 | Ariel Y.      
再提供一些:
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

 回复 引用 查看   
#3楼 2005-01-26 20:48 | Ariel Y.      
从别处Copy来的代码最好引用一下吧
http://www.bobpowell.net/highqualitythumb.htm

 回复 引用 查看   
#4楼 2005-01-26 21:13 | idior      
这个不太厚道
 回复 引用 查看   
#5楼[楼主] 2005-01-27 09:38 | 阿好.NET      
代码应经添加上地址引用,谢 Ariel Y.
 回复 引用 查看   
#6楼 2005-03-01 16:41 | bg[未注册用户]
阿是新手!想学习一些GDI+的知识!谢谢请教!

1.如何下载GDI+控件?
2.如何获得一些关于GDI+的帮助?

谢谢!

 回复 引用   
#7楼 2005-06-27 15:57 | 蛙蛙池塘      
晕,你也在这里呀
 回复 引用 查看   
#8楼 2005-08-09 02:44 | myx[未注册用户]
谢谢~~

刚开始我是这样生成的:
Bitmap b = new Bitmap(intwidth, intheight, PixelFormat.Format48bppRgb);

但这样生成的图有点走样~~我把这
g.InterpolationMode=InterpolationMode.HighQualityBicubic; 一加上就一切OK了~~~真好~~

还有那percentage其实不用这么写~~一般生成的小图都是固定长与宽的。用这%不要确定这图的大小了~~其实也就是在这自己算出来~~

Dim tn As New Bitmap(CInt(original.Width * 0.01F * percentage), _

CInt(original.Height * 0.01F * percentage))

自己把这宽与高算出来:
int intwidth, intheight;
if(oImg.Width > oImg.Height)
{
if(oImg.Width>iWidth)
{
intwidth = iWidth;
intheight = (oImg.Height * iWidth) / oImg.Width;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
else
{
if(oImg.Height > iHeight)
{
intwidth = (oImg.Width * iHeight) / oImg.Height;
intheight = iHeight;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}



 回复 引用   
#9楼 2006-11-30 10:13 | aaa[匿名][未注册用户]
十 sgfsdgsg
 回复 引用   
昵称:张锋
园龄:7年9个月
粉丝:1
关注:0
<2005年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

搜索

 
 

常用链接

随笔分类

随笔档案

最爱网址

积分与排名

  • 积分 - 92111
  • 排名 - 1114

评论排行榜

推荐排行榜