using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace TEWeb.Commond
{
public static class PhotoUtils
{
/**/
///
/// ========================================================
/// 2009-7-3
/// Tclywork Studio
/// tclywork@163.com
/// http://space.itpub.net/54654
/// 图片缩略图处理类.
/// ========================================================
///
// *** Jpeg Format ***
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public static void SaveToJpeg(Bitmap bitmap, string filePath, long qualityPercent)
{
ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder encoder = Encoder.Quality;
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(encoder, qualityPercent);
bitmap.Save(filePath, imageCodecInfo, encoderParameters);
}
// *** Thumbnails ***
public static Bitmap CreateThumbnail(System.Drawing.Image image, int thumbnailSize)
{
int w;
int h;
if (image.Width > image.Height)
{
h = thumbnailSize;
w = (int)((image.Width * (float)h) / (float)image.Height);
}
else
{
w = thumbnailSize;
h = (int)((image.Height * (float)w) / (float)image.Width);
}
Bitmap newImage = new Bitmap(thumbnailSize, thumbnailSize, PixelFormat.Format24bppRgb);
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(image, new Rectangle(0, 0, w, h));
}
return newImage;
}
///
/// 生成缩略图
///
/// 源图路径(物理路径)
/// 缩略图路径(物理路径)
/// 缩略图宽度
/// 缩略图高度
/// 生成缩略图的方式
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
if (!File.Exists(originalImagePath))
{
thumbnailPath = originalImagePath;
return;
}
System.Drawing.Image riginalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int w = originalImage.Width;
int h = originalImage.Height;
switch (mode)
{
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)
{
h = originalImage.Height;
w = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
w = originalImage.Width;
h = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
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(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
///
/// A better alternative to Image.GetThumbnail. Higher quality but slightly slower
///
///
///
///
///
public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
{
// return the source image if it's smaller than the designated thumbnail
if (source.Width < thumbWi && source.Height < thumbHi) return source;
System.Drawing.Bitmap ret = null;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
if (maintainAspect)
{
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = thumbWi;
hi = (int)(source.Height * ((decimal)thumbWi / source.Width));
}
else
{
hi = thumbHi;
wi = (int)(source.Width * ((decimal)thumbHi / source.Height));
}
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
}
}
catch
{
ret = null;
}
/* 压缩图片
////Configure JPEG Compression Engine
//System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
//long[] quality = new long[1];
//quality[0] = 75;
//System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
//encoderParams.Param[0] = encoderParam;
//System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
//System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
//for (int x = 0; x < arrayICI.Length; x++)
//{
// if (arrayICI[x].FormatDescription.Equals("JPEG"))
// {
// jpegICI = arrayICI[x];
// break;
// }
//}
////myThumbnail.Save(Path.Combine(SavePathThumb, fileName), jpegICI, encoderParams);
////myThumbnail.Dispose();
* */
return ret;
}
public static void CreateThumbnailFile(string filePath, int thumbnailSize, string thumbnailFilePath, long qualityPercent)
{
if (File.Exists(filePath))
{
using (FileStream stream = new FileStream(filePath, FileMode.Open ))
{
using (System.Drawing.Image image =
System.Drawing.Image.FromStream(stream))
{
if (image.Width == thumbnailSize && image.Height == thumbnailSize)
{
File.Copy(filePath, thumbnailFilePath, true);
}
else
{
using (Bitmap bitmap =
CreateThumbnail(image, thumbnailSize))
{
SaveToJpeg(bitmap, thumbnailFilePath, qualityPercent);
}
}
}
}
}
}
public static void CreateThumbnailFile(string filePath, int thumbnailSize,string thumbnailFilePath)
{
CreateThumbnailFile(filePath, thumbnailSize, thumbnailFilePath, 90L);
}
// 按模版比例生成缩略图(以流的方式获取源文件)
//生成缩略图函数
//顺序参数:源图文件流、缩略图存放地址、模版宽、模版高
//注:缩略图大小控制在模版区域内
public static void MakeSmallImg(string strImageFile, string strThumbnailFile, System.Double templateWidth, System.Double templateHeight)
{
//从文件取得图片对象,并使用流中嵌入的颜色管理信息
System.Drawing.Image myImage = System.Drawing.Image.FromFile(strImageFile, true);
//缩略图宽、高
System.Double newWidth = myImage.Width, newHeight = myImage.Height;
//if (myImage.Width > templateWidth) //如果图片的宽度大于模板宽度
//{
// if (myImage.Height > templateHeight) //如果图片的高度大于模板高度
// {
// if (myImage.Height / templateHeight < myImage.Width / templateWidth) //确定缩略的原则,是宽度还是高度,采用比率小的为准
// {
// }
// else
// }
//}
//else
//{
//}
//宽大于模版的横图
if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
{
if (myImage.Width > templateWidth)
{
//宽按模版,高按比例缩放
newWidth = templateWidth;
newHeight = myImage.Height * (newWidth / myImage.Width);
}
}
//高大于模版的竖图
else
{
if (myImage.Height > templateHeight)
{
//高按模版,宽按比例缩放
newHeight = templateHeight;
newWidth = myImage.Width * (newHeight / myImage.Height);
}
}
//取得图片大小
System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一个画板
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.White);
//在指定位置画图
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存缩略图
bitmap.Save(strThumbnailFile, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
}
}