WinForm利用picturebox控件Paint方法绘制图形 实心圆、实心矩形

编程开发
2200
Graphics
WinForm
PaintEventArgs
  1. 首先创建一个WinForm窗体应用
  2. 拖一个菜单控件(设置几个按钮)和一个picturebox控件(Name属性这里设置为 DrawPanel)
  3. 后台代码如下
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.DrawPanel.Paint += DrawPanel_Paint;
            this.SizeChanged += Form2_SizeChanged;
        }
        private void Form2_SizeChanged(object sender, EventArgs e)
        {
            this.DrawPanel.Refresh();
        }
        enum DrawStyle
        {
            圆形, 方形, 表格,文字
        }
        int effects = 10;//控件边距位置偏移量
        DrawStyle drawStyle;
        private void DrawPanel_Paint(object sender, PaintEventArgs e)
        {
            switch (drawStyle)
            {
                case DrawStyle.圆形:
                    this.DrawYuanXing(e);
                    break;
                case DrawStyle.方形:
                    this.DrawFangXing(e);
                    break;
                case DrawStyle.表格:
                    this.DrawBiaoGe(e);
                    break;
                case DrawStyle.文字:
                    this.DrawText(e);
                    break;
            }
        }
		
        void DrawYuanXing(PaintEventArgs e)
        {
            Brush brush = new SolidBrush(Color.Red);
            Pen pen = new Pen(brush);
            e.Graphics.DrawEllipse(pen, new RectangleF(e.ClipRectangle.Left+effects, 
			e.ClipRectangle.Top +
			effects, e.ClipRectangle.Width -(2* effects), 
			e.ClipRectangle.Height - (2 * effects)));
			//实心填充
            Rectangle r = new Rectangle(e.ClipRectangle.Left + effects, e.ClipRectangle.Top +
            effects, e.ClipRectangle.Width - (2 * effects), e.ClipRectangle.Height - (2 * effects));//标识区域
            e.Graphics.DrawEllipse(pen, r);
            e.Graphics.FillEllipse(brush, r);
        }
        void DrawFangXing(PaintEventArgs e)
        {
            Brush brush = new SolidBrush(Color.Red);
            e.Graphics.FillRectangle(brush, new RectangleF(e.ClipRectangle.Left + effects, 
			e.ClipRectangle.Top 
			+ effects, e.ClipRectangle.Width - (2 * effects), e.ClipRectangle.Height - (2 * effects)));
				//实心填充
			 Pen pen = new Pen(brush);
            Rectangle r = new Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);//标识区域
            e.Graphics.DrawRectangle(pen, r);
            e.Graphics.FillRectangle(brush, r);
        }
        void DrawBiaoGe(PaintEventArgs e)
        {

            Pen TableGridLineColor = new Pen(Color.Red, 2F);
            #region 绘制表格网格线 
            
            //先画外边框 左、上、右、下
         
            e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left + effects, 
			e.ClipRectangle.Top + effects, e.ClipRectangle.Top + effects, e.ClipRectangle.Bottom - effects);
            e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left+ effects, e.ClipRectangle.Top+
			effects, e.ClipRectangle.Right- effects, effects);
			e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Right - effects, e.ClipRectangle.Top
			+ effects, e.ClipRectangle.Right- effects, e.ClipRectangle.Bottom- effects);
			e.Graphics.DrawLine(TableGridLineColor, effects, e.ClipRectangle.Bottom- effects,
			e.ClipRectangle.Right- effects, e.ClipRectangle.Bottom- effects);

            ///绘制网格竖线 从左至右
            int ColumNum = 5;
            int ColumnWidth = e.ClipRectangle.Width/ColumNum;
            for (int i = 0; i < ColumNum; i++)
            {
                if (i <= 0)
                {
                    continue;
                }
                e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Top + (i *ColumnWidth),
				e.ClipRectangle.Top + effects, e.ClipRectangle.Top + (i * ColumnWidth), 
				e.ClipRectangle.Bottom - effects);
            }
            ///绘制网格横线 从上至下
            int RowNum = 5;
            int RowWidth = e.ClipRectangle.Height / RowNum;
            for (int i = 0; i < RowNum; i++)
            {
                if (i <= 0 )
                {
                    continue;

                }
                //绘制网格横线 从上往下
                e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left+ effects, e.ClipRectangle.Top +
				(i*RowWidth), e.ClipRectangle.Right- effects, e.ClipRectangle.Top + (i * RowWidth));
            }
            #endregion
        }

        void DrawText(PaintEventArgs e) {
            Brush brush = new SolidBrush(Color.Red);
            Font font = DefaultFont;

            e.Graphics.DrawString("我是文字内容", DefaultFont, brush, e.ClipRectangle.Width / 2-50,
			e.ClipRectangle.Height / 2-50);
        }

        private void YuanXing_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.圆形;
            this.DrawPanel.Refresh();
        }

        private void FangXing_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.方形;
            this.DrawPanel.Refresh();
        }

        private void WangGe_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.表格;
            this.DrawPanel.Refresh();
        }

        private void TextContent_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.文字;
            this.DrawPanel.Refresh();
        }
    }

运行效果图

1. 绘制圆形(椭圆)

WinForm利用picturebox控件Paint方法绘制图形

2. 绘制矩形

WinForm利用picturebox控件Paint方法绘制图形

3. 绘制网格

WinForm利用picturebox控件Paint方法绘制图形

4. 绘制文本

WinForm利用picturebox控件Paint方法绘制图形

gdi+ Graphics 实现图片拼接排版合并
.NET 5 web环境 Graphics上实现图片实时添加文字水印效果
wpf、winform最大化被任务栏遮挡的解决办法
GDI+ 多图像按指定行列实现图像合并
C# Graphics 获取桌面显示DPI 以及屏幕缩放比例方法
winform  flowlayoutpanel 性能优化 以及 闪烁问题的解决办法
暂无相关内容...
高质量C#+Winform模仿腾讯QQ的截图工具项目源码
编程作业C#+winform实现的仿windows画图工具源码
免责声明 部分转载分享内容若侵犯您的权益,还请 邮件联系 侵删