如何在图片框中画圆和线?

发布于 2024-08-30 18:46:16 字数 19 浏览 1 评论 0原文

如何在图片框中画圆和线?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

臻嫒无言 2024-09-06 18:46:16

或者:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(
            new Pen(Color.Red,2f), 
            new Point(0,0), 
            new Point(pictureBox1.Size.Width, pictureBox1.Size.Height ));

        e.Graphics.DrawEllipse(
            new Pen(Color.Red, 2f),
            0,0, pictureBox1.Size.Width, pictureBox1.Size.Height  );
    }

处理图片框的绘制事件并在那里进行自定义绘图。

or:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(
            new Pen(Color.Red,2f), 
            new Point(0,0), 
            new Point(pictureBox1.Size.Width, pictureBox1.Size.Height ));

        e.Graphics.DrawEllipse(
            new Pen(Color.Red, 2f),
            0,0, pictureBox1.Size.Width, pictureBox1.Size.Height  );
    }

Handle the paint event of the picture box and do your custom drawing there.

遗忘曾经 2024-09-06 18:46:16

最好的方法是不要在图片框中画圆和线!它不是为此目的而设计的。

来自 Bob Powell 的 GDI+ 博客:

这个问题的根源在于windows的基本规则
编程已被破坏。由于图片框
因一些实际上不是其过错的事情而受到指责。帮忙解释一下
为什么,下面的四点概述了这个案例中出了什么问题。

  • PictureBox 控件用于显示图像。它不是一个方便的图形表面占位符。

  • Windows 是一个事件驱动系统,其中每个事件都必须在正确的上下文中提供服务,并且注定要处理按钮单击或鼠标移动事件的事件不得用于在屏幕上绘图或其他奇怪的事情。

  • PictureBox 通过绘制存储在其 Image 属性中的基于 System.Drawing.Image 的对象来刷新自身。如果没有图像,则会显示背景颜色。

  • 窃取和绘制任何控件的 Graphics 对象都不是好的做法,应强烈反对,并且违反了在正确的时间正确的位置处理事件的规则。基本上,如果你这样做,它会给你带来痛苦。当你用头撞墙时,你会感到疼痛。这是一个信号,表明你应该停止这样做。 PictureBox.CreateGraphics 调用也是如此。

正确的做法。

遵循事件驱动系统的规则很容易,但需要
很少有远见。所以,如果你想画一些
图形并在窗口在其前面移动时将其保留在那里
再次离开或者当您最小化并恢复时,您必须维修
您希望在其上绘画的任何对象的 Paint 事件。
PictureBox 随身携带不必要的行李
这种应用。如果你只是想把一些东西合而为一
放置,通过响应 Form.Paint 事件将其绘制在窗体上。如果
您想要一个方便的占位符,用于在集合中使用图形
边界,使用面板控件并为其 Paint 事件提供服务。如果你想
要为您的企业形象一遍又一遍地复制图形,请创建
控件并在 OnPaint 覆盖中进行绘图。

资料来源: https://web.archive.org/ web/20120330003635/http://bobpowell.net/picturebox.htm(原网站已失效)。

The best way is to NOT draw a circle and line in a picturebox! It is not designed for that purpose.

From Bob Powell's GDI+ blog:

The root of this problem is that the fundamental rules of windows
programming have been broken. And as a consequence of the picture box
is blamed for something that's really not its fault. To help explain
why, the four points below outline what's gone wrong in this case.

  • The PictureBox control is for displaying images. It is not a handy placeholder for a graphics surface.

  • Windows is an event driven system in which each event must be serviced in the correct context and events destined to handle button click or mouse move events must not be used to do drawing on screen or other weird stuff.

  • The PictureBox refreshes itself by drawing the System.Drawing.Image based object stored in it's Image property. If there is no image, it will show the background colour.

  • Stealing and drawing upon the Graphics object of any control is not good practice, should be strongly discouraged and breaks the rules of handling events in the right place at the right time. Basically if you do this it will cause you pain. When you bang your head against a wall it causes you pain. that is a sign that you should stop doing it. It's the same for the PictureBox.CreateGraphics call.

The right way to do it.

Following the rules of the event driven system is easy but requires a
little forethought. So, if you want to draw some little bit of
graphics and have it remain there when a window moves in front of it
and away again or when you minimize and restore, you have to service
the Paint event of whatever object it is that you wish to paint on.
The PictureBox carries baggage around with it that is unnecessary for
this kind of application. If you just want to draw something in one
place, draw it on the form by responding to the Form.Paint event. If
you want a handy placeholder for a graphic that works within a set
bounds, use a Panel control and service it's Paint event. If you want
to duplicate a graphic over and over for your corporate image, create
a control and do the drawing in the OnPaint override.

Source: https://web.archive.org/web/20120330003635/http://bobpowell.net/picturebox.htm (the original site is defunct).

寄居者 2024-09-06 18:46:16

图片框是一个控件,并且有一个图像作为源 - 因此您必须在图像上绘图并将图像交给控件才能显示它

MyImage = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = MyImage;

the picturebox is a control and has an image as source - so you have to draw on the image and hand the image to the control to show it

MyImage = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = MyImage;
小伙你站住 2024-09-06 18:46:16
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Blue);
            int radius = 200;
            int x =Width/2;
            int y =Height/2;


            int first_point1 = (int)(Math.Cos(0) * radius + x);
            int first_point2 = (int)(Math.Sin(0) * radius + y);

            Point p1= new Point(first_point1,first_point2);
            for(int i=1;i<500; i++)
            {
                int dx = (int)(Math.Cos(i)*radius+x );
                int dy = (int)(Math.Sin(i)*radius+y );
                Point p2 = new Point(dx, dy);
                g.DrawLine(p, p1, p2);
                p1 = p2;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Blue);
            int radius = 200;
            int x =Width/2;
            int y =Height/2;


            int first_point1 = (int)(Math.Cos(0) * radius + x);
            int first_point2 = (int)(Math.Sin(0) * radius + y);

            Point p1= new Point(first_point1,first_point2);
            for(int i=1;i<500; i++)
            {
                int dx = (int)(Math.Cos(i)*radius+x );
                int dy = (int)(Math.Sin(i)*radius+y );
                Point p2 = new Point(dx, dy);
                g.DrawLine(p, p1, p2);
                p1 = p2;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文