为什么图形不显示?

时间:2019-01-22 15:31:05

标签: c# winforms

我想保存所有抽绳,这是用下半部分的代码完成的。我通过将“点”保存为列表类型来保存它。保存的目的是因为我希望能够删除特定的图形。所有其他工程图将保留,仅要删除的工程图将被删除。我的主要查询是为什么在删除特定图形时我不能使用相同的代码进行一些较小的编辑(代码的上半部分是我用来添加新绘制字符串的代码),而我在绘制该图形时要重绘。

Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\Bus_Nearside.png";
Side_pictureBox.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\pictures for app\Bus_Nearside.png");
Bitmap bm = new Bitmap(Side_pictureBox.Image);
if (Tagged_Remarks_listBox.SelectedIndex == 0)
{
  for (int x = 0; x <= NumberingPosition.Count - 1; x++)
  {
    if (x != 0)
    {
      using (Graphics gr = Graphics.FromImage(bm))
      {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        Font drawFont = new Font("Calibri (Body)", 15);
        SolidBrush drawBrush = new SolidBrush(Color.Blue);
        //MessageBox.Show(Numbering[u] + NumberingPosition[u]);
        gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);
      }
    }
    Side_pictureBox.Image = bm;
    Side_pictureBox.Invalidate();
  }

///以上代码是我第一次绘制字符串时,下面的代码是在删除特定图形时重新绘制///

Bitmap bm = new Bitmap(Side_pictureBox.Image);


                    using (Graphics gr = Graphics.FromImage(bm))
                    {
                        gr.SmoothingMode = SmoothingMode.AntiAlias;

                        String drawString = numbering_for_digram.ToString();
                        Font drawFont = new Font("Calibri (Body)", 15);
                        SolidBrush drawBrush = new SolidBrush(Color.Blue);

                        gr.DrawString(drawString, drawFont, drawBrush, lastPoint);
                        Numbering.Add(drawString);


                        drawFont.Dispose();
                        drawBrush.Dispose();
                    }

                    Side_pictureBox.Image = bm;

2 个答案:

答案 0 :(得分:1)

如果我们处于Paint()事件中,那么我希望看到更多类似的东西:

if (Tagged_Remarks_listBox.SelectedIndex == 0)
{
    Graphics gr = e.Graphics;
    gr.SmoothingMode = SmoothingMode.AntiAlias;
    Font drawFont = new Font("Calibri (Body)", 15);
    SolidBrush drawBrush = new SolidBrush(Color.Blue);
    for (int x = 1; x <= NumberingPosition.Count - 1; x++)
    {       
        //MessageBox.Show(Numbering[u] + NumberingPosition[u]);
        gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);
    }
    drawFont.Dispose();
    drawBrush.Dispose();
}

请注意,在Paint()中调用Invalidate()会导致它重新绘制自身 REPEATEDLY FOREVER ...这可能是问题的一部分。

答案 1 :(得分:0)

我找到了一种使用Bitmap来绘制所有图纸的方法(也许不是最好,最有效,最有效的方法)。然后,该位图将另存为Png文件。完成所有绘图后,我将picturebox.image声明为位图的Png文件。

private void Side_pictureBox_Paint(object sender, PaintEventArgs e)
{
    if (Tagged_Remarks_listBox.SelectedIndex == 0 && selectedindexreset == true)
    {
        //Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\Bus_Nearside.png";
        Side_pictureBox.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\pictures for app\Bus_Nearside.png");
        Bitmap bmforedit = new Bitmap(Side_pictureBox.Image);

        //MessageBox.Show("inside");

        using (Graphics gr = Graphics.FromImage(bmforedit))
        {
            for (int x = 0; x <= NumberingPosition.Count - 1; x++)
            {
                //MessageBox.Show(x.ToString());
                if (Numbering[x] != "1") // change accordingly
                {
                    //MessageBox.Show(Numbering[x]);
                    gr.SmoothingMode = SmoothingMode.AntiAlias;
                    Font drawFont = new Font("Calibri (Body)", 15);
                    SolidBrush drawBrush = new SolidBrush(Color.Blue);
                    //MessageBox.Show(Numbering[x] + NumberingPosition[x]);

                    gr.DrawString(Numbering[x], drawFont, drawBrush, NumberingPosition[x]);

                    drawFont.Dispose();
                    drawBrush.Dispose();
                }
            }
            // bmforedit.Save(@"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage.png");
            Side_pictureBox.Image.Dispose();
            //bmforedit.Save(@"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage1.png");
            Side_pictureBox.Image = bmforedit;
        }

        for (int u = 0; u <= PrevStore.Count - 1; u++)
        {
            using (Graphics g = Graphics.FromImage(bmforedit))
            {
                if (u < StartDrawCount[0] || u > StopDrawCount[0])
                {
                    g.DrawLine(new Pen(Color.DarkRed, 2), PrevStore[u], NowStore[u]);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                }
            }
        }

        bmforedit.Save(@"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\PDIPROTOTYPE2\bin\Debug\pictures for app\TestImage2.png");
        selectedindexreset = false;
        Side_pictureBox.ImageLocation = AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\TestImage2.png";
        Side_pictureBox.Refresh();
    }
}