|
Server : Microsoft-IIS/10.0 System : Windows NT EBS-6136 10.0 build 17763 (Windows Server 2016) i586 User : jxgj ( 0) PHP Version : 7.0.33 Disable Function : passthru,exec,system,shell_exec,proc_open,popen,pcntl_exec,socket_bind,stream_socket_server Directory : D:/tmp_aspnet/root/794bf435/a236a820/ |
?#pragma checksum "d:\wwwroot\sznzpj\wwwroot\App_Code\Thumbnail.cs" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "F73806618F03A5C92039878D9D89994C"
#line 1 "d:\wwwroot\sznzpj\wwwroot\App_Code\Thumbnail.cs"
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Web.UI.WebControls;
/// <summary>
///Image 的提要注明
/// </summary>
public class Thumbnail
{
/// <summary>
/// 图片地位
/// </summary>
public enum ImagePosition
{
LeftTop, //左上
LeftBottom, //左下
RightTop, //右上
RigthBottom, //右下
TopMiddle, //顶部居中
BottomMiddle, //底部居中
Center //中心
}
/// 获取一个图片按等比例缩幼后的大幼。
/// </summary>
/// <param name="maxWidth">必要缩幼到的宽度</param>
/// <param name="maxHeight">必要缩幼到的高度</param>
/// <param name="imageOriginalWidth">图片的原始宽度</param>
/// <param name="imageOriginalHeight">图片的原始高度</param>
/// <returns>返回图片按等比例缩幼后的现实大幼</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 对给定的一个图片(Image对象)天生一个指定大幼的缩略图。
/// </summary>
/// <param name="originalImage">原始图片</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
/// <returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size.Empty;
System.Drawing.Image newImage = originalImage;
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
graphics = Graphics.FromImage(newImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
}
catch { }
finally
{
if (graphics != null)
{
graphics.Dispose();
graphics = null;
}
}
return newImage;
}
/// <summary>
/// 对给定的一个图片文件天生一个指定大幼的缩略图。
/// </summary>
/// <param name="originalImage">图片的物理文件地址</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
/// <returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image originalImage = null;
System.Drawing.Image newImage = null;
try
{
originalImage = System.Drawing.Image.FromFile(imageFile);
newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
}
catch { }
finally
{
if (originalImage != null)
{
originalImage.Dispose();
originalImage = null;
}
}
return newImage;
}
/// <summary>
/// 对给定的一个图片文件天生一个指定大幼的缩略图,并将缩略图保留到指定地位。
/// </summary>
/// <param name="originalImageFile">图片的物理文件地址</param>
/// <param name="thumbNailImageFile">缩略图的物理文件地址</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
try
{
newImage.Save(thumbNailImageFile, ImageFormat.Jpeg);
}
catch
{ }
finally
{
newImage.Dispose();
newImage = null;
}
}
/// <summary>
/// 将一个图片的内存流调整为指定大幼,并返回调整后的内存流。
/// </summary>
/// <param name="originalImageStream">原始图片的内存流</param>
/// <param name="newWidth">新图片的宽度</param>
/// <param name="newHeight">新图片的高度</param>
/// <returns>返回调整后的图片的内存流</returns>
//public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight)
//{
// MemoryStream newImageStream = null;
// System.Drawing.Image newImage = Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream), newWidth, newHeight);
// if (newImage != null)
// {
// newImageStream = new MemoryStream();
// newImage.Save(newImageStream, ImageFormat.Jpeg);
// }
// return newImageStream;
//}
/// <summary>
/// 将一个内存流保留为磁盘文件。
/// </summary>
/// <param name="stream">内存流</param>
/// <param name="newFile">指标磁盘文件地址</param>
public static void SaveStreamToFile(Stream stream, string newFile)
{
if (stream == null || stream.Length == 0 || string.IsNullOrEmpty(newFile))
{
return;
}
byte[] buffer = new byte[stream.Length];
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
FileStream fileStream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
}
/// <summary>
/// 对一个指定的图片加上图片水印成效。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterImage">水印图片(Image对象)</param>
public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
{
if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
{
return;
}
Graphics graphics = Graphics.FromImage(originalImage);
int x = originalImage.Width - waterImage.Width - 10;
int y = originalImage.Height - waterImage.Height - 10;
int width = waterImage.Width;
int height = waterImage.Height;
graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
graphics.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 对一个指定的图片加上文字水印成效。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterText">水印文字内容</param>
public static void CreateTextWaterMark(string imageFile, string waterText)
{
if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
Graphics graphics = Graphics.FromImage(originalImage);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);
float x = (float)originalImage.Width - waterTextSize.Width - 10F;
float y = (float)originalImage.Height - waterTextSize.Height - 10F;
graphics.DrawString(waterText, waterTextFont, brush, x, y);
graphics.Dispose();
brush.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 判断上传组件是否蕴含内容。
/// </summary>
/// <param name="fileUpload">ASP.NET 2.0尺度上传组件</param>
/// <returns>若是数据有效,则返回True,不然返回False</returns>
public static bool IsAttachmentValid(FileUpload fileUpload)
{
if (fileUpload != null &&
fileUpload.PostedFile != null &&
!string.IsNullOrEmpty(fileUpload.PostedFile.FileName) &&
fileUpload.PostedFile.ContentLength > 0)
{
return true;
}
return false;
}
/// <summary>
/// 将文件转换为流
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static byte[] SetImageToByteArray(string fileName)
{
byte[] image = null;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open);
FileInfo fileInfo = new FileInfo(fileName);
int streamLength = (int)fs.Length;
image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}
catch
{
return image;
}
}
/// <summary>
/// 增长图片水印
/// </summary>
/// <param name="sourcePicture">源图片文件名</param>
/// <param name="waterImage">水印图片文件名</param>
/// <param name="alpha">通明度(0.1-1.0数值越幼通明度越高)</param>
/// <param name="position">地位</param>
/// <param name="PicturePath" >图片的蹊径</param>
/// <returns>返回天生于指定文件夹下的水印文件名</returns>
public static string DrawImage(string sourcePicture,
string waterImage,
float alpha,
ImagePosition position,
string PicturePath)
{
//
// 判断参数是否有效
//
if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
{
return sourcePicture;
}
//
// 源图片,水印图片全蹊径
//
string sourcePictureName = sourcePicture;
string waterPictureName = waterImage;
string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
//
// 判断文件是否存在,以及类型是否正确
//
if (System.IO.File.Exists(sourcePictureName) == false ||
System.IO.File.Exists(waterPictureName) == false || (
fileSourceExtension != ".gif" &&
fileSourceExtension != ".jpg" &&
fileSourceExtension != ".png") || (
fileWaterExtension != ".gif" &&
fileWaterExtension != ".jpg" &&
fileWaterExtension != ".png")
)
{
return sourcePicture;
}
// 指标图片名称及全蹊径
//
string targetImageName = System.IO.Path.GetFileNameWithoutExtension(sourcePictureName);
string targetImage = sourcePictureName.Replace(targetImageName, "p_" + targetImageName);
//
// 将必要加上水印的图片装载到Image对象中
//
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(sourcePictureName);
//
// 确定其长宽
//
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//
// 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
//
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//
// 设定分辨率
//
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//
// 界说一个画图画面用来装载位图
//
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//
//同样,由于水印是图片,我们也必要界说一个Image来装载它
//
System.Drawing.Image imgWatermark = new Bitmap(waterPictureName);
//
// 获取水印图片的高度和宽度
//
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//SmoothingMode:指定是否将滑润处置(解除锯齿)利用于直线、曲线和已填充区域的边缘。
// 成员名称 注明
// AntiAlias 指定解除锯齿的出现。
// Default 指定不解除锯齿。
// HighQuality 指定高质量、低快率出现。
// HighSpeed 指定高快率、低质量出现。
// Invalid 指定一个无效模式。
// None 指定不解除锯齿。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//
// 第一次描述,将918博天堂底图描述在画图画面上
//
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//
// 与底图一样,我们必要一个位图来装载水印图片。并设定其分辨率
//
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//
// 持续,将水印图片装载到一个画图画面grWatermark
//
Graphics grWatermark = Graphics.FromImage(bmWatermark);
//
//ImageAttributes 对象蕴含有关在出现时若何操作位图和图元文件色彩的信息。
//
ImageAttributes imageAttributes = new ImageAttributes();
//
//Colormap: 界说转换色彩的映射
//
ColorMap colorMap = new ColorMap();
//
//我的水印图被界说成占有绿色布景致的图片被代替成通明
//
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red红色
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色
new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //通明度
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//
// ColorMatrix:界说蕴含 RGBA 空间坐标的 5 x 5 矩阵。
// ImageAttributes 类的若干步骤通过使用色彩矩阵调整图像色彩。
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//
//上面设置完色彩,下面起头设置地位
//
int xPosOfWm;
int yPosOfWm;
switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.Center:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = (phHeight - wmHeight) / 2;
break;
case ImagePosition.LeftBottom:
xPosOfWm = 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.LeftTop:
xPosOfWm = 10;
yPosOfWm = 10;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = 10;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.TopMiddle:
xPosOfWm = (phWidth - wmWidth) / 2;
yPosOfWm = 10;
break;
default:
xPosOfWm = 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
}
//
// 第二次画图,把水印印上去
//
grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm,
yPosOfWm,
wmWidth,
wmHeight),
0,
0,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes);
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
bmPhoto.Dispose();
//
// 保留文件到服务器的文件夹里面
//
imgPhoto.Save(targetImage, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
return targetImage.Replace(PicturePath, "");
}
public bool ThumbnailCallback()
{
return false;
}
//int_Height int_Width 指定高度和指定宽度 input_Imgfile,out_ImgFile为原图片和缩幼后图片的蹊径。
/// <summary>
/// (填充空缺)对给定的一个图片文件天生一个指定大幼的缩略图,并将缩略图保留到指定地位。
/// </summary>
/// <param name="originalImageFile">图片的物理文件地址</param>
/// <param name="thumbNailImageFile">缩略图的物理文件地址</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
public static void MkThumbnail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(originalImageFile);
float New_Width; // 新的宽度
float New_Height; // 新的高度
float Old_Width, Old_Height; //原始高宽
int flat = 0;//标記图片是不是等比
int xPoint = 0;//若果要补白边的话,原图像地点的x,y坐标。
int yPoint = 0;
//判断图片
Old_Width = (float)oldimage.Width;
Old_Height = (float)oldimage.Height;
if ((Old_Width / Old_Height) > ((float)thumMaxWidth / (float)thumMaxHeight)) //当图片太宽的时辰
{
New_Height = Old_Height * ((float)thumMaxWidth / (float)Old_Width);
New_Width = (float)thumMaxWidth;
//此时x坐标不用批改
yPoint = (int)(((float)thumMaxHeight - New_Height) / 2);
flat = 1;
}
else if ((oldimage.Width / oldimage.Height) == ((float)thumMaxWidth / (float)thumMaxHeight))
{
New_Width = thumMaxWidth;
New_Height = thumMaxHeight;
}
else
{
New_Width = (int)oldimage.Width * ((float)thumMaxHeight / (float)oldimage.Height); //太高的时辰
New_Height = thumMaxHeight;
//此时y坐标不用批改
xPoint = (int)(((float)thumMaxWidth - New_Width) / 2);
flat = 1;
}
// ===缩幼图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(new Thumbnail().ThumbnailCallback), IntPtr.Zero);
Bitmap bm = new Bitmap(thumbnailImage);
if (flat != 0)
{
Bitmap bmOutput = new Bitmap(thumMaxWidth, thumMaxHeight);
Graphics gc = Graphics.FromImage(bmOutput);
gc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
gc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低快率出现滑润水平
SolidBrush tbBg = new SolidBrush(Color.White);
gc.FillRectangle(tbBg, 0, 0, thumMaxWidth, thumMaxHeight); //填充为白色
gc.DrawImage(bm, xPoint, yPoint, (int)New_Width, (int)New_Height);
bmOutput.Save(thumbNailImageFile, ImageFormat.Jpeg);
}
else
{
bm.Save(thumbNailImageFile, ImageFormat.Jpeg);
}
oldimage.Dispose();
thumbnailImage.Dispose();
}
/// <summary>
/// 天生清澈缩列图,凭据固定大幼缩减
/// </summary>
/// <param name="sSavePath">原图蹊径</param>
/// <param name="smallImagePath">天生缩列图蹊径</param>
/// <param name="intThumbWidth">缩列图宽度</param>
/// <param name="intThumbHeight">缩列图高度</param>
public static void UpLoadImage(string sSavePath, string smallImagePath, int intThumbWidth, int intThumbHeight)
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sSavePath))
{
//原图宽度和高度
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;
}
//新建一个图板,以最幼等比例压缩大幼绘造原图
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.Black);
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.Black);
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);
}
}
}
}
}
# region 图片处置
/// <summary>
/// 缩放图像
/// </summary>
/// <param name="originalImagePath">图片原始蹊径</param>
/// <param name="thumNailPath">保留蹊径</param>
/// <param name="width">缩放图的宽</param>
/// <param name="height">缩放图的高</param>
/// <param name="model">缩放模式</param>
public static void MakeThumNail4(string originalImagePath, string thumNailPath, int width, int height, string model)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int thumWidth = width; //缩略图的宽度
int thumHeight = height; //缩略图的高度
int x = 0;
int y = 0;
int originalWidth = originalImage.Width; //原始图片的宽度
int originalHeight = originalImage.Height; //原始图片的高度
switch (model)
{
case "HW": //指定高宽缩放,可能变形
break;
case "W": //指定宽度,高度依照比例缩放
thumHeight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高度,宽度依照等比例缩放
thumWidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
{
originalHeight = originalImage.Height;
originalWidth = originalImage.Height * thumWidth / thumHeight;
y = 0;
x = (originalImage.Width - originalWidth) / 2;
}
else
{
originalWidth = originalImage.Width;
originalHeight = originalWidth * height / thumWidth;
x = 0;
y = (originalImage.Height - originalHeight) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight);
//新建一个画板
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量查值法
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低快率出现滑润水平
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以通明布景致填充
graphic.Clear(System.Drawing.Color.Transparent);
//在指定地位并且按指定大幼绘造原图片的指定部门
graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);
try
{
bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}
}
# endregion
}
#line default
#line hidden