简体   繁体   English

如何在PictureBox中保留图像

[英]How to keep the image Drawn in PictureBox

Hey everyone, a new guy here in C#.Net. 嘿大家,这是C#.Net的新人。

I'm trying to make an application like Ms Paint, of course much simpler, and I'm stuck. 我正在尝试制作像Paint这样的应用程序,当然要简单得多,而且我被卡住了。

The problem is this. 问题是这个。

In pictureBox, I'm drawing grid lines on the PictureBox, after that I'm reading a .map(A Mapper3 file) and want to draw onto grid lines, but When I draw the map, The grid lines disappers. 在pictureBox中,我在PictureBox上绘制网格线,之后我正在读取.map(一个Mapper3文件)并想要绘制到网格线上,但是当我绘制地图时,网格线消失了。

I think the problem is because of the PictureBox Image becomes null while I'm drawing the map. 我认为问题是因为PictureBox Image在我绘制地图时变为null。 How can I overcome this, is there any tricks? 我怎么能克服这一点,有什么窍门吗?

Thanks for the replies from now on, and sorry for my bad English... 感谢你们从现在开始的回复,对不起我的英语不好......

My best Regards... 我最诚挚的问候...

Do you using winforms? 你使用winforms吗? If yes, you actually dont need picture box for working area. 如果是的话,你实际上不需要工作区的图片框。 I think more appropriate would be Graphics class on form or panel. 我认为更合适的是表单或面板上的Graphics类。 You have lost lines because of form repaint circle, put your drawing code into form paint handler and picture would be repainted when it needed. 由于形式重绘圆圈,您丢失了线条,将您的绘图代码放入表单绘制处理程序,图片将在需要时重新绘制。 In some cases you can need to manual trigger repaint circle, for this purposes you should use Invalidate method of your form. 在某些情况下,您可能需要手动触发重绘圈,为此您应该使用表单的Invalidate方法。

For example, add this code to paint handler: 例如,将此代码添加到绘制处理程序:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Drawing vertical lines
        for (int x = 5; x < this.ClientRectangle.Width; x+=5)
        {
            e.Graphics.DrawLine(Pens.Gray, new Point(x, 0), new Point(x, this.ClientRectangle.Height));
        }

        // Drawing horisontal lines
        for (int y = 5; y < this.ClientRectangle.Width; y += 5)
        {
            e.Graphics.DrawLine(Pens.Gray, new Point(0, y), new Point(this.ClientRectangle.Width,y));
        }
    }

You also may use Graphics in button click handler this way: 您也可以通过这种方式在按钮单击处理程序中使用Graphics:

Graphics g = Graphics.FromHwnd(this.Handle);

g.FillEllipse(Brushes.Beige, new Rectangle(10, 10, 10, 10));

But in this case all you have drawn would be erased during form's repaint circle and you will have to repeint it in form paint handler 但是在这种情况下,你所绘制的所有内容都将在表单的重绘圆圈中被删除,你将不得不在表单绘制处理程序中重复它

[EDIT] Ok, for example you have pictureBox1 on your form, you can easly draw into it with help of Bitmap class in this way: [编辑]好的,例如你的表单上有pictureBox1,你可以通过这种方式借助Bitmap类轻松地绘制它:

// Draw into bitmap
Bitmap bmp = new Bitmap(150, 150);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Green, new Rectangle(25, 75, 10, 30));

// Set bitmap into picture box
pictureBox1.Image = bmp;

In this case you have no need to redraw your paintings, picture box would do it for you. 在这种情况下,你不需要重绘你的画作,图片框会为你做。 Dont forget to set BackColor ot picture box to Transparent if you prefer to show paintings from below of picture box. 如果您希望从图片框下方显示绘画,请不要忘记将BackColor ot图片框设置为Transparent

You have to draw everything including the grid lines whenever the paint event raised, if you are concerned about performance you may detect the clipping area and only draw that portion. 每当paint事件引发时,您都必须绘制包括网格线在内的所有内容,如果您关注性能,则可以检测到剪切区域并仅绘制该部分。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM