s
o
u
l
s
j
i
e

C#GDI+阴影笔刷样式HatchStyle探讨

命名空间:System.Drawing.Drawing2D.HatchBrush

在创建笔刷时,需要传入参数

1.阴影样式HatchStyle

2.前景色

3.背景色

Brush brs1 = new HatchBrush(参数1,参数2, 参数3);

参数1中HatchStyle为枚举值,且枚举很多有0-52,突发奇想,想试试每种笔刷的效果,结果如下,可供参考:

测试代码,使用循环生成枚举笔刷绘制:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string code = GetAllBrushCode();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Point start = new Point(50, 50);
            Point end = new Point(300, 50);
            //前景色
            Color color1 = Color.Blue;
            //背景色
            Color color2 = Color.Yellow;
            //每根线之间的间距
            int margin = 10;
            //每个笔刷的宽度
            int pwidth = 10;
            //笔刷
            //1.颜色笔刷
            Brush br1 = new SolidBrush(Color.Black);
            Pen p1 = new Pen(br1, pwidth);
            g.DrawLine(p1, NewPoint(start, 0), NewPoint(end, 0));
            //2.阴影画刷
            //复制code生成的代码开始
            //样式1
            Brush brs1 = new HatchBrush((HatchStyle)0, color1, color2);
            Pen ps1 = new Pen(brs1, pwidth);
            g.DrawLine(ps1, NewPoint(start, 22), NewPoint(end, 22));
            g.DrawString("HatchStyle.Horizontal", new Font("微软雅黑", 8), br1, NewPoint(end, 23));
            //样式2
            Brush brs2 = new HatchBrush((HatchStyle)1, color1, color2);
            Pen ps2 = new Pen(brs2, pwidth);
            g.DrawLine(ps2, NewPoint(start, 44), NewPoint(end, 44));
            g.DrawString("HatchStyle.Vertical", new Font("微软雅黑", 8), br1, NewPoint(end, 46));
            //样式3
            Brush brs3 = new HatchBrush((HatchStyle)2, color1, color2);
            Pen ps3 = new Pen(brs3, pwidth);
            g.DrawLine(ps3, NewPoint(start, 66), NewPoint(end, 66));
            g.DrawString("HatchStyle.ForwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 69));
            //样式4
            Brush brs4 = new HatchBrush((HatchStyle)3, color1, color2);
            Pen ps4 = new Pen(brs4, pwidth);
            g.DrawLine(ps4, NewPoint(start, 88), NewPoint(end, 88));
            g.DrawString("HatchStyle.BackwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 92));
            //样式5
            Brush brs5 = new HatchBrush((HatchStyle)4, color1, color2);
            Pen ps5 = new Pen(brs5, pwidth);
            g.DrawLine(ps5, NewPoint(start, 110), NewPoint(end, 110));
            g.DrawString("HatchStyle.LargeGrid", new Font("微软雅黑", 8), br1, NewPoint(end, 115));
            //样式6
            Brush brs6 = new HatchBrush((HatchStyle)5, color1, color2);
            Pen ps6 = new Pen(brs6, pwidth);
            g.DrawLine(ps6, NewPoint(start, 132), NewPoint(end, 132));
            g.DrawString("HatchStyle.DiagonalCross", new Font("微软雅黑", 8), br1, NewPoint(end, 138));
            //样式7
            Brush brs7 = new HatchBrush((HatchStyle)6, color1, color2);
            Pen ps7 = new Pen(brs7, pwidth);
            g.DrawLine(ps7, NewPoint(start, 154), NewPoint(end, 154));
            g.DrawString("HatchStyle.Percent05", new Font("微软雅黑", 8), br1, NewPoint(end, 161));
            //样式8
            Brush brs8 = new HatchBrush((HatchStyle)7, color1, color2);
            Pen ps8 = new Pen(brs8, pwidth);
            g.DrawLine(ps8, NewPoint(start, 176), NewPoint(end, 176));
            g.DrawString("HatchStyle.Percent10", new Font("微软雅黑", 8), br1, NewPoint(end, 184));
            //样式9
            Brush brs9 = new HatchBrush((HatchStyle)8, color1, color2);
            Pen ps9 = new Pen(brs9, pwidth);
            g.DrawLine(ps9, NewPoint(start, 198), NewPoint(end, 198));
            g.DrawString("HatchStyle.Percent20", new Font("微软雅黑", 8), br1, NewPoint(end, 207));
            //样式10
            Brush brs10 = new HatchBrush((HatchStyle)9, color1, color2);
            Pen ps10 = new Pen(brs10, pwidth);
            g.DrawLine(ps10, NewPoint(start, 220), NewPoint(end, 220));
            g.DrawString("HatchStyle.Percent25", new Font("微软雅黑", 8), br1, NewPoint(end, 230));
            //样式11
            Brush brs11 = new HatchBrush((HatchStyle)10, color1, color2);
            Pen ps11 = new Pen(brs11, pwidth);
            g.DrawLine(ps11, NewPoint(start, 242), NewPoint(end, 242));
            g.DrawString("HatchStyle.Percent30", new Font("微软雅黑", 8), br1, NewPoint(end, 253));
            //样式12
            Brush brs12 = new HatchBrush((HatchStyle)11, color1, color2);
            Pen ps12 = new Pen(brs12, pwidth);
            g.DrawLine(ps12, NewPoint(start, 264), NewPoint(end, 264));
            g.DrawString("HatchStyle.Percent40", new Font("微软雅黑", 8), br1, NewPoint(end, 276));
            //样式13
            Brush brs13 = new HatchBrush((HatchStyle)12, color1, color2);
            Pen ps13 = new Pen(brs13, pwidth);
            g.DrawLine(ps13, NewPoint(start, 286), NewPoint(end, 286));
            g.DrawString("HatchStyle.Percent50", new Font("微软雅黑", 8), br1, NewPoint(end, 299));
            //样式14
            Brush brs14 = new HatchBrush((HatchStyle)13, color1, color2);
            Pen ps14 = new Pen(brs14, pwidth);
            g.DrawLine(ps14, NewPoint(start, 308), NewPoint(end, 308));
            g.DrawString("HatchStyle.Percent60", new Font("微软雅黑", 8), br1, NewPoint(end, 322));
            //样式15
            Brush brs15 = new HatchBrush((HatchStyle)14, color1, color2);
            Pen ps15 = new Pen(brs15, pwidth);
            g.DrawLine(ps15, NewPoint(start, 330), NewPoint(end, 330));
            g.DrawString("HatchStyle.Percent70", new Font("微软雅黑", 8), br1, NewPoint(end, 345));
            //样式16
            Brush brs16 = new HatchBrush((HatchStyle)15, color1, color2);
            Pen ps16 = new Pen(brs16, pwidth);
            g.DrawLine(ps16, NewPoint(start, 352), NewPoint(end, 352));
            g.DrawString("HatchStyle.Percent75", new Font("微软雅黑", 8), br1, NewPoint(end, 368));
            //样式17
            Brush brs17 = new HatchBrush((HatchStyle)16, color1, color2);
            Pen ps17 = new Pen(brs17, pwidth);
            g.DrawLine(ps17, NewPoint(start, 374), NewPoint(end, 374));
            g.DrawString("HatchStyle.Percent80", new Font("微软雅黑", 8), br1, NewPoint(end, 391));
            //样式18
            Brush brs18 = new HatchBrush((HatchStyle)17, color1, color2);
            Pen ps18 = new Pen(brs18, pwidth);
            g.DrawLine(ps18, NewPoint(start, 396), NewPoint(end, 396));
            g.DrawString("HatchStyle.Percent90", new Font("微软雅黑", 8), br1, NewPoint(end, 414));
            //样式19
            Brush brs19 = new HatchBrush((HatchStyle)18, color1, color2);
            Pen ps19 = new Pen(brs19, pwidth);
            g.DrawLine(ps19, NewPoint(start, 418), NewPoint(end, 418));
            g.DrawString("HatchStyle.LightDownwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 437));
            //样式20
            Brush brs20 = new HatchBrush((HatchStyle)19, color1, color2);
            Pen ps20 = new Pen(brs20, pwidth);
            g.DrawLine(ps20, NewPoint(start, 440), NewPoint(end, 440));
            g.DrawString("HatchStyle.LightUpwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 460));
            //样式21
            Brush brs21 = new HatchBrush((HatchStyle)20, color1, color2);
            Pen ps21 = new Pen(brs21, pwidth);
            g.DrawLine(ps21, NewPoint(start, 462), NewPoint(end, 462));
            g.DrawString("HatchStyle.DarkDownwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 483));
            //样式22
            Brush brs22 = new HatchBrush((HatchStyle)21, color1, color2);
            Pen ps22 = new Pen(brs22, pwidth);
            g.DrawLine(ps22, NewPoint(start, 484), NewPoint(end, 484));
            g.DrawString("HatchStyle.DarkUpwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 506));
            //样式23
            Brush brs23 = new HatchBrush((HatchStyle)22, color1, color2);
            Pen ps23 = new Pen(brs23, pwidth);
            g.DrawLine(ps23, NewPoint(start, 506), NewPoint(end, 506));
            g.DrawString("HatchStyle.WideDownwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 529));
            //样式24
            Brush brs24 = new HatchBrush((HatchStyle)23, color1, color2);
            Pen ps24 = new Pen(brs24, pwidth);
            g.DrawLine(ps24, NewPoint(start, 528), NewPoint(end, 528));
            g.DrawString("HatchStyle.WideUpwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 552));
            //样式25
            Brush brs25 = new HatchBrush((HatchStyle)24, color1, color2);
            Pen ps25 = new Pen(brs25, pwidth);
            g.DrawLine(ps25, NewPoint(start, 550), NewPoint(end, 550));
            g.DrawString("HatchStyle.LightVertical", new Font("微软雅黑", 8), br1, NewPoint(end, 575));
            //样式26
            Brush brs26 = new HatchBrush((HatchStyle)25, color1, color2);
            Pen ps26 = new Pen(brs26, pwidth);
            g.DrawLine(ps26, NewPoint(start, 572), NewPoint(end, 572));
            g.DrawString("HatchStyle.LightHorizontal", new Font("微软雅黑", 8), br1, NewPoint(end, 598));
            //样式27
            Brush brs27 = new HatchBrush((HatchStyle)26, color1, color2);
            Pen ps27 = new Pen(brs27, pwidth);
            g.DrawLine(ps27, NewPoint(start, 594), NewPoint(end, 594));
            g.DrawString("HatchStyle.NarrowVertical", new Font("微软雅黑", 8), br1, NewPoint(end, 621));
            //样式28
            Brush brs28 = new HatchBrush((HatchStyle)27, color1, color2);
            Pen ps28 = new Pen(brs28, pwidth);
            g.DrawLine(ps28, NewPoint(start, 616), NewPoint(end, 616));
            g.DrawString("HatchStyle.NarrowHorizontal", new Font("微软雅黑", 8), br1, NewPoint(end, 644));
            //样式29
            Brush brs29 = new HatchBrush((HatchStyle)28, color1, color2);
            Pen ps29 = new Pen(brs29, pwidth);
            g.DrawLine(ps29, NewPoint(start, 638), NewPoint(end, 638));
            g.DrawString("HatchStyle.DarkVertical", new Font("微软雅黑", 8), br1, NewPoint(end, 667));
            //样式30
            Brush brs30 = new HatchBrush((HatchStyle)29, color1, color2);
            Pen ps30 = new Pen(brs30, pwidth);
            g.DrawLine(ps30, NewPoint(start, 660), NewPoint(end, 660));
            g.DrawString("HatchStyle.DarkHorizontal", new Font("微软雅黑", 8), br1, NewPoint(end, 690));
            //样式31
            Brush brs31 = new HatchBrush((HatchStyle)30, color1, color2);
            Pen ps31 = new Pen(brs31, pwidth);
            g.DrawLine(ps31, NewPoint(start, 682), NewPoint(end, 682));
            g.DrawString("HatchStyle.DashedDownwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 713));
            //样式32
            Brush brs32 = new HatchBrush((HatchStyle)31, color1, color2);
            Pen ps32 = new Pen(brs32, pwidth);
            g.DrawLine(ps32, NewPoint(start, 704), NewPoint(end, 704));
            g.DrawString("HatchStyle.DashedUpwardDiagonal", new Font("微软雅黑", 8), br1, NewPoint(end, 736));
            //样式33
            Brush brs33 = new HatchBrush((HatchStyle)32, color1, color2);
            Pen ps33 = new Pen(brs33, pwidth);
            g.DrawLine(ps33, NewPoint(start, 726), NewPoint(end, 726));
            g.DrawString("HatchStyle.DashedHorizontal", new Font("微软雅黑", 8), br1, NewPoint(end, 759));
            //样式34
            Brush brs34 = new HatchBrush((HatchStyle)33, color1, color2);
            Pen ps34 = new Pen(brs34, pwidth);
            g.DrawLine(ps34, NewPoint(start, 748), NewPoint(end, 748));
            g.DrawString("HatchStyle.DashedVertical", new Font("微软雅黑", 8), br1, NewPoint(end, 782));
            //样式35
            Brush brs35 = new HatchBrush((HatchStyle)34, color1, color2);
            Pen ps35 = new Pen(brs35, pwidth);
            g.DrawLine(ps35, NewPoint(start, 770), NewPoint(end, 770));
            g.DrawString("HatchStyle.SmallConfetti", new Font("微软雅黑", 8), br1, NewPoint(end, 805));
            //样式36
            Brush brs36 = new HatchBrush((HatchStyle)35, color1, color2);
            Pen ps36 = new Pen(brs36, pwidth);
            g.DrawLine(ps36, NewPoint(start, 792), NewPoint(end, 792));
            g.DrawString("HatchStyle.LargeConfetti", new Font("微软雅黑", 8), br1, NewPoint(end, 828));
            //样式37
            Brush brs37 = new HatchBrush((HatchStyle)36, color1, color2);
            Pen ps37 = new Pen(brs37, pwidth);
            g.DrawLine(ps37, NewPoint(start, 814), NewPoint(end, 814));
            g.DrawString("HatchStyle.ZigZag", new Font("微软雅黑", 8), br1, NewPoint(end, 851));
            //样式38
            Brush brs38 = new HatchBrush((HatchStyle)37, color1, color2);
            Pen ps38 = new Pen(brs38, pwidth);
            g.DrawLine(ps38, NewPoint(start, 836), NewPoint(end, 836));
            g.DrawString("HatchStyle.Wave", new Font("微软雅黑", 8), br1, NewPoint(end, 874));
            //样式39
            Brush brs39 = new HatchBrush((HatchStyle)38, color1, color2);
            Pen ps39 = new Pen(brs39, pwidth);
            g.DrawLine(ps39, NewPoint(start, 858), NewPoint(end, 858));
            g.DrawString("HatchStyle.DiagonalBrick", new Font("微软雅黑", 8), br1, NewPoint(end, 897));
            //样式40
            Brush brs40 = new HatchBrush((HatchStyle)39, color1, color2);
            Pen ps40 = new Pen(brs40, pwidth);
            g.DrawLine(ps40, NewPoint(start, 880), NewPoint(end, 880));
            g.DrawString("HatchStyle.HorizontalBrick", new Font("微软雅黑", 8), br1, NewPoint(end, 920));
            //样式41
            Brush brs41 = new HatchBrush((HatchStyle)40, color1, color2);
            Pen ps41 = new Pen(brs41, pwidth);
            g.DrawLine(ps41, NewPoint(start, 902), NewPoint(end, 902));
            g.DrawString("HatchStyle.Weave", new Font("微软雅黑", 8), br1, NewPoint(end, 943));
            //样式42
            Brush brs42 = new HatchBrush((HatchStyle)41, color1, color2);
            Pen ps42 = new Pen(brs42, pwidth);
            g.DrawLine(ps42, NewPoint(start, 924), NewPoint(end, 924));
            g.DrawString("HatchStyle.Plaid", new Font("微软雅黑", 8), br1, NewPoint(end, 966));
            //样式43
            Brush brs43 = new HatchBrush((HatchStyle)42, color1, color2);
            Pen ps43 = new Pen(brs43, pwidth);
            g.DrawLine(ps43, NewPoint(start, 946), NewPoint(end, 946));
            g.DrawString("HatchStyle.Divot", new Font("微软雅黑", 8), br1, NewPoint(end, 989));
            //样式44
            Brush brs44 = new HatchBrush((HatchStyle)43, color1, color2);
            Pen ps44 = new Pen(brs44, pwidth);
            g.DrawLine(ps44, NewPoint(start, 968), NewPoint(end, 968));
            g.DrawString("HatchStyle.DottedGrid", new Font("微软雅黑", 8), br1, NewPoint(end, 1012));
            //样式45
            Brush brs45 = new HatchBrush((HatchStyle)44, color1, color2);
            Pen ps45 = new Pen(brs45, pwidth);
            g.DrawLine(ps45, NewPoint(start, 990), NewPoint(end, 990));
            g.DrawString("HatchStyle.DottedDiamond", new Font("微软雅黑", 8), br1, NewPoint(end, 1035));
            //样式46
            Brush brs46 = new HatchBrush((HatchStyle)45, color1, color2);
            Pen ps46 = new Pen(brs46, pwidth);
            g.DrawLine(ps46, NewPoint(start, 1012), NewPoint(end, 1012));
            g.DrawString("HatchStyle.Shingle", new Font("微软雅黑", 8), br1, NewPoint(end, 1058));
            //样式47
            Brush brs47 = new HatchBrush((HatchStyle)46, color1, color2);
            Pen ps47 = new Pen(brs47, pwidth);
            g.DrawLine(ps47, NewPoint(start, 1034), NewPoint(end, 1034));
            g.DrawString("HatchStyle.Trellis", new Font("微软雅黑", 8), br1, NewPoint(end, 1081));
            //样式48
            Brush brs48 = new HatchBrush((HatchStyle)47, color1, color2);
            Pen ps48 = new Pen(brs48, pwidth);
            g.DrawLine(ps48, NewPoint(start, 1056), NewPoint(end, 1056));
            g.DrawString("HatchStyle.Sphere", new Font("微软雅黑", 8), br1, NewPoint(end, 1104));
            //样式49
            Brush brs49 = new HatchBrush((HatchStyle)48, color1, color2);
            Pen ps49 = new Pen(brs49, pwidth);
            g.DrawLine(ps49, NewPoint(start, 1078), NewPoint(end, 1078));
            g.DrawString("HatchStyle.SmallGrid", new Font("微软雅黑", 8), br1, NewPoint(end, 1127));
            //样式50
            Brush brs50 = new HatchBrush((HatchStyle)49, color1, color2);
            Pen ps50 = new Pen(brs50, pwidth);
            g.DrawLine(ps50, NewPoint(start, 1100), NewPoint(end, 1100));
            g.DrawString("HatchStyle.SmallCheckerBoard", new Font("微软雅黑", 8), br1, NewPoint(end, 1150));
            //样式51
            Brush brs51 = new HatchBrush((HatchStyle)50, color1, color2);
            Pen ps51 = new Pen(brs51, pwidth);
            g.DrawLine(ps51, NewPoint(start, 1122), NewPoint(end, 1122));
            g.DrawString("HatchStyle.LargeCheckerBoard", new Font("微软雅黑", 8), br1, NewPoint(end, 1173));
            //样式52
            Brush brs52 = new HatchBrush((HatchStyle)51, color1, color2);
            Pen ps52 = new Pen(brs52, pwidth);
            g.DrawLine(ps52, NewPoint(start, 1144), NewPoint(end, 1144));
            g.DrawString("HatchStyle.OutlinedDiamond", new Font("微软雅黑", 8), br1, NewPoint(end, 1196));

            //复制code生成的代码结束
        }

        /// <summary>
        /// 将传入的点往y轴方向下移一定的距离
        /// </summary>
        /// <param name="offset"></param>
        private Point NewPoint(Point p, int offset)
        {
            p.Y += offset;
            return p;
        }

        private string GetAllBrushCode()
        {
            string a = "";
            for (int i = 1; i <= 52; i++)
            {
                a += "//样式" + i.ToString() + "\r\n";
                a += "Brush brs" + i.ToString() + " = new HatchBrush((HatchStyle)" + (i - 1).ToString() + ", color1, color2);\r\n";
                a += "Pen ps" + i.ToString() + " = new Pen(brs" + i.ToString() + ", pwidth);\r\n";
                a += "g.DrawLine(ps" + i.ToString() + ", NewPoint(start, " + (i * 22).ToString() + "), NewPoint(end, " + (i * 22).ToString() + "));\r\n";
                a += "g.DrawString(\"HatchStyle." + getBrushName(i - 1) + "\", new Font(\"微软雅黑\", 8), br1, NewPoint(end, " + (i * 23).ToString() + "));\r\n";

            }
            return a;
        }

        private string getBrushName(int index)
        {
            HatchStyle temp = (HatchStyle)index;
            return temp.ToString();
        }
    }

  

posted @ 2023-07-12 15:55  soulsjie  阅读(497)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43