本文介绍了在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);
}
}
}
推荐答案
这篇关于在picturebox上绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!