在picturebox上绘画 [英] Painting on picturebox

查看:107
本文介绍了在picturebox上绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在图片框上绘画?



How can I paint on picturebox?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace howto_polygon_geometry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const int PT_RAD = 2;
        const int PT_WID = PT_RAD * 2 + 1;

        private List<PointF> m_Points = new List<PointF>();

        // Draw the polygon.
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);

            // Draw the lines.
            if (m_Points.Count >= 3)
            {
                // Draw the polygon.
                e.Graphics.DrawPolygon(Pens.Blue, m_Points.ToArray());
            }
            else if (m_Points.Count == 2)
            {
                // Draw the line.
                e.Graphics.DrawLines(Pens.Blue, m_Points.ToArray());
            }

            // Draw the points.
            if (m_Points.Count > 0)
            {
                foreach (PointF pt in m_Points)
                {
                    e.Graphics.FillRectangle(Brushes.White, pt.X - PT_RAD, pt.Y - PT_RAD, PT_WID, PT_WID);
                    e.Graphics.DrawRectangle(Pens.Black, pt.X - PT_RAD, pt.Y - PT_RAD, PT_WID, PT_WID);
                }
            }

            // Enable menu items appropriately.
            EnableMenus();
        }

        // Enable menu items appropriately.
        private void EnableMenus()
        {
            bool enabled = (m_Points.Count >= 3);
            mnuTestsArea.Enabled = enabled;
        }

        // Remove all points.
        private void mnuTestsClear_Click(object sender, EventArgs e)
        {
            m_Points = new List<PointF>();
            EnableMenus();
            this.Invalidate();
        }

        // Save a new point.
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            m_Points.Add(new PointF(e.X, e.Y));

            // Redraw.
            this.Invalidate();
        }

        // Find the polygon's area.
        private void mnuTestsArea_Click(object sender, EventArgs e)
        {
            // Make a Polygon.
            Polygon pgon = new Polygon(m_Points.ToArray());

            MessageBox.Show("Area: " + pgon.PolygonArea().ToString(), "Area",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}

推荐答案

查看这些

在图片框上绘图 [ ^ ]



在c#上的Picturebox上画线 [ ^ ]



在图片框中的现有图像上绘制线条 [ ^ ]



如何使用鼠标在c#上绘制图片框图像 [ ^ ]



如何在图片框中绘制圆圈和线? [ ^ ]
check these
Draw on picturebox[^]

Draw Lines on a Picturebox in c#[^]

Draw lines over existing image in picture box[^]

How to draw on a picturebox image using mouse by c#[^]

How do I draw a circle and line in the picturebox?[^]


这篇关于在picturebox上绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆