0

我想在图片框中画一些东西并删除它们。在这种情况下,我需要绘制一个填充圆,一段时间后将其移除并绘制一个不填充的圆。

我使用下面的代码,但是当我想删除一个形状时,我需要用新的形状再次绘制图片框,这会导致图片框发生敏感的变化。现在我知道我应该使用 invalidate() 方法。但我不知道我应该在哪里以及如何使用它。

void pbmapDo()
    {
        Graphics graphicPBMap  = pbMap.CreateGraphics();
        // usually Values : gridNeedUpdate = true; rulersNeedUpdate = true; rulerNeedUpdate = true; backGroundNeedUpdate = true; nodesNeedUpdate = true;
        if (backGroundNeedUpdate)
        {
            Bitmap srce = new Bitmap(BackGround);
            Bitmap dest = new Bitmap(pbMap.Width, pbMap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            Graphics gr = Graphics.FromImage(dest);
            gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size));
            graphicPBMap.DrawImage(dest, 0, 0);
        }
        backGroundNeedUpdate = false;

        if (isGridShow && gridNeedUpdate)
        {
            for (int i = 0; i < pbMap.Width / 60 + 1; i++)
            {
                graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, 0, pbMap.Width - i * 60, pbMap.Height);
            }
            for (int i = 0; i < pbMap.Height / 60 + 1; i++)
            {
                graphicPBMap.DrawLine(gridPen, pbMap.Width, i * 60, 0, i * 60);
            }
        }
        gridNeedUpdate = false;

        if (isShowRulers && rulersNeedUpdate )
        {
            graphicPBMap.DrawLine(new Pen(Brushes.Black, 46), pbMap.Width - 1, pbMap.Height - 1, 0 - 1, pbMap.Height - 1);
            graphicPBMap.DrawLine(new Pen(Brushes.Black, 49), 0, 0, 0, pbMap.Height);

            for (int i = 0; i < pbMap.Width / 60 + 1; i++)
            {
                graphicPBMap.DrawString(XPixelToLong((double)pbMap.Width - i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, pbMap.Width - i * 60, pbMap.Height - 15);
                graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, pbMap.Height - 24, pbMap.Width - i * 60, pbMap.Height);
            }
            for (int i = 0; i < pbMap.Height / 60 + 1; i++)
            {
                graphicPBMap.DrawLine(gridPen, 0, i * 60, 25, i * 60);
                graphicPBMap.DrawString(YPixelToLat(i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, 0, i * 60);
            }
        }
        rulersNeedUpdate = false;

        if (rulerNeedUpdate)
        {
            if (x0ruler != -1 && y0ruler != -1)
            {
                if (x1ruler != -1 && y1ruler != -1)
                {
                    Rectangle rectAngle = new Rectangle((int)(x1ruler - 1), (int)(y1ruler - 1), 2, 2);
                    graphicPBMap.DrawEllipse(rulerPen, rectAngle);
                    graphicPBMap.DrawLine(rulerPen, x0ruler, y0ruler, x1ruler, y1ruler);
                    rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
                    graphicPBMap.DrawEllipse(rulerPen, rectAngle);
                }
                else
                {
                    Rectangle rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
                    graphicPBMap.DrawEllipse(rulerPen, rectAngle);
                }
            }

        }
        rulerNeedUpdate = false;

        if (nodesNeedUpdate)
        {
            nodesNeedUpdate = false;
            Node node;
            for (int i = 0; i < nodes.Count; i++)
            {
                node = (Node)(nodes[i]);
                if (node.IsOn)
                {
                    drawOnCircle(graphicPBMap,node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
                }
                else
                {
                    drawOffCircle(graphicPBMap, node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
                }
            }
        }
    }

编辑 1:

我改变了一次代码。

4

3 回答 3

0

您可以使用 invalidate()pictureBox.image = null;在需要更改图片框的图像的地方。无论如何,不​​需要总是改变背景。相反,您可以制作位图并将其放置为pictureBox.Image = bitmap. 如果您需要确切的示例源代码,请告诉我。

于 2011-09-12T04:23:50.443 回答
0

对不起,我来晚了。试试这个

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Bitmap bckMap = null;

    private void Form1_Load(object sender, EventArgs e)
    {
        bckMap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
        using (Graphics g = Graphics.FromImage(bckMap))
        {
            g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bckMap.Width, bckMap.Height));
            g.Dispose();
        }

        pictureBox1.BackgroundImage = bckMap;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = null;
        Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
        using (Graphics g = Graphics.FromImage(ellips))
        {
            g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(0, 0, ellips.Width, ellips.Height));
            g.Dispose();
        }
        this.pictureBox1.Image = ellips;

    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = null;
        Bitmap ellips = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
        using (Graphics g = Graphics.FromImage(ellips))
        {
            g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(5, 5, ellips.Width-10, ellips.Height-10));
            g.Dispose();
        }
        this.pictureBox1.Image = ellips;
    }
于 2011-09-20T08:06:56.407 回答
0

我终于找到了解决办法。

我需要使用图片框的绘制事件。

private void pbMap_Paint(object sender, PaintEventArgs e)
    {
       //do every think you want with picture box like draw circle or change background
    }

在这次活动中,我把我想做的每一个想法都放在了pbMap图片框上。

当我改变一些我调用下面的函数来应用我的改变时,

    public void updateMap()
    {
        if (pbMap.InvokeRequired) // just if you call in thread
        {
            pbMap.Invoke(new Action(() => pbMap.Invalidate()));
        }
        else
        {
            pbMap.Invalidate();
        }
    }
于 2011-09-26T05:31:59.027 回答