ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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;
        }
    }
}