using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace HotelPms.Share.Windows.GraphicsApi { /// /// UTF8中文转码 /// public static class GdiPlus { /// /// 等比缩放图片,画到目标区域 /// /// /// /// 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(); } } /// /// 円角長方形 /// /// /// /// /// 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; //要らない!!! } } /// /// 円角長方形 /// /// /// /// /// 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; //要らない!!! } } /// ///角丸長方形のGraphicsPathを作成します。 /// /// 長方形 /// 角丸の半径 /// 左上は角丸:true 直角:false /// 右上は角丸:true 直角:false /// 右下は角丸:true 直角:false /// 左下は角丸:true 直角:false /// 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; } /// /// /// /// /// 角丸の半径 /// 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; } /// /// 対象画像の目標領域より丸い画像を切り出し /// /// 対象画像 /// 対象画像の目標領域 /// 必要な画像サイズ /// 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; } } }