在面板内的 PictureBox 上绘图 - C#

发布于 2024-12-18 09:35:40 字数 1763 浏览 3 评论 0原文

我这里的问题有点像两个人。

我有一个位于面板内部的图片框。当我打开图像时,图片框的大小会调整为图像的大小,而面板保持相同的大小。该面板只有滚动条来查看整个图像。

这有两件事出了问题。

  1. 当我调整图片框的大小时,由于某种原因我只能在图片框的前一部分中绘制。前任。默认情况下,图像框的起始尺寸为 200x200。我打开一张 500x400 的图像。而且我仍然只能在图像的 200x200 部分进行绘制。

  2. 我遇到的第二个问题是,当我在图片框的选定部分中绘制时,当我滚动到我的绘画不在视图中的位置并返回时,我绘制的图像消失了。我知道我需要某种 picturebox.invalidate() 。我只是不知道如何使用它。

这是我的代码,可以很好地理解我正在做的事情。

public Form1()
    {
        InitializeComponent();

        DrawArea = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height );
        pictureBox1.Image = DrawArea;

        objGraphics = this.pictureBox1.CreateGraphics();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
            drawImage(e);
    }

    public void drawImage(MouseEventArgs e)
    {
        Rectangle rDraw = new Rectangle();

        if (e.Button == MouseButtons.Left)
        {
            rDraw.X = e.X;
            rDraw.Y = e.Y;
            rDraw.Width = 3;
            rDraw.Height = 3;
            objGraphics.DrawEllipse(System.Drawing.Pens.Black, rDraw);
        }
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.bmp)|*.jpg; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                Bitmap bit = new Bitmap(open.FileName);

                pictureBox1.Size = bit.Size;
                DrawArea = bit;
                pictureBox1.Image = bit;
            }
        }
        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
    }

多谢!

The question I have here is sort of a 2 parter.

I have a picturebox that is positioned inside of a panel. When I open an image, the picturebox is resized to the size of the image, while the panel stays the same size. The panel just has scrollbars to see the whole image.

There are 2 things going wrong with this.

  1. When I resize the picturebox, for some reason I can only draw in the previous portion of the picturebox. Ex. The imagebox starts out by default as 200x200. I open an image that is 500x400. And I can only still draw in the 200x200 portion of the image.

  2. The second issue that I am having is that when I do draw in that selective portion of the picturebox, when I scroll to where my painting is out of view, and come back, the image that i painted gone. I know there is some sort of picturebox.invalidate() that I need. I am just not sure how to use it.

Here is my code to get a good grasp on what I'm doing.

public Form1()
    {
        InitializeComponent();

        DrawArea = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height );
        pictureBox1.Image = DrawArea;

        objGraphics = this.pictureBox1.CreateGraphics();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
            drawImage(e);
    }

    public void drawImage(MouseEventArgs e)
    {
        Rectangle rDraw = new Rectangle();

        if (e.Button == MouseButtons.Left)
        {
            rDraw.X = e.X;
            rDraw.Y = e.Y;
            rDraw.Width = 3;
            rDraw.Height = 3;
            objGraphics.DrawEllipse(System.Drawing.Pens.Black, rDraw);
        }
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.bmp)|*.jpg; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                Bitmap bit = new Bitmap(open.FileName);

                pictureBox1.Size = bit.Size;
                DrawArea = bit;
                pictureBox1.Image = bit;
            }
        }
        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
    }

Thanks Alot!

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

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

发布评论

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

评论(1

吻泪 2024-12-25 09:35:40

您需要在图片框的 Paint 事件中进行绘制。

您不应该(几乎)永远不要使用 CreateGraphics() 进行绘制。

You need to draw in the picturebox's Paint event.

You should (almost) never draw on CreateGraphics().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文