<%@ Page language="c#" %> <%@ Import Namespace="Seaskyer.Strings" %> <%@ Import Namespace="Seaskyer.WebApp.Utility" %> <% WebGDI.MakeSpamImageGen( "SignCode", Function.Str.RandomNUM(5) );%>
生成验证码类:
// Type: Seaskyer.WebApp.Utility.WebGDI // Assembly: Seaskyer.WebApp.Utility, Version=1.2.10.14947, Culture=neutral, PublicKeyToken=c12b5a19434f2d93 // Assembly location: C:\Users\Administrator\Downloads\1ccfff8f01f0\skyNews12\海天人.Net新闻系统(skyNews) v1.2\bin\Seaskyer.WebApp.Utility.dll using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Web; namespace Seaskyer.WebApp.Utility { /// <summary> /// WebGDI : 文字图片、水印图片。 /// /// </summary> public class WebGDI { /// <summary> /// 生成缩微图,并加上阴影文字 /// /// </summary> /// <param name="Width">生成缩微图的宽度</param><param name="Height">生成缩微图的高度</param><param name="SourceImg"/><param name="Text"/><param name="Left"/><param name="Top"/><param name="font">new Font("Fixedsys", 9, FontStyle.Regular)</param> public static void GetThumbnailImage(int Width, int Height, string SourceImg, string Text, int Left, int Top, Font font) { string filename = SourceImg + ".jpg"; Image image = Image.FromFile(SourceImg); Image thumbnailImage = image.GetThumbnailImage(Width, Height, new Image.GetThumbnailImageAbort(WebGDI.ThumbnailCallback), IntPtr.Zero); HttpContext.Current.Response.Clear(); Bitmap bitmap = new Bitmap(thumbnailImage); Graphics graphics = Graphics.FromImage((Image) bitmap); graphics.DrawString(Text, font, (Brush) new SolidBrush(Color.Black), (float) (Left - 1), (float) (Top + 2), new StringFormat(StringFormatFlags.DirectionVertical)); graphics.DrawString(Text, font, (Brush) new SolidBrush(Color.White), (float) Left, (float) Top, new StringFormat(StringFormatFlags.DirectionVertical)); bitmap.Save(filename, ImageFormat.Jpeg); thumbnailImage.Dispose(); image.Dispose(); bitmap.Dispose(); graphics.Dispose(); } private static bool ThumbnailCallback() { return true; } /// <summary> /// 在一张图片的指定位置处加入水印文字 /// /// </summary> /// <param name="SourceImage">指定源图片的绝对路径</param><param name="Text">指定文本</param><param name="fontFamily">文本字体</param><param name="textPos">指定位置</param><param name="SaveImage">保存图片的绝对路径</param> public static void GetWaterMarkTextImage(string SourceImage, string Text, string fontFamily, wmPosition textPos, string SaveImage) { Image image1 = Image.FromFile(SourceImage); int width = image1.Width; int height = image1.Height; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); bitmap.SetResolution(image1.HorizontalResolution, image1.VerticalResolution); Graphics graphics = Graphics.FromImage((Image) bitmap); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawImage(image1, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel); int[] numArray = new int[7] { 16, 14, 12, 10, 8, 6, 4 }; Font font = (Font) null; SizeF sizeF = new SizeF(); for (int index = 0; index < 7; ++index) { font = new Font(fontFamily, (float) numArray[index], FontStyle.Bold); sizeF = graphics.MeasureString(Text, font); if ((int) (ushort) sizeF.Width < (int) (ushort) width) break; } int num = (int) ((double) height * 0.05); float y = (float) (height - num) - sizeF.Height / 2f; float x = (float) (width / 2); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; SolidBrush solidBrush1 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); graphics.DrawString(Text, font, (Brush) solidBrush1, new PointF(x + 1f, y + 1f), format); SolidBrush solidBrush2 = new SolidBrush(Color.FromArgb(153, (int) byte.MaxValue, (int) byte.MaxValue, (int) byte.MaxValue)); graphics.DrawString(Text, font, (Brush) solidBrush2, new PointF(x, y), format); graphics.Dispose(); image1.Dispose(); Image image2 = (Image) bitmap; image2.Save(SaveImage, ImageFormat.Jpeg); bitmap.Dispose(); image2.Dispose(); } /// <summary> /// 在一张图片的指定位置处加入一张具有水印效果的图片 /// /// </summary> /// <param name="SourceImage">指定源图片的绝对路径</param><param name="WaterMarkImage">指定水印图片的绝对路径</param><param name="wmPos">指定位置</param><param name="SaveImage">保存图片的绝对路径</param> public static void GetWaterMarkPicImage(string SourceImage, string WaterMarkImage, wmPosition wmPos, string SaveImage) { Image image1 = Image.FromFile(SourceImage); int width1 = image1.Width; int height1 = image1.Height; Bitmap bitmap1 = new Bitmap(width1, height1, PixelFormat.Format24bppRgb); bitmap1.SetResolution(image1.HorizontalResolution, image1.VerticalResolution); Graphics graphics1 = Graphics.FromImage((Image) bitmap1); graphics1.SmoothingMode = SmoothingMode.AntiAlias; graphics1.DrawImage(image1, new Rectangle(0, 0, width1, height1), 0, 0, width1, height1, GraphicsUnit.Pixel); Image image2 = (Image) new Bitmap(WaterMarkImage); int width2 = image2.Width; int height2 = image2.Height; Bitmap bitmap2 = new Bitmap((Image) bitmap1); bitmap2.SetResolution(image1.HorizontalResolution, image1.VerticalResolution); Graphics graphics2 = Graphics.FromImage((Image) bitmap2); ImageAttributes imageAttr = new ImageAttributes(); ColorMap[] map = new ColorMap[1] { new ColorMap() { OldColor = Color.FromArgb((int) byte.MaxValue, 0, (int) byte.MaxValue, 0), NewColor = Color.FromArgb(0, 0, 0, 0) } }; imageAttr.SetRemapTable(map, ColorAdjustType.Bitmap); float[][] newColorMatrix1 = new float[5][]; float[][] numArray1 = newColorMatrix1; int index1 = 0; float[] numArray2 = new float[5]; numArray2[0] = 1f; float[] numArray3 = numArray2; numArray1[index1] = numArray3; float[][] numArray4 = newColorMatrix1; int index2 = 1; float[] numArray5 = new float[5]; numArray5[1] = 1f; float[] numArray6 = numArray5; numArray4[index2] = numArray6; float[][] numArray7 = newColorMatrix1; int index3 = 2; float[] numArray8 = new float[5]; numArray8[2] = 1f; float[] numArray9 = numArray8; numArray7[index3] = numArray9; float[][] numArray10 = newColorMatrix1; int index4 = 3; float[] numArray11 = new float[5]; numArray11[3] = 0.3f; float[] numArray12 = numArray11; numArray10[index4] = numArray12; float[][] numArray13 = newColorMatrix1; int index5 = 4; float[] numArray14 = new float[5]; numArray14[4] = 1f; float[] numArray15 = numArray14; numArray13[index5] = numArray15; ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix1); imageAttr.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x = width1 - width2 - 10; int y = 10; graphics2.DrawImage(image2, new Rectangle(x, y, width2, height2), 0, 0, width2, height2, GraphicsUnit.Pixel, imageAttr); image1.Dispose(); Image image3 = (Image) bitmap2; graphics1.Dispose(); graphics2.Dispose(); bitmap1.Dispose(); image3.Save(SaveImage, ImageFormat.Jpeg); image3.Dispose(); image2.Dispose(); bitmap2.Dispose(); } /// <summary> /// 在一张图片的指定位置处加入一张具有水印效果的图片和一段文本 /// /// </summary> /// <param name="SourceImage"/><param name="WaterMarkImage"/><param name="Text"/><param name="fontFamily"/><param name="wmPos"/><param name="textPos"/><param name="SaveImage"/> public static void GetWarterMarkPicTextImage(string SourceImage, string WaterMarkImage, string Text, string fontFamily, wmPosition wmPos, wmPosition textPos, string SaveImage) { Image image1 = Image.FromFile(SourceImage); int width1 = image1.Width; int height1 = image1.Height; Bitmap bitmap1 = new Bitmap(width1, height1, PixelFormat.Format24bppRgb); bitmap1.SetResolution(image1.HorizontalResolution, image1.VerticalResolution); Graphics graphics1 = Graphics.FromImage((Image) bitmap1); graphics1.SmoothingMode = SmoothingMode.AntiAlias; graphics1.DrawImage(image1, new Rectangle(0, 0, width1, height1), 0, 0, width1, height1, GraphicsUnit.Pixel); int[] numArray1 = new int[7] { 16, 14, 12, 10, 8, 6, 4 }; Font font = (Font) null; SizeF sizeF = new SizeF(); for (int index = 0; index < 7; ++index) { font = new Font(fontFamily, (float) numArray1[index], FontStyle.Bold); sizeF = graphics1.MeasureString(Text, font); if ((int) (ushort) sizeF.Width < (int) (ushort) width1) break; } int num = (int) ((double) height1 * 0.05); float y1 = (float) (height1 - num) - sizeF.Height / 2f; float x1 = (float) (width1 / 2); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; SolidBrush solidBrush1 = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); graphics1.DrawString(Text, font, (Brush) solidBrush1, new PointF(x1 + 1f, y1 + 1f), format); SolidBrush solidBrush2 = new SolidBrush(Color.FromArgb(100, (int) byte.MaxValue, (int) byte.MaxValue, (int) byte.MaxValue)); graphics1.DrawString(Text, font, (Brush) solidBrush2, new PointF(x1, y1), format); Image image2 = (Image) new Bitmap(WaterMarkImage); int width2 = image2.Width; int height2 = image2.Height; Bitmap bitmap2 = new Bitmap((Image) bitmap1); bitmap2.SetResolution(image1.HorizontalResolution, image1.VerticalResolution); Graphics graphics2 = Graphics.FromImage((Image) bitmap2); ImageAttributes imageAttr = new ImageAttributes(); ColorMap[] map = new ColorMap[1] { new ColorMap() { OldColor = Color.FromArgb((int) byte.MaxValue, 0, (int) byte.MaxValue, 0), NewColor = Color.FromArgb(0, 0, 0, 0) } }; imageAttr.SetRemapTable(map, ColorAdjustType.Bitmap); float[][] newColorMatrix1 = new float[5][]; float[][] numArray2 = newColorMatrix1; int index1 = 0; float[] numArray3 = new float[5]; numArray3[0] = 1f; float[] numArray4 = numArray3; numArray2[index1] = numArray4; float[][] numArray5 = newColorMatrix1; int index2 = 1; float[] numArray6 = new float[5]; numArray6[1] = 1f; float[] numArray7 = numArray6; numArray5[index2] = numArray7; float[][] numArray8 = newColorMatrix1; int index3 = 2; float[] numArray9 = new float[5]; numArray9[2] = 1f; float[] numArray10 = numArray9; numArray8[index3] = numArray10; float[][] numArray11 = newColorMatrix1; int index4 = 3; float[] numArray12 = new float[5]; numArray12[3] = 0.3f; float[] numArray13 = numArray12; numArray11[index4] = numArray13; float[][] numArray14 = newColorMatrix1; int index5 = 4; float[] numArray15 = new float[5]; numArray15[4] = 1f; float[] numArray16 = numArray15; numArray14[index5] = numArray16; ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix1); imageAttr.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x2 = width1 - width2 - 10; int y2 = 10; graphics2.DrawImage(image2, new Rectangle(x2, y2, width2, height2), 0, 0, width2, height2, GraphicsUnit.Pixel, imageAttr); image1.Dispose(); Image image3 = (Image) bitmap2; graphics1.Dispose(); graphics2.Dispose(); bitmap1.Dispose(); image3.Save(SaveImage, ImageFormat.Jpeg); image3.Dispose(); image2.Dispose(); bitmap2.Dispose(); } /// <summary> /// 生成验证码图片 /// /// </summary> /// <param name="sessionName"/><param name="str"/> public static void MakeSpamImageGen(string sessionName, string str) { HttpContext current = HttpContext.Current; int num1 = 5; int num2 = 12; int width = 60; int height = 20; float num3 = (float) (((double) width - (double) num1 * ((double) num2 + 0.5)) / 2.0); float y = (float) (((double) height - (double) num2 * 1.70000004768372) / 2.0); if (num2 == -1) num2 = 30; if (width == -1) width = 290; if (height == -1) height = 80; string str1 = str; current.Session.Add(sessionName, (object) str1); Bitmap bitmap = new Bitmap(Image.FromFile(current.Server.MapPath(string.Concat(new object[4] { (object) current.Request.ApplicationPath, (object) "/Images/AntiSpamBgImgs/bg_", (object) new Random().Next(5), (object) ".jpg" }))), width, height); Graphics graphics = Graphics.FromImage((Image) bitmap); SolidBrush solidBrush = new SolidBrush(Color.Black); string[] strArray = new string[10] { "Arial", "Verdana", "Fixedsys", "宋体", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" }; for (int index = 0; index < str1.Length; ++index) { Font font = new Font(strArray[new Random().Next(index)], (float) num2, FontStyle.Bold); graphics.DrawString(str1.Substring(index, 1), font, (Brush) solidBrush, num3 + (float) (index * num2), y); graphics.Flush(); } graphics.Flush(); graphics.Dispose(); current.Response.ContentType = "image/gif"; bitmap.Save(current.Response.OutputStream, ImageFormat.Gif); current.Response.Flush(); current.Response.End(); } /// <summary> /// 检测指定图片是否符合标准 /// /// </summary> /// <param name="imgLoc"/><param name="width"/><param name="height"/> /// <returns/> public static bool JustImage(string imgLoc, int width, int height) { Image image = Image.FromFile(imgLoc); int width1 = image.Width; int height1 = image.Height; return width1 >= width && height1 >= height; } /// <summary/> /// <param name="imgLoc"/><param name="wmLoc"/> public static void ImageMark(string imgLoc, string wmLoc) { WebGDI.ImageMark(imgLoc, wmLoc, "RB"); } /// <summary/> /// <param name="imgLoc"/><param name="wmLoc"/><param name="wmAlign"/> public static void ImageMark(string imgLoc, string wmLoc, string wmAlign) { if (imgLoc.Substring(imgLoc.Length - 3, 3).ToLower() != "jpg") return; Image original = Image.FromFile(imgLoc); int width1 = original.Width; int height1 = original.Height; Bitmap bitmap1 = new Bitmap(original, width1, height1); bitmap1.SetResolution(72f, 72f); Graphics graphics1 = Graphics.FromImage((Image) bitmap1); Image image1 = (Image) new Bitmap(wmLoc); int width2 = image1.Width; int height2 = image1.Height; if (width1 < width2 || height1 < height2 * 2) return; Bitmap bitmap2 = new Bitmap((Image) bitmap1); bitmap2.SetResolution(original.HorizontalResolution, original.VerticalResolution); Graphics graphics2 = Graphics.FromImage((Image) bitmap2); ImageAttributes imageAttr = new ImageAttributes(); ColorMap[] map = new ColorMap[1] { new ColorMap() { OldColor = Color.FromArgb(0, 0, (int) byte.MaxValue, 0), NewColor = Color.FromArgb(0, 0, 0, 0) } }; imageAttr.SetRemapTable(map, ColorAdjustType.Bitmap); float[][] newColorMatrix1 = new float[5][]; float[] numArray1 = new float[5]; numArray1[0] = 1f; newColorMatrix1[0] = numArray1; float[] numArray2 = new float[5]; numArray2[1] = 1f; newColorMatrix1[1] = numArray2; float[] numArray3 = new float[5]; numArray3[2] = 1f; newColorMatrix1[2] = numArray3; float[] numArray4 = new float[5]; numArray4[3] = 0.8f; newColorMatrix1[3] = numArray4; float[] numArray5 = new float[5]; numArray5[4] = 1f; newColorMatrix1[4] = numArray5; ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix1); imageAttr.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x; int y; if (wmAlign == "LT") { x = 0; y = 0; } else if (wmAlign == "LB") { x = 0; y = height1 - height2; } else if (wmAlign == "RT") { x = width1 - width2; y = 0; } else if (wmAlign == "CT") { x = (width1 - width2) / 2; y = (height1 - height2) / 2; } else { x = width1 - width2; y = height1 - height2; } graphics2.DrawImage(image1, new Rectangle(x, y, width2, height2), 0, 0, width2, height2, GraphicsUnit.Pixel, imageAttr); original.Dispose(); Image image2 = (Image) bitmap2; graphics1.Dispose(); graphics2.Dispose(); bitmap1.Dispose(); ImageCodecInfo encoderInfo = WebGDI.GetEncoderInfo("image/jpeg"); EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, 90L); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = encoderParameter; image2.Save(imgLoc, encoderInfo, encoderParams); image2.Dispose(); image1.Dispose(); } /// <summary/> /// <param name="mimeType"/> /// <returns/> private static ImageCodecInfo GetEncoderInfo(string mimeType) { ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); for (int index = 0; index < imageEncoders.Length; ++index) { if (imageEncoders[index].MimeType == mimeType) return imageEncoders[index]; } return (ImageCodecInfo) null; } } }
下载地地址:点击下载
浙公网安备 33010602011771号