缩略图生成类
来源:互联网 发布:c语言如何判断闰年 编辑:程序博客网 时间:2024/06/06 07:27
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;using System.Drawing.Imaging;using System.Drawing.Drawing2D;namespace LinSpace{ /// <summary> /// Date: 2012/8/10 /// Author: Lin_ /// </summary> public class ThumbnailsHandle { /* public int maxThumbnailWidth; public int MaxThumbnailWidth { get { return this.maxThumbnailWidth; } set { this.maxThumbnailWidth = value; } } public int maxThumbnailHeight; public int MaxThumbnailHeight { get { return this.MaxThumbnailHeight; } set { this.MaxThumbnailHeight = value; } } */ /// <summary> /// 按指定大小生成缩略图。比例不足的用透明填充 /// </summary> /// <param name="srcPath"></param> /// <param name="savePath"></param> /// <param name="MaxThumbnailWidth"></param> /// <param name="MaxThumbnailHeight"></param> public static void GenerateImg(string srcPath, string savePath, int maxThumbnailWidth, int maxThumbnailHeight) { Bitmap newBitmap = new Bitmap(maxThumbnailWidth, maxThumbnailHeight); Bitmap oldBitmap = new Bitmap(srcPath); int PdtPicWidth=oldBitmap.Width; int PdtPicHeight=oldBitmap.Height; int generateWidth = maxThumbnailWidth; //比例 float bi = (float)PdtPicWidth / PdtPicHeight; if (maxThumbnailWidth / bi > maxThumbnailHeight) { generateWidth = (int)(maxThumbnailHeight * bi); } else { generateWidth = maxThumbnailWidth; } Graphics g = Graphics.FromImage(newBitmap); SolidBrush sb=new SolidBrush (Color.White); g.FillRectangle(sb, 0, 0, newBitmap.Width, newBitmap.Height); Bitmap drawBitmap = zoomPic(oldBitmap, generateWidth); g.DrawImage(drawBitmap, (newBitmap.Width - drawBitmap.Width) / 2, (newBitmap.Height - drawBitmap.Height) / 2); newBitmap.Save(savePath); drawBitmap.Dispose(); newBitmap.Dispose(); oldBitmap.Dispose(); g.Dispose(); } /// <summary> /// 按比例缩小图片,自动计算高度 /// </summary> /// <param name="strOldPic">源图文件)</param> /// <param name="intWidth">缩小至宽度</param> public static Bitmap ZoomPic(Bitmap objPic, int intWidth) { System.Drawing.Bitmap objNewPic; int intHeight = (int)(((float)intWidth / objPic.Width) * objPic.Height); objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight); return objNewPic; } /// <summary> /// 图片水印 /// </summary> /// <param name="img">图片</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkFilename">水印文件名</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param> public static void AddImageSignPic(System.Drawing.Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { Graphics g = Graphics.FromImage(img); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Image watermark = new Bitmap(watermarkFilename); if (watermark.Height >= img.Height || watermark.Width >= img.Width) { return; } ImageAttributes imageAttributes = new ImageAttributes(); 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 transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) { transparency = (watermarkTransparency / 10.0F); } /* 对图片的透明度的调整可以通过重绘并且对颜色进行调整得到实现 C#中对颜色的调整是通过一个ColorMatrix的对象实现的 这个对象表示一个5X5的矩阵 用于对颜色进行线性的变换 作为一般的理解 只需要指定一个如下的矩阵即可实现对颜色的变换: 1,0,0,0,0 0,1,0,0,0 0,0,1,0,0 0,0,0,透明度,0 0,0,0,0,1 */ float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; //定义颜色矩阵 ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); //利用矩阵进行透明体处理 imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; //位置处理 switch (watermarkStatus) { case 1: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case 2: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)(img.Height * (float).01); break; case 3: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)(img.Height * (float).01); break; case 4: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 5: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 6: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 7: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 8: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 9: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; } g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); //g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); } /// <summary> /// 文字水印 /// </summary> /// <param name="img">图片</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkText">水印文字</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="fontname">字体</param> /// <param name="fontsize">字体大小</param> /// <param name="alpha">alpha值,也代表不透明度0~255</param> public static void AddImageSignText(System.Drawing.Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize, string fontColor, int alpha) { Graphics g = Graphics.FromImage(img); Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel); SizeF crSize; crSize = g.MeasureString(watermarkText, drawFont); float xpos = 0; float ypos = 0; switch (watermarkStatus) { case 1: xpos = (float)img.Width * (float).01; ypos = (float)img.Height * (float).01; break; case 2: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = (float)img.Height * (float).01; break; case 3: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = (float)img.Height * (float).01; break; case 4: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 5: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 6: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 7: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 8: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 9: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).99) - crSize.Height; break; } // g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1); //文字透明度可以创建alpha的颜色 Color col = Color.FromArgb(alpha, System.Drawing.ColorTranslator.FromHtml("#" + fontColor)); g.DrawString(watermarkText, drawFont, new SolidBrush(col), xpos, ypos); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); //bmp.Dispose(); img.Dispose(); } } }
大嘴巴:http://www.bigzb.com
- 图片生成缩略图类
- PHP 生成缩略图类
- php生成缩略图类
- 缩略图生成类
- PHP生成缩略图类
- C生成缩略图的类
- 图像压缩、生成缩略图类
- php生成缩略图的类
- php生成缩略图类,经典
- Java生成缩略图工具类
- 生成缩略图简单的类
- Java-生成缩略图工具类
- 生成缩略图
- 生成缩略图
- 生成缩略图
- 生成缩略图
- 生成缩略图
- 生成缩略图
- 研磨JavaScript系列(三):函数的魔力
- 面试题10:二进制中1的个数
- Spring2.5.6整合hibernate3遇到的异常3
- OPENCV学习心得
- LoadRunner模拟客户端发送JSON格式的请求
- 缩略图生成类
- javascript: Convert special characters to HTML
- 不要一辈子靠技术生存
- web开发日志01--linux apache 结合jsp python
- Eclipse更改svn用户
- ASP.NET MVC 项目结构与核心的DLL
- 【总结】Div-CSS
- 产品36记:第一,有梦想的地方
- Problem G B. Calendar Codeforces Round #183 (Div. 2)