[英]Why doesn't Graphics show up?
我想保存所有用下半部分代碼完成的拉繩。 我通過在列表類型中保存“點”來保存它。 保存的目的是因為我想有刪除特定圖紙的能力。 所有其他圖紙將被保留,只有要刪除的圖紙將被刪除。 我的主要疑問是為什么我不能使用相同的代碼進行一些小的編輯(代碼的上半部分是我用來添加新拉繩的代碼),當我刪除特定繪圖時,我用它來重新繪制。
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;
如果我們在 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();
}
需要注意的是調用Invalidate()在paint()會導致它一再FOREVER重繪自己......這可能是問題的一部分。
我找到了一種使它工作的方法(也許不是最好、最有效、最高效的方法),通過使用 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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.