首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PictureBox PaintEvent和其他方法

PictureBox PaintEvent和其他方法
EN

Stack Overflow用户
提问于 2014-12-06 23:38:13
回答 2查看 5.4K关注 0票数 1

我的表单中只有一个picturebox,我想在这个picturebox上画一个方法,但是我不能这样做,而不是working.The方法:

代码语言:javascript
复制
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;
    }

图片盒

代码语言:javascript
复制
 private void pictureBox2_Paint(object sender, PaintEventArgs e)
    {
        Graphics gfx= Graphics.FromImage(Circle());
        gfx=e.Graphics;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-07 10:40:28

你需要决定你想做什么:

  • 绘制到图像
  • 绘制到控件

您的代码是两者的混合,这就是它不能工作的原因。

下面是如何将绘制到 Control

代码语言:javascript
复制
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawEllipse(Pens.Red, new Rectangle(3, 4, 44, 44));
    ..
}

下面是如何将绘制到中-- PictureBoxImage

代码语言:javascript
复制
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();
}

这两种绘画方式都是持久的。后者改变为图像的像素,前者不改变。

因此,如果像素被绘制到图像中,然后缩放、拉伸或移动图像,像素就会随之而去。绘制到PictureBox控件顶部的像素不会这样做!

当然,对于这两种绘制方式,您都可以更改所有常用的部分,比如绘图命令,可以在FillEllipse之前添加一个DrawEllipsePensBrushes,以及它们的刷类型和Colors以及维度。

票数 5
EN

Stack Overflow用户

发布于 2014-12-07 02:24:51

代码语言:javascript
复制
private static void DrawCircle(Graphics gfx)
{    
    SolidBrush firca_dis = new SolidBrush(Color.FromArgb(192, 0, 192));
    Rectangle rec = new Rectangle(0, 0, 40, 40); //Size and location of the Circle

    gfx.FillEllipse(firca_dis, rec); //Draw a Circle and fill it
    gfx.DrawEllipse(new Pen(firca_dis), rec); //draw a the border of the cicle your choice
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27337825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档