0
首先,我很抱歉我的英語不好。現在我遇到了C#項目(ms paint)的問題。當我在圖片框中打開新圖片時,我繪製的最後一個圖形仍然保留,直到我在該圖像上繪製另一條線。這裏是我的代碼:
-draw行:創建新圖片框時出現問題!
public Form1()
{
InitializeComponent();
snapshot = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
if (tempDraw != null)
{
tempDraw = (Bitmap)snapshot.Clone();
Graphics g = Graphics.FromImage(tempDraw);
Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g.DrawLine(myPen, pDau, pHientai);
myPen.Dispose();
e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
g.Dispose();
}
- 鼠標事件:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
snapshot = (Bitmap)tempDraw.Clone();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
saved = false;
pDau = e.Location;
tempDraw = (Bitmap)snapshot.Clone();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
pHientai = e.Location;
pictureBox1.Invalidate();
saved = false;
}
}
- 創造新的圖片框:
public void New()
{
pictureBox1.Image =null;
snapshot = null;
tempDraw = null;
snapshot= new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
- 開放的形象:
New();
snapshot = new Bitmap(openFileDialog1.FileName);
tempDraw = (Bitmap)snapshot.Clone();
pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
strPath = openFileDialog1.FileName;
this.Text = strPath + " - Paint";
你能告訴我什麼問題嗎?非常感謝你!
首先,感謝您的回覆。但是這個'if'語句實際上在picturebox_paint事件中。我在Form1上添加了一個圖片框。然後你的代碼只是改變我的圖片框顏色相同的形式的背景。我的問題是當我在picturebox上繪製了一些東西,然後從計算機上打開了新的圖像,我繪製的最後一個圖形沒有消失,直到我在picturebox上繪製了其他圖形,它消失了。 – Superquay 2011-04-16 17:58:18
'PictureBox'不適用於繪圖。它是爲了顯示圖像,通常來自文件。直接在你的「表格」或「面板」上繪製。 – 2011-04-16 18:08:02