init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 缩略图缩小方式(自动、根据宽度、根据高度、根据宽高、裁切)
|
||||
/// </summary>
|
||||
public enum SaveThumbnailMode {
|
||||
Auto,
|
||||
ByWidth,
|
||||
ByHeight,
|
||||
ByWidthHeight,
|
||||
Cut
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
using System;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 缩略图大小类型(小、中等、大)
|
||||
/// </summary>
|
||||
public enum ThumbnailType {
|
||||
Small,
|
||||
Medium,
|
||||
Big
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 水印位置(上面左部、上面中部、上面右部、下面左部、下面中部、下面右部)
|
||||
/// </summary>
|
||||
public enum WatermarkPosition {
|
||||
TopLeft,
|
||||
TopCenter,
|
||||
TopRight,
|
||||
BottomLeft,
|
||||
BottomCenter,
|
||||
BottomRight
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// ×ÖÌå³ß´ç
|
||||
/// </summary>
|
||||
public class FontAndSize {
|
||||
|
||||
public Font font { get; set; }
|
||||
public SizeF size { get; set; }
|
||||
|
||||
public static FontAndSize GetValue( Graphics g, String text, String fontFamily, int fontSize, int srcWidth ) {
|
||||
|
||||
FontAndSize wfont = new FontAndSize();
|
||||
|
||||
Font font = null;
|
||||
SizeF size = new SizeF();
|
||||
|
||||
for (int i = fontSize; i > 6; i = i - 2) {
|
||||
font = new Font( fontFamily, i, FontStyle.Bold );
|
||||
size = g.MeasureString( text, font );
|
||||
if (size.Width <= srcWidth) break;
|
||||
}
|
||||
|
||||
wfont.font = font;
|
||||
wfont.size = size;
|
||||
|
||||
return wfont;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Web.Utils;
|
||||
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 图片常用操作
|
||||
/// </summary>
|
||||
public class Img {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( Img ) );
|
||||
private static Random rd = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// 删除图片以及缩略图。如果图片不存在,则忽略
|
||||
/// </summary>
|
||||
/// <param name="srcPath">相对网址</param>
|
||||
public static void DeleteImgAndThumb( String srcPath ) {
|
||||
|
||||
DeleteImgAndThumb( srcPath, Uploader.ThumbTypes );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除图片以及指定类型的缩略图。如果图片不存在,则忽略
|
||||
/// </summary>
|
||||
/// <param name="srcPath">相对网址</param>
|
||||
/// <param name="arrThumbType">多个缩略图类型</param>
|
||||
public static void DeleteImgAndThumb( String srcPath, ThumbnailType[] arrThumbType ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( srcPath )) return;
|
||||
if (srcPath.ToLower().StartsWith( "http://" )) return;
|
||||
|
||||
String path = PathHelper.Map( srcPath );
|
||||
if (file.Exists( path ))
|
||||
Zhaizj.Framework.IO.File.Delete( path );
|
||||
|
||||
foreach (ThumbnailType ttype in arrThumbType) {
|
||||
|
||||
String pathThumb = PathHelper.Map( GetThumbPath( srcPath, ttype ) );
|
||||
if (file.Exists( pathThumb ))
|
||||
Zhaizj.Framework.IO.File.Delete( pathThumb );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 彻底删除磁盘文件
|
||||
/// </summary>
|
||||
/// <param name="srcPath">相对路径</param>
|
||||
public static void DeleteFile( String srcPath ) {
|
||||
if (strUtil.IsNullOrEmpty( srcPath )) return;
|
||||
if (srcPath.ToLower().StartsWith( "http://" )) return;
|
||||
|
||||
String path = PathHelper.Map( srcPath );
|
||||
if (file.Exists( path ))
|
||||
Zhaizj.Framework.IO.File.Delete( path );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取图片的随机文件名(会添加日期文件夹和随机文件名),比如 2009-9-28/1530703343314547.jpg
|
||||
/// </summary>
|
||||
/// <remarks>如果日期文件夹不存在,则在磁盘上自动创建文件夹</remarks>
|
||||
/// <param name="pathName">图片存储的绝对路径</param>
|
||||
/// <param name="strContentType">图片类型</param>
|
||||
/// <returns>返回图片名称,包括所在文件夹,比如 2009-9-28/1530703343314547.jpg</returns>
|
||||
public static String GetPhotoName( String absPath, String strContentType ) {
|
||||
DateTime now = DateTime.Now;
|
||||
String strDir = getDirName( now );
|
||||
|
||||
String strRandom = rd.Next( 100000000, 999999999 ).ToString();
|
||||
|
||||
String strFile = now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString() + strRandom;
|
||||
strFile = strUtil.Join( strFile, GetImageExt( strContentType ), "." );
|
||||
|
||||
String fullDir = Path.Combine( absPath, strDir );
|
||||
if (!Directory.Exists( fullDir )) {
|
||||
Directory.CreateDirectory( fullDir );
|
||||
}
|
||||
return Path.Combine( strDir, strFile );
|
||||
}
|
||||
|
||||
//private static string getDirName( DateTime now ) {
|
||||
// return string.Format( "{0}-{1}-{2}", now.Year, now.Month, now.Day );
|
||||
//}
|
||||
|
||||
private static string getDirName( DateTime now ) {
|
||||
return string.Format( "{0}/{1}/{2}", now.Year, now.Month, now.Day );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件的随机文件名(会添加日期文件夹和随机文件名),比如 2009-9-28/1530703343314547.zip
|
||||
/// </summary>
|
||||
/// <remarks>如果日期文件夹不存在,则在磁盘上自动创建文件夹</remarks>
|
||||
/// <param name="pathName">存储路径</param>
|
||||
/// <param name="fileExt">文件类型</param>
|
||||
/// <returns>返回文件名称,包括所在文件夹,比如 2009-9-28/1530703343314547.zip</returns>
|
||||
public static String GetFileName( String pathName, String fileExt ) {
|
||||
DateTime now = DateTime.Now;
|
||||
String strDate = getDirName( now );
|
||||
|
||||
String strRandom = rd.Next( 100000000, 999999999 ).ToString();
|
||||
String strFile = now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString() + strRandom;
|
||||
strFile = strUtil.Join( strFile, fileExt, "." );
|
||||
|
||||
String path = Path.Combine( pathName, strDate );
|
||||
if (!Directory.Exists( path )) {
|
||||
Directory.CreateDirectory( path );
|
||||
}
|
||||
return Path.Combine( strDate, strFile );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据缩略图名称,获取原始图片名称
|
||||
/// </summary>
|
||||
/// <param name="thumbPath"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetOriginalPath( String thumbPath ) {
|
||||
|
||||
String path = thumbPath.Trim();
|
||||
|
||||
String ext = Path.GetExtension( path );
|
||||
String pathWithoutExt = strUtil.TrimEnd( path, ext );
|
||||
|
||||
if (pathWithoutExt.EndsWith( "_s" )) {
|
||||
return strUtil.TrimEnd( pathWithoutExt, "_s" ) + ext;
|
||||
}
|
||||
|
||||
if (pathWithoutExt.EndsWith( "_m" )) {
|
||||
return strUtil.TrimEnd( pathWithoutExt, "_m" ) + ext;
|
||||
}
|
||||
|
||||
if (pathWithoutExt.EndsWith( "_b" )) {
|
||||
return strUtil.TrimEnd( pathWithoutExt, "_b" ) + ext;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据原始图片名称,获取缩略图名称(最小的缩略图)
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetThumbPath( Object srcPath ) {
|
||||
|
||||
if (srcPath == null) return null;
|
||||
if (strUtil.IsNullOrEmpty( srcPath.ToString() )) return null;
|
||||
return GetThumbPath( srcPath, ThumbnailType.Small );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据原始图片名称和缩略图类型,获取缩略图名称
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="ttype"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetThumbPath( Object srcPath, ThumbnailType ttype ) {
|
||||
|
||||
if (srcPath == null) return "";
|
||||
String path = srcPath.ToString();
|
||||
if (strUtil.IsNullOrEmpty( path )) return "";
|
||||
|
||||
String ext = Path.GetExtension( path );
|
||||
String pathWithoutExt = strUtil.TrimEnd( path, ext );
|
||||
|
||||
if (pathWithoutExt.EndsWith( "_s" )) pathWithoutExt = strUtil.TrimEnd( pathWithoutExt, "_s" );
|
||||
if (pathWithoutExt.EndsWith( "_b" )) pathWithoutExt = strUtil.TrimEnd( pathWithoutExt, "_b" );
|
||||
if (pathWithoutExt.EndsWith( "_m" )) pathWithoutExt = strUtil.TrimEnd( pathWithoutExt, "_m" );
|
||||
|
||||
String suffix = "_s";
|
||||
if (ttype == ThumbnailType.Medium)
|
||||
suffix = "_m";
|
||||
else if (ttype == ThumbnailType.Big)
|
||||
suffix = "_b";
|
||||
|
||||
return pathWithoutExt + suffix + ext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存缩略图到磁盘(可指定宽度,默认自动缩放)
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="destPath"></param>
|
||||
/// <param name="width"></param>
|
||||
public static void SaveThumbnail( String srcPath, String destPath, int width ) {
|
||||
SaveThumbnail( srcPath, destPath, width, width, SaveThumbnailMode.Auto );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存缩略图到磁盘(可指定宽高,默认自动缩放)
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="destPath"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
public static void SaveThumbnail( String srcPath, String destPath, int width, int height ) {
|
||||
SaveThumbnail( srcPath, destPath, width, height, SaveThumbnailMode.Auto );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存缩略图到磁盘(可指定宽高,可指定缩放模式)
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="destPath"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="mode"></param>
|
||||
public static void SaveThumbnail( String srcPath, String destPath, int width, int height, SaveThumbnailMode mode ) {
|
||||
|
||||
using (Image srcImg = Image.FromFile( srcPath )) {
|
||||
|
||||
ThumbSize t = getTargetSize( width, height, mode, srcImg );
|
||||
using (Bitmap newImg = new Bitmap( t.New.Width, t.New.Height )) {
|
||||
|
||||
using (Graphics g = Graphics.FromImage( newImg )) {
|
||||
g.InterpolationMode = InterpolationMode.High;
|
||||
g.SmoothingMode = SmoothingMode.HighQuality;
|
||||
g.Clear( Color.Transparent );
|
||||
g.DrawImage( srcImg, t.getNewRect(), t.getRect(), GraphicsUnit.Pixel );
|
||||
|
||||
try {
|
||||
newImg.Save( destPath, ImageFormat.Jpeg );
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缩略图尺寸
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="mode"></param>
|
||||
/// <param name="src"></param>
|
||||
/// <returns></returns>
|
||||
public static ThumbSize getTargetSize( int width, int height, SaveThumbnailMode mode, Image src ) {
|
||||
|
||||
ThumbSize t = new ThumbSize();
|
||||
t.Src = new Size( src.Width, src.Height );
|
||||
t.Point = new Point( 0, 0 );
|
||||
|
||||
if (mode == SaveThumbnailMode.ByWidth) {
|
||||
int newHeight = src.Height * width / src.Width;
|
||||
t.New = new Size( width, newHeight );
|
||||
return t;
|
||||
}
|
||||
else if (mode == SaveThumbnailMode.ByHeight) {
|
||||
int newWidth = src.Width * height / src.Height;
|
||||
t.New = new Size( newWidth, height );
|
||||
return t;
|
||||
}
|
||||
else if (mode == SaveThumbnailMode.ByWidthHeight) {
|
||||
t.New = new Size( width, height );
|
||||
return t;
|
||||
}
|
||||
else if (mode == SaveThumbnailMode.Cut) {
|
||||
return getCutSize( width, height, mode, src );
|
||||
}
|
||||
|
||||
return getAutoSize( width, height, mode, src );
|
||||
}
|
||||
|
||||
private static ThumbSize getAutoSize( int width, int height, SaveThumbnailMode mode, Image src ) {
|
||||
ThumbSize t = new ThumbSize();
|
||||
t.Src = new Size( src.Width, src.Height );
|
||||
t.Point = new Point( 0, 0 );
|
||||
|
||||
int newWidth = width;
|
||||
int newHeight = height;
|
||||
if ((double)src.Width / (double)src.Height > (double)width / (double)height)
|
||||
newHeight = src.Height * width / src.Width;
|
||||
else
|
||||
newWidth = src.Width * height / src.Height;
|
||||
t.New = new Size( newWidth, newHeight );
|
||||
|
||||
return t;
|
||||
|
||||
}
|
||||
|
||||
private static ThumbSize getCutSize( int width, int height, SaveThumbnailMode mode, Image src ) {
|
||||
|
||||
ThumbSize t = new ThumbSize();
|
||||
|
||||
if ((double)src.Width / (double)src.Height > (double)width / (double)height) {
|
||||
|
||||
int newWidth = src.Height * width / height;
|
||||
t.Src = new Size( newWidth, src.Height );
|
||||
t.New = new Size( width, height );
|
||||
t.Point = new Point( (src.Width - newWidth) / 2, 0 );
|
||||
}
|
||||
else {
|
||||
|
||||
int newHeight = src.Width * height / width;
|
||||
t.Src = new Size( src.Width, newHeight );
|
||||
t.New = new Size( width, height );
|
||||
t.Point = new Point( 0, (src.Height - newHeight) / 2 );
|
||||
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取图片的后缀名(不包括点号)
|
||||
/// </summary>
|
||||
/// <param name="strContentType"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetImageExt( String strContentType ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( strContentType )) return null;
|
||||
|
||||
String t = strContentType.ToLower();
|
||||
|
||||
logger.Info( "strContentType=>" + t );
|
||||
|
||||
if (t.Equals( ".gif" )) return "gif";
|
||||
if (t.Equals( ".bmp" )) return "bmp";
|
||||
if (t.Equals( ".png" )) return "png";
|
||||
if (t.Equals( ".jpg" )) return "jpg";
|
||||
if (t.Equals( ".jpeg" )) return "jpeg";
|
||||
|
||||
if (t.Equals( "gif" )) return "gif";
|
||||
if (t.Equals( "bmp" )) return "bmp";
|
||||
if (t.Equals( "png" )) return "png";
|
||||
if (t.Equals( "jpg" )) return "jpg";
|
||||
if (t.Equals( "jpeg" )) return "jpeg";
|
||||
|
||||
if (t.Equals( "image/gif")) return "gif";
|
||||
if (t.Equals( "image/bmp")) return "bmp";
|
||||
if (t.Equals( "image/tiff")) return "tiff";
|
||||
if (t.Equals( "image/x-icon")) return "icon";
|
||||
if (t.Equals( "image/x-png")) return "png"; // ie
|
||||
if (t.Equals( "image/png" )) return "png"; // firefox, google
|
||||
|
||||
if (t.Equals( "image/x-emf")) return "emf";
|
||||
if (t.Equals( "image/x-exif")) return "exif";
|
||||
if (t.Equals( "image/x-wmf")) return "wmf";
|
||||
if (t.Equals( "image/pjpeg")) return "jpg"; // ie6
|
||||
if (t.Equals( "image/jpg" )) return "jpg"; // ie8
|
||||
if (t.Equals( "image/jpeg" )) return "jpg"; // firefox, google
|
||||
|
||||
return "jpg";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取图片的类型
|
||||
/// </summary>
|
||||
/// <param name="strContentType"></param>
|
||||
/// <returns></returns>
|
||||
public static ImageFormat GetImageType( String strContentType ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( strContentType )) return null;
|
||||
|
||||
String t = strContentType.ToLower();
|
||||
|
||||
if (t.Equals( ".gif" )) return ImageFormat.Gif;
|
||||
if (t.Equals( ".bmp" )) return ImageFormat.Bmp;
|
||||
if (t.Equals( ".png" )) return ImageFormat.Png;
|
||||
if (t.Equals( ".jpg" )) return ImageFormat.Jpeg;
|
||||
if (t.Equals( ".jpeg" )) return ImageFormat.Jpeg;
|
||||
|
||||
if (t.Equals( "gif" )) return ImageFormat.Gif;
|
||||
if (t.Equals( "bmp" )) return ImageFormat.Bmp;
|
||||
if (t.Equals( "png" )) return ImageFormat.Png;
|
||||
if (t.Equals( "jpg" )) return ImageFormat.Jpeg;
|
||||
if (t.Equals( "jpeg" )) return ImageFormat.Jpeg;
|
||||
|
||||
if (t.Equals( "image/pjpeg")) return ImageFormat.Jpeg;
|
||||
if (t.Equals( "image/gif")) return ImageFormat.Gif;
|
||||
if (t.Equals( "image/bmp")) return ImageFormat.Bmp;
|
||||
if (t.Equals( "image/tiff")) return ImageFormat.Tiff;
|
||||
if (t.Equals( "image/x-icon")) return ImageFormat.Icon;
|
||||
if (t.Equals( "image/x-png")) return ImageFormat.Png;
|
||||
if (t.Equals( "image/png" )) return ImageFormat.Png;
|
||||
if (t.Equals( "image/x-emf")) return ImageFormat.Emf;
|
||||
if (t.Equals( "image/x-exif")) return ImageFormat.Exif;
|
||||
if (t.Equals( "image/x-wmf")) return ImageFormat.Wmf;
|
||||
|
||||
return ImageFormat.MemoryBmp;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Drawing;
|
||||
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// ËõÂÔͼ³ß´ç
|
||||
/// </summary>
|
||||
public class ThumbSize {
|
||||
|
||||
public Size New { get; set; }
|
||||
public Size Src { get; set; }
|
||||
|
||||
public Point Point { get; set; }
|
||||
|
||||
public Rectangle getRect() {
|
||||
return new Rectangle( this.Point.X, this.Point.Y, this.Src.Width, this.Src.Height );
|
||||
}
|
||||
|
||||
public Rectangle getNewRect() {
|
||||
return new Rectangle( 0, 0, this.New.Width, this.New.Height );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 验证码工具
|
||||
/// </summary>
|
||||
public class ValidationCode {
|
||||
|
||||
/// <summary>
|
||||
/// 创建验证码,返回一个 Image 对象
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="fontFamily"></param>
|
||||
/// <returns></returns>
|
||||
public Image CreateImage( String code, int width, int height, String fontFamily ) {
|
||||
|
||||
Bitmap bm = new Bitmap( width, height );
|
||||
|
||||
using (Graphics g = Graphics.FromImage( bm )) {
|
||||
|
||||
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
|
||||
HatchBrush brush = new HatchBrush( HatchStyle.SmallConfetti, Color.LightGray, Color.White );
|
||||
Rectangle rect = new Rectangle( 0, 0, width, height );
|
||||
g.FillRectangle( brush, rect );
|
||||
|
||||
int fontsize = rect.Height + 1;
|
||||
FontAndSize size = FontAndSize.GetValue( g, code, fontFamily, fontsize, bm.Width );
|
||||
|
||||
GraphicsPath gpath = getGraphicsPath( code, size.font, rect );
|
||||
|
||||
Color brushColor = ColorTranslator.FromHtml( "#000000" );
|
||||
brush = new HatchBrush( HatchStyle.Divot, brushColor, Color.DarkGray );
|
||||
g.FillPath( brush, gpath );
|
||||
|
||||
addRandomNoise( g, rect );
|
||||
|
||||
}
|
||||
return bm;
|
||||
}
|
||||
|
||||
private void addRandomNoise( Graphics g, Rectangle rect ) {
|
||||
|
||||
HatchBrush brush = new HatchBrush( HatchStyle.Weave, Color.LightGray, Color.White );
|
||||
|
||||
for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) {
|
||||
int x = rd.Next( rect.Width );
|
||||
int y = rd.Next( rect.Height );
|
||||
g.FillEllipse( brush, x, y, 2, 3 );
|
||||
}
|
||||
}
|
||||
|
||||
private Rectangle addRandomLine( Graphics g, Rectangle rect ) {
|
||||
int lineCount = rd.Next( 1, 8 );
|
||||
for (int i = 0; i < lineCount; i++) {
|
||||
|
||||
Point pt1 = new Point();
|
||||
pt1.X = rd.Next( rect.Width );
|
||||
pt1.Y = rd.Next( rect.Height );
|
||||
|
||||
Point pt2 = new Point();
|
||||
pt2.X = rd.Next( rect.Width );
|
||||
pt2.Y = rd.Next( rect.Height );
|
||||
|
||||
float width = rd.Next( 2 );
|
||||
|
||||
Pen p = new Pen( Color.Aqua, width );
|
||||
g.DrawLine( p, pt1, pt2 );
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
private StringFormat getFormat() {
|
||||
StringFormat format = new StringFormat();
|
||||
format.Alignment = StringAlignment.Center;
|
||||
format.LineAlignment = StringAlignment.Center;
|
||||
return format;
|
||||
}
|
||||
|
||||
private GraphicsPath getGraphicsPath( String code, Font font, Rectangle rect ) {
|
||||
|
||||
StringFormat format = getFormat();
|
||||
|
||||
GraphicsPath path = new GraphicsPath();
|
||||
path.AddString( code, font.FontFamily, (int)font.Style, font.Size, rect, format );
|
||||
|
||||
PointF[] points = getRandomPoint( rect );
|
||||
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.Translate( 0F, 0F );
|
||||
|
||||
path.Warp( points, rect, matrix, WarpMode.Perspective, 0F );
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
private PointF[] getRandomPoint( Rectangle rect ) {
|
||||
float v = 4F;
|
||||
PointF[] points =
|
||||
{
|
||||
new PointF(rd.Next(rect.Width) / v, rd.Next(rect.Height) / v),
|
||||
new PointF(rect.Width - rd.Next(rect.Width) / v, rd.Next(rect.Height) / v),
|
||||
new PointF(rd.Next(rect.Width) / v, rect.Height - rd.Next(rect.Height) / v),
|
||||
new PointF(rect.Width - rd.Next(rect.Width) / v, rect.Height - rd.Next(rect.Height) / v)
|
||||
};
|
||||
return points;
|
||||
}
|
||||
|
||||
private Random rd = new Random();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
|
||||
namespace Zhaizj.Framework.Drawing {
|
||||
|
||||
/// <summary>
|
||||
/// 水印工具
|
||||
/// </summary>
|
||||
public class Watermark {
|
||||
|
||||
/// <summary>
|
||||
/// 将原始图片添加图片水印之后存储
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="savePath"></param>
|
||||
/// <param name="watermarkPath"></param>
|
||||
/// <param name="wp"></param>
|
||||
public static void MakeByPic( String srcPath, String savePath, String watermarkPath, WatermarkPosition wp ) {
|
||||
|
||||
using (Image src = Image.FromFile( srcPath )) {
|
||||
using (Bitmap bm = new Bitmap( srcPath )) {
|
||||
using (Graphics g = Graphics.FromImage( bm )) {
|
||||
using (Image wm = Image.FromFile( watermarkPath )) {
|
||||
|
||||
ImageAttributes imgAttr = getImageAttributes();
|
||||
Rectangle rect = getPicRectangle( src, wm, wp );
|
||||
|
||||
g.DrawImage( wm, rect, 0, 0, wm.Width, wm.Height, GraphicsUnit.Pixel, imgAttr );
|
||||
|
||||
try {
|
||||
bm.Save( savePath );
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static ImageAttributes getImageAttributes() {
|
||||
|
||||
ImageAttributes imgAttr = 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 };
|
||||
|
||||
imgAttr.SetRemapTable( remapTable, ColorAdjustType.Bitmap );
|
||||
|
||||
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, 0.3f, 0.0f},
|
||||
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
|
||||
};
|
||||
|
||||
ColorMatrix wmColorMatrix = new ColorMatrix( colorMatrixElements );
|
||||
imgAttr.SetColorMatrix( wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap );
|
||||
|
||||
return imgAttr;
|
||||
}
|
||||
|
||||
private static Rectangle getPicRectangle( Image src, Image wm, WatermarkPosition wp ) {
|
||||
|
||||
int x = 10;
|
||||
int y = 10;
|
||||
|
||||
if (wp == WatermarkPosition.TopLeft) {
|
||||
}
|
||||
else if (wp == WatermarkPosition.TopCenter) {
|
||||
x = src.Width / 2 - wm.Width / 2;
|
||||
y = 10;
|
||||
}
|
||||
else if (wp == WatermarkPosition.TopRight) {
|
||||
x = ((src.Width - wm.Width) - 10);
|
||||
y = 10;
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomLeft) {
|
||||
x = 10;
|
||||
y = src.Height - wm.Height - 10;
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomCenter) {
|
||||
x = src.Width / 2 - wm.Width / 2;
|
||||
y = src.Height - wm.Height - 10;
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomRight) {
|
||||
x = ((src.Width - wm.Width) - 10);
|
||||
y = src.Height - wm.Height - 10;
|
||||
}
|
||||
|
||||
return new Rectangle( x, y, wm.Width, wm.Height );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将原始图片添加文字水印之后存储
|
||||
/// </summary>
|
||||
/// <param name="srcPath"></param>
|
||||
/// <param name="savePath"></param>
|
||||
/// <param name="words"></param>
|
||||
/// <param name="wp"></param>
|
||||
/// <param name="fontSize"></param>
|
||||
public static void MakeByText( String srcPath, String savePath, String words, WatermarkPosition wp, int fontSize ) {
|
||||
|
||||
using (Image src = Image.FromFile( srcPath )) {
|
||||
using (Bitmap bm = new Bitmap( srcPath )) {
|
||||
using (Graphics g = Graphics.FromImage( bm )) {
|
||||
|
||||
FontAndSize fs = FontAndSize.GetValue( g, words, "arial", fontSize, src.Width );
|
||||
PointF p = getTextPoint( src, fs.size, wp );
|
||||
StringFormat sf = getStringFormat();
|
||||
|
||||
drawText( g, words, fs.font, p, sf );
|
||||
|
||||
try {
|
||||
bm.Save( savePath );
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void drawText( Graphics g, String wmText, Font font, PointF p, StringFormat format ) {
|
||||
|
||||
SolidBrush brush = new SolidBrush( Color.FromArgb( 153, 255, 255, 255 ) );
|
||||
g.DrawString( wmText, font, brush, p, format );
|
||||
|
||||
SolidBrush brush2 = new SolidBrush( Color.FromArgb( 153, 0, 0, 0 ) );
|
||||
g.DrawString( wmText, font, brush2, new PointF( p.X + 1, p.Y + 1 ), format );
|
||||
}
|
||||
|
||||
private static StringFormat getStringFormat() {
|
||||
StringFormat StrFormat = new StringFormat();
|
||||
StrFormat.Alignment = StringAlignment.Center;
|
||||
return StrFormat;
|
||||
}
|
||||
|
||||
private static PointF getTextPoint( Image src, SizeF textSize, WatermarkPosition wp ) {
|
||||
|
||||
float x = textSize.Width / 2 + 5;
|
||||
float y = textSize.Height / 2 + 5;
|
||||
|
||||
int margin = 10;
|
||||
|
||||
if (wp == WatermarkPosition.TopLeft) {
|
||||
x = getLeftPosition( src, textSize, margin );
|
||||
y = getTopPosition( src, textSize, margin );
|
||||
}
|
||||
else if (wp == WatermarkPosition.TopCenter) {
|
||||
x = src.Width / 2;
|
||||
y = getTopPosition( src, textSize, margin );
|
||||
}
|
||||
else if (wp == WatermarkPosition.TopRight) {
|
||||
x = getRightPosition( src, textSize, margin );
|
||||
y = getTopPosition( src, textSize, margin );
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomLeft) {
|
||||
x = getLeftPosition( src, textSize, margin );
|
||||
y = getBottomPosition( src, textSize, margin );
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomCenter) {
|
||||
x = src.Width / 2;
|
||||
y = getBottomPosition( src, textSize, margin );
|
||||
}
|
||||
else if (wp == WatermarkPosition.BottomRight) {
|
||||
x = getRightPosition( src, textSize, margin );
|
||||
y = getBottomPosition( src, textSize, margin );
|
||||
}
|
||||
|
||||
return new PointF( x, y );
|
||||
}
|
||||
|
||||
private static float getLeftPosition( Image src, SizeF textSize, int margin ) {
|
||||
return textSize.Width / 2 + margin;
|
||||
}
|
||||
|
||||
private static float getTopPosition( Image src, SizeF textSize, int margin ) {
|
||||
return textSize.Height / 2 + margin / 2;
|
||||
}
|
||||
|
||||
private static float getRightPosition( Image src, SizeF textSize, int margin ) {
|
||||
float x = src.Width - margin * 2 - (textSize.Width / 2);
|
||||
return x;
|
||||
}
|
||||
|
||||
private static float getBottomPosition( Image src, SizeF textSize, int margin ) {
|
||||
float y = src.Height - margin * 2 - (textSize.Height / 2);
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user