using System.Drawing;
|
using System.Drawing.Drawing2D;
|
using System.Drawing.Imaging;
|
|
namespace HotelPms.Share.Windows.GraphicsApi
|
{
|
/// <summary>
|
/// UTF8中文转码
|
/// </summary>
|
public static class GdiPlus
|
{
|
/// <summary>
|
/// 等比缩放图片,画到目标区域
|
/// </summary>
|
/// <param name="g"></param>
|
/// <param name="img"></param>
|
/// <param name="tagRect"></param>
|
public static void DrawImage(Graphics g, Bitmap img, Rectangle tagRect)
|
{
|
int width = tagRect.Width;
|
int height = tagRect.Height;
|
|
//Zoom
|
if ((double)img.Width / (double)tagRect.Width < (double)img.Height / (double)tagRect.Height) // image is wider
|
{
|
//例1: img(w=100,h=100) → tag(w=10,h=5) w缩小10倍,h缩小20倍 → 全体缩小20倍 → w=5,h=5
|
width = (int)((double)img.Width / ((double)img.Height / (double)tagRect.Height));
|
}
|
else
|
{
|
//例2: img(w=100,h=100) → tag(w=100,h=500) w缩小1倍,h缩小0.5倍 → 全体缩小1倍 → w=100,h=100
|
height = (int)((double)img.Height / (double)img.Width * (double)tagRect.Width);
|
}
|
|
//居中
|
Rectangle imgRect = new Rectangle(tagRect.X, tagRect.Y, width, height);
|
ImageAttributes vAttr = new ImageAttributes();
|
vAttr.SetColorKey(img.GetPixel(0, 0), img.GetPixel(0, 0));
|
g.DrawImage(img, imgRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, vAttr);
|
}
|
|
public static void FillRectangle(Graphics g, Color color, RectangleF rect)
|
{
|
bool newflag = color.IsSystemColor;
|
SolidBrush brush = newflag ? SystemBrushes.FromSystemColor(color) as SolidBrush : new SolidBrush(color);
|
g.FillRectangle(brush, rect);
|
if (!newflag) { brush.Dispose(); }
|
}
|
|
/// <summary>
|
/// 円角長方形
|
/// </summary>
|
/// <param name="g"></param>
|
/// <param name="pen"></param>
|
/// <param name="rect"></param>
|
/// <param name="cornerRadius"></param>
|
public static void DrawRoundRectangle(Graphics g, Pen pen, RectangleF rect, int cornerRadius)
|
{
|
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
|
{
|
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
|
//g.InterpolationMode = InterpolationMode.HighQualityBicubic; //要らない!!!
|
//g.CompositingQuality = CompositingQuality.HighQuality; //要らない!!!
|
g.DrawPath(pen, path);
|
g.SmoothingMode = SmoothingMode.Default;
|
//g.InterpolationMode = InterpolationMode.Default; //要らない!!!
|
///g.CompositingQuality = CompositingQuality.Default; //要らない!!!
|
}
|
}
|
|
/// <summary>
|
/// 円角長方形
|
/// </summary>
|
/// <param name="g"></param>
|
/// <param name="brush"></param>
|
/// <param name="rect"></param>
|
/// <param name="cornerRadius"></param>
|
public static void FillRoundRectangle(Graphics g, Brush brush, RectangleF rect, int cornerRadius)
|
{
|
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
|
{
|
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
|
//g.InterpolationMode = InterpolationMode.HighQualityBicubic; //要らない!!!
|
//g.CompositingQuality = CompositingQuality.HighQuality; //要らない!!!
|
g.FillPath(brush, path);
|
g.SmoothingMode = SmoothingMode.Default;
|
//g.InterpolationMode = InterpolationMode.Default; //要らない!!!
|
//g.CompositingQuality = CompositingQuality.Default; //要らない!!!
|
}
|
}
|
|
/// <summary>
|
///角丸長方形のGraphicsPathを作成します。
|
/// </summary>
|
/// <param name="rect">長方形</param>
|
/// <param name="radius">角丸の半径</param>
|
/// <param name="leftTop">左上は角丸:true 直角:false</param>
|
/// <param name="rightYop">右上は角丸:true 直角:false</param>
|
/// <param name="rightBottom">右下は角丸:true 直角:false</param>
|
/// <param name="leftBottom">左下は角丸:true 直角:false</param>
|
/// <returns></returns>
|
public static GraphicsPath GetRoundRect(RectangleF rect, float radius, bool leftTop, bool rightYop, bool rightBottom, bool leftBottom)
|
{
|
GraphicsPath path = new GraphicsPath();
|
path.StartFigure();
|
|
// 左上の角丸(※枠は正方形、枠の中心は円心)
|
if (leftTop) { path.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90); }
|
|
// 上の線
|
path.AddLine(rect.Left + (leftTop ? radius : 0), rect.Top, rect.Right - (rightYop ? radius : 0), rect.Top);
|
|
// 右上の角丸
|
if (rightYop) { path.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90); }
|
|
// 右の線
|
path.AddLine(rect.Right, rect.Top + (rightYop ? radius : 0), rect.Right, rect.Bottom - (rightBottom ? radius : 0));
|
|
// 右下の角丸
|
if (rightBottom) { path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90); }
|
|
// 下の線
|
path.AddLine(rect.Right - (rightBottom ? radius : 0), rect.Bottom, rect.Left + (leftBottom ? radius : 0), rect.Bottom);
|
|
// 左下の角丸
|
if (leftBottom) { path.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); }
|
|
// 左の線
|
path.AddLine(rect.Left, rect.Bottom - (leftBottom ? radius : 0), rect.Left, rect.Top + (leftTop ? radius : 0));
|
|
path.CloseFigure();
|
return path;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="rect"></param>
|
/// <param name="cornerRadius">角丸の半径</param>
|
/// <returns></returns>
|
public static GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadius)
|
{
|
GraphicsPath Rect = new GraphicsPath();
|
Rect.StartFigure();
|
Rect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
|
Rect.AddArc(rect.X + rect.Width - cornerRadius * 2 - 1, rect.Y, cornerRadius * 2, cornerRadius * 2, -90, 90);
|
Rect.AddArc(rect.X + rect.Width - cornerRadius * 2 - 1, rect.Y + rect.Height - cornerRadius * 2 - 1, cornerRadius * 2, cornerRadius * 2, 0, 90);
|
Rect.AddArc(rect.X, rect.Bottom - cornerRadius * 2 - 1, cornerRadius * 2, cornerRadius * 2, 90, 90);
|
Rect.CloseFigure();
|
return Rect;
|
}
|
|
/// <summary>
|
/// 対象画像の目標領域より丸い画像を切り出し
|
/// </summary>
|
/// <param name="img">対象画像</param>
|
/// <param name="rec">対象画像の目標領域</param>
|
/// <param name="size">必要な画像サイズ</param>
|
/// <returns></returns>
|
public static Image CutEllipse(Image img, Rectangle rec, Size size)
|
{
|
Bitmap bitmap = new Bitmap(size.Width, size.Height);
|
using (Graphics g = Graphics.FromImage(bitmap))
|
{
|
using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
|
{
|
br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
|
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
g.FillEllipse(br, new Rectangle(Point.Empty, size));
|
}
|
}
|
return bitmap;
|
}
|
}
|
}
|