PictureBox与其他方法的PaintEvent

 Candy王丫丫 发布于 2022-12-07 14:25

在我的表单中只有一个图片框,我想用这个图片框上的方法绘制圆圈,但我不能这样做而不工作.方法是:

private Bitmap Circle()
    {
        Bitmap bmp;
        Graphics gfx;
        SolidBrush firca_dis=new SolidBrush(Color.FromArgb(192,0,192));

            bmp = new Bitmap(40, 40);
            gfx = Graphics.FromImage(bmp);
            gfx.FillRectangle(firca_dis, 0, 0, 40, 40);

        return bmp;
    }

图片框

 private void pictureBox2_Paint(object sender, PaintEventArgs e)
    {
        Graphics gfx= Graphics.FromImage(Circle());
        gfx=e.Graphics;
    }

TaW.. 5

你需要决定你想做什么:

入图像

吸引控制

你的代码是两者的混合,这就是为什么它不起作用.

下面是如何画Control:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44));
    ..
}

下面是如何绘制ImagePictureBox::

void drawIntoImage()
{
    using (Graphics G = Graphics.FromImage(pictureBox1.Image))
    {
        G.DrawEllipse(Pens.Orange, new Rectangle(13, 14, 44, 44));
        ..
    }
    // when done with all drawing you can enforce the display update by calling:
    pictureBox1.Refresh();
}

两种绘制方式都是持久的.后者改变为Image的像素,前者不是.

因此,如果像素被绘制到图像中并且您缩放,拉伸或移动图像,则像素将随之移动.绘制在PictureBox控件顶部的像素不会这样做!

当然,对于这两种方式来绘制,你可以改变所有常见的部件,如绘图命令,也许增加一个FillEllipse之前DrawEllipse,在PensBrushes他们的画笔类型,Colors并且尺寸..

1 个回答
  • 你需要决定你想做什么:

    入图像

    吸引控制

    你的代码是两者的混合,这就是为什么它不起作用.

    下面是如何画Control:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44));
        ..
    }
    

    下面是如何绘制ImagePictureBox::

    void drawIntoImage()
    {
        using (Graphics G = Graphics.FromImage(pictureBox1.Image))
        {
            G.DrawEllipse(Pens.Orange, new Rectangle(13, 14, 44, 44));
            ..
        }
        // when done with all drawing you can enforce the display update by calling:
        pictureBox1.Refresh();
    }
    

    两种绘制方式都是持久的.后者改变为Image的像素,前者不是.

    因此,如果像素被绘制到图像中并且您缩放,拉伸或移动图像,则像素将随之移动.绘制在PictureBox控件顶部的像素不会这样做!

    当然,对于这两种方式来绘制,你可以改变所有常见的部件,如绘图命令,也许增加一个FillEllipse之前DrawEllipse,在PensBrushes他们的画笔类型,Colors并且尺寸..

    2022-12-11 02:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有