using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
namespace Eventech.DynPicture.Model
{
///
///
///
public static class ImageExtension
{
///
/// 获取索引像素格式数组
///
public static PixelFormat[] IndexedPixelFormats
{
get
{
return new PixelFormat[]
{
PixelFormat.Undefined,
PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555,
PixelFormat.Format16bppGrayScale,
PixelFormat.Format1bppIndexed,
PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
};
}
}
///
/// 是否为索引像素格式
///
///
///
public static bool IsPixelFormatIndexed(PixelFormat format)
{
if (IndexedPixelFormats.Contains(format))
return true;
return false;
}
///
/// 是否为索引像素格式
///
///
///
public static bool IsPixelFormatIndexed(this Image img)
{
return IsPixelFormatIndexed(img.PixelFormat);
}
///
/// 自定义创建图片的副本(若PixelFormat为索引像素格式会抛异常))
///
///
/// 默认Format32bppArgb
///
public static Image CloneC(this Image img, PixelFormat format = PixelFormat.Format32bppArgb)
{
try
{
if (!IsPixelFormatIndexed(img.PixelFormat))
{
format = img.PixelFormat;
}
var bmp = new Bitmap(img.Width, img.Height, format);
using (var g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height));
}
return bmp;
}
catch
{
return img;
}
}
///
/// 在图片上绘制文字信息(图片像素格式不能为像素索引格式)
///
/// 绘制文字的图片
/// 文本信息
/// 定位点
/// 字体
/// 字体颜色
/// 水平对齐方式
/// 垂直对齐方式
/// 旋转角度(顺时针)
public static void DrawString(this Graphics g, string text, PointF pf, Font f, Color fc,float angle=0, StringAlignment horiz = StringAlignment.Near, StringAlignment verti = StringAlignment.Near)
{
using (var brush = new SolidBrush(fc))
using (var format = new StringFormat())
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(pf.X, pf.Y);
g.RotateTransform(angle);
format.Alignment = horiz;
format.LineAlignment = verti;
g.DrawString(text, f, brush, new PointF(0, 0), format);
g.ResetTransform();
}
}
///
/// 绘制文字信息(图片像素格式不能为像素索引格式)
///
///
///
/// 原点
/// 文字依据的点
/// 文字颜色
/// 旋转角度
///
///
public static void DrawString(this Graphics g,string text,PointF op,SizeF size,Font f,Color fc,float angle=0, StringAlignment align = StringAlignment.Near,StringAlignment vlign=StringAlignment.Center)
{
using (var brush = new SolidBrush(fc))
using (var format = new StringFormat())
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(op.X, op.Y);
g.RotateTransform(angle);
format.Alignment = align;
format.LineAlignment = vlign;
g.DrawString(text,f,brush,new RectangleF(0,0,size.Width,size.Height),format);
g.ResetTransform();
}
}
public static void DrawString(this Graphics g, string text, ImageInfoCoordinate info, Font f, Color fc,float scaleX,float scaleY, StringAlignment align = StringAlignment.Near, StringAlignment vlign = StringAlignment.Center)
{
using (var brush = new SolidBrush(fc))
using (var format = new StringFormat())
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(info.Origin.X,info.Origin.Y);
g.RotateTransform(info.Angle);
if (scaleX <= 0)
scaleX = 1f;
if (scaleY <= 0)
scaleY = 1f;
g.ScaleTransform(scaleX,scaleY);
format.Alignment = align;
format.LineAlignment = vlign;
var startPoint = new PointF(info.BeforeLeftTop.X-info.Origin.X,info.BeforeLeftTop.Y-info.Origin.Y);
startPoint = new PointF(startPoint.X/scaleX,startPoint.Y/scaleY);
var size = new SizeF(info.Size.Width/scaleX,info.Size.Height/scaleY);
g.DrawString(text, f, brush, new RectangleF(startPoint, size), format);
g.ResetTransform();
}
}
///
/// 绘制图片
///
///
///
///
///
///
///
///
public static void DrawImage(this Graphics g,Image img,PointF pf,float angle, StringAlignment horiz = StringAlignment.Center, StringAlignment verti = StringAlignment.Center)
{
PointF p = new PointF();
switch(horiz)
{
case StringAlignment.Near:p.X=0;break;
case StringAlignment.Center:p.X=-img.Width/2;break;
case StringAlignment.Far:p.X=-img.Width;break;
default:break;
}
switch(verti)
{
case StringAlignment.Near:p.Y=0;break;
case StringAlignment.Center:p.Y=-img.Height/2;break;
case StringAlignment.Far:p.Y=-img.Height;break;
default:break;
}
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(pf.X,pf.Y);
g.RotateTransform(angle);
g.DrawImage(img,p);
g.ResetTransform();
}
///
/// 绘制箭头
///
///
/// 开始点
/// 旋转角度
/// 箭头线宽
/// 箭头宽度
/// 箭头长度
/// 箭头颜色
///
///
public static void DrawArrow(this Graphics g, PointF pf,float angle,float lineWidth,float lineLength,Color lc, float arrowWidth, float arrowLength,Color ac)
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(pf.X, pf.Y);
g.RotateTransform(angle);
var halfLineWidth=lineWidth/2;
var halfArrowWidth=arrowWidth/2;
lineLength=lineLength-arrowLength;
var lp1 = new PointF(0,halfLineWidth);
var lp2 = new PointF(lineLength,halfLineWidth);
var lp3 = new PointF(lineLength,-halfLineWidth);
var lp4 = new PointF(0,-halfLineWidth);
var lps = new PointF[] { lp1, lp2, lp3, lp4 };
using (var lb = new SolidBrush(lc))
{
g.FillPolygon(lb, lps);
}
var ap1 = new PointF(lineLength,halfArrowWidth);
var ap2 = new PointF(lineLength,-halfArrowWidth);
var ap3 = new PointF(lineLength+arrowLength,0);
var aps = new PointF[] { ap1,ap2,ap3};
using (var ab = new SolidBrush(ac))
{
g.FillPolygon(ab, aps);
}
g.ResetTransform();
}
public static void DrawLine(this Graphics g, PointF pf, float angle, float lineWidth, float lineLength, Color lc, eLineStyle lineStyle)
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TranslateTransform(pf.X, pf.Y);
g.RotateTransform(angle);
using(var pen=new Pen(lc,lineWidth))
{
if (lineStyle == eLineStyle.Dotted)
{
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[] { 10f,5f };
}
//pen.DashStyle=DashStyle.Dash;
g.DrawLine(pen,0,0,lineLength,0);
}
g.ResetTransform();
}
///
/// 获取图片物理尺寸(KB)
///
///
///
public static long GetPhysicalKb(this Image img)
{
using (var ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Png);
return ms.Length / 1024;
}
}
}
}