All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
在SQL,Server2005中删除数据库用户和修改SQLServer数据库的登录名_mssql学习_编程技术
用PHP打造超酷的数据饼图_php资料_编程技术
ASP.NET,2.0程序安全的基础知识_.net资料_编程技术
扩展GridView控件(八)——导出为Excel_[Asp.Net教程]
PHP实例:PHP安全编程之加密功能_[PHP教程]
菜鸟的最爱,.NET经典代码汇总(一)_.net资料_编程技术
XML入门教程-使用CSS显示XML_[XML教程]
PHP网页编码问题:任意字符集正常显示网页的方法_php资料_编程技术
WEB标准(XHTML介绍)_[Html教程]
ASP.NET,3.5,Extensions新特性:ASP.NET,Dynamic,Data,体验1_[Asp.Net教程]
Reading number is top 10 pictures
看到这个手速,决定过年就让我家猫帮我抢红包了。。
Poor doll, hand job was caught the currently in effect by his dad
Summer is most suitable for young people to travel in China8
人美胸美腿更美4
某某人向找小三的人宣战了
美女和狗狗1
看到这名字我也是醉了。。。。。。
NeedWallpaper9
大四女生借债隆胸成功
关于海盗的研究2
Download software ranking
Boxer Classic video3
Sora aoi‘s film--Lust fan wall
仙剑奇侠传98版歌曲
Unix video tutorial4
Red cliff
jdk1.5
仙剑奇侠传98硬盘WINXP版
dreamweaver8中文版
C++教程第四版
Boxer's Top ten classic battle8
归海一刀 published in(发表于) 2014/1/30 1:06:57 Edit(编辑)
上传图片生成略缩图“最佳解决”_[Asp.Net教程]

上传图片生成略缩图“最佳解决”_[Asp.Net教程]

上传图片生成略缩图“最佳解决”_[Asp.Net教程]

从用 .Net Web 开发到现在所看到的略缩生成代码都不尽人意,要不太局限,要不失真厉害。

为此写了一个相对完善的函数供大家学习。

其中的 SaveIamge 函数提供了失真解决方法,对于处理过的图片(如加水印……)要求保持高品质可以直接调用。


using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Text.RegularExpressions;

///
// 图片处理。
// (c) 2008 Dao
///

public class Image
{
///
/// 缩略模式。
///

public enum ThumbMode : byte
{
///
/// 完整模式
///

Full = 1,
///
/// 最大尺寸
///

Max
}

///
/// 缩略图。
///

/// 要缩略的图片
/// 要缩放的尺寸
/// 缩略模式
/// 对齐方式
/// 返回已经缩放的图片。
public static Bitmap Thumbnail(Bitmap image, Size size, ThumbMode mode, ContentAlignment contentAlignment)
{
if (!size.IsEmpty && !image.Size.IsEmpty && !size.Equals(image.Size))
{
//先取一个宽比例。
double scale = (double)image.Width / (double)size.Width;
//缩略模式
switch (mode)
{
case ThumbMode.Full:
if (image.Height > image.Width)
scale = (double)image.Height / (double)size.Height;
break;
case ThumbMode.Max:
if (image.Height / scale < size.Height)
scale = (double)image.Height / (double)size.Height;
break;
}
SizeF newSzie = new SizeF((float)(image.Width / scale), (float)(image.Height / scale));
Bitmap result = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(result))
{
g.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), size));
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//对齐方式
RectangleF destRect;
switch (contentAlignment)
{
case ContentAlignment.TopCenter:
destRect = new RectangleF(new PointF(-(float)((newSzie.Width - size.Width) * 0.5), 0), newSzie);
break;
case ContentAlignment.TopRight:
destRect = new RectangleF(new PointF(-(float)(newSzie.Width - size.Width), 0), newSzie);
break;
case ContentAlignment.MiddleLeft:
destRect = new RectangleF(new PointF(0, -(float)((newSzie.Height - size.Height) * 0.5)), newSzie);
break;
case ContentAlignment.MiddleCenter:
destRect = new RectangleF(new PointF(-(float)((newSzie.Width - size.Width) * 0.5), -(float)((newSzie.Height - size.Height) * 0.5)), newSzie);
break;
case ContentAlignment.MiddleRight:
destRect = new RectangleF(new PointF(-(float)(newSzie.Width - size.Width), -(float)((newSzie.Height - size.Height) * 0.5)), newSzie);
break;
case ContentAlignment.BottomLeft:
destRect = new RectangleF(new PointF(0, -(float)(newSzie.Height - size.Height)), newSzie);
break;
case ContentAlignment.BottomCenter:
destRect = new RectangleF(new PointF(-(float)((newSzie.Width - size.Width) * 0.5), -(float)(newSzie.Height - size.Height)), newSzie);
break;
case ContentAlignment.BottomRight:
destRect = new RectangleF(new PointF(-(float)(newSzie.Width - size.Width), -(float)(newSzie.Height - size.Height)), newSzie);
break;
default:
destRect = new RectangleF(new PointF(0, 0), newSzie);
break;
}
g.DrawImage(image, destRect, new RectangleF(new PointF(0F, 0F), image.Size), GraphicsUnit.Pixel);
image.Dispose();
}
return result;
}
else
return image;
}

///
/// 保存图片。
///

/// 要保存的图片
/// 品质(1L~100L之间,数值越大品质越好)
/// 保存路径
public static void SaveIamge(Bitmap image, long quality, string filename)
{
using (EncoderParameters encoderParams = new EncoderParameters(1))
{
using (EncoderParameter parameter = (encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality)))
{
ImageCodecInfo encoder = null;
//取得扩展名
string ext = Path.GetExtension(filename);
if (string.IsNullOrEmpty(ext))
ext = ".jpg";
//根据扩展名得到解码、编码器
foreach (ImageCodecInfo codecInfo in ImageCodecInfo.GetImageEncoders())
{
if (Regex.IsMatch(codecInfo.FilenameExtension, string.Format(@"(;|^)\*\{0}(;|)", ext), RegexOptions.IgnoreCase))
{
encoder = codecInfo;
break;
}
}
Directory.CreateDirectory(Path.GetDirectoryName(filename));
image.Save(filename, encoder, encoderParams);
}
}
}

///
/// 保存图片。
///

/// 要保存的流
/// 品质(1L~100L之间,数值越大品质越好)
/// 保存路径
public static void SaveIamge(Stream stream, long quality, string filename)
{
using (Bitmap bmpTemp = new Bitmap(stream))
{
SaveIamge(bmpTemp, quality, filename);
}
}
}

调用方法如下:

using (Bitmap bmpAvatar = Image.Thumbnail(new Bitmap(/*<流,FileUpload 控件的 PostedFile.InputStream 属性>*/), new Size(48, 48), Image.ThumbMode.Full, ContentAlignment.MiddleCenter))
{
Image.SaveIamge(bmpAvatar, 95L, Server.MapPath("~/upfiles/Avatar.jpg"));
}

来源:http://www.cnblogs.com/dao





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.