畫畫上使用C#如何繪製圖紙圖片框圖片框
回答
鼠標拖動您可以通過捕獲圖片框的鼠標移動事件,然後得到像圖片框圖形做到這一點。
Graphics g = pictureBox.CreateGraphics(); 然後你可以繪製任何你想要使用這個圖形對象繪製
把一個PictureBox放在你的窗體上,並將其BackColor設置爲白色。然後將此代碼添加到您的窗體(你必須低於實際掛鉤的鼠標事件,即你不能只是複製該代碼粘貼到您的形式):
private Point? _Previous = null;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_Previous = e.Location;
pictureBox1_MouseMove(sender, e);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_Previous != null)
{
if (pictureBox1.Image == null)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
}
pictureBox1.Image = bmp;
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(Pens.Black, _Previous.Value, e.Location);
}
pictureBox1.Invalidate();
_Previous = e.Location;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_Previous = null;
}
再畫了!
如果您願意,可以通過設置Graphics
對象的SmoothingMode
屬性來改善繪製圖像的質量。
更新:的.Net CF簡化版,有Pens
集合,MoustEventArgs
沒有Location
,所以這裏是一個CF的版本:
private Point? _Previous = null;
private Pen _Pen = new Pen(Color.Black);
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_Previous = new Point(e.X, e.Y);
pictureBox1_MouseMove(sender, e);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_Previous != null)
{
if (pictureBox1.Image == null)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
}
pictureBox1.Image = bmp;
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(_Pen, _Previous.Value.X, _Previous.Value.Y, e.X, e.Y);
}
pictureBox1.Invalidate();
_Previous = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_Previous = null;
}
謝謝你的幫助,但我得到的錯誤不知道鋼筆和電子地址 – Gold 2010-04-26 13:32:39
@金:剛剛添加了這種方法的CF友好版本,因爲CF沒有Pens集合,MouseEventArgs沒有一個位置(只是X和Y)。 – MusiGenesis 2010-04-26 14:16:26
+1對一個簡短的,沒有措辭很好的問題做了詳細的回答。 :) – JYelton 2010-04-26 14:17:49
這裏,pictureBox1 ==簽名。我翻譯了以這種方式VB:
全球:
Dim _previous As Point = Nothing
Dim _pen As Pen = New Pen(Color.Black)
Dim drawing As Boolean = False
''' <summary>
''' This handles the signature drawing events (drawing)
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub signature_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseMove
If drawing = True Then
If signature.Image Is Nothing Then
Dim bmp As Bitmap = New Bitmap(signature.Width, signature.Height)
Using g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.White)
End Using
signature.Image = bmp
End If
Using g As Graphics = Graphics.FromImage(signature.Image)
g.DrawLine(_pen, _previous.X, _previous.Y, e.X, e.Y)
End Using
signature.Invalidate()
_previous = New Point(e.X, e.Y)
End If
End Sub
''' <summary>
''' this indicates somebody is going to write a signature
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub signature_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseDown
_previous = New Point(e.X, e.Y)
drawing = True
signature_MouseMove(sender, e)
End Sub
''' <summary>
''' the signature is done.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub signature_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseUp
_previous = Nothing
drawing = False
End Sub
- 1. 繪製圖片框C#
- 2. 如何正確繪製圖片框?
- 3. 圖片框在圖片框
- 4. 圖片圖片框
- 5. 如果圖片框小於圖片,圖像適合圖片框
- 6. 在圖片框中緩慢繪製圖片
- 7. 圖片縮放圖片框
- 8. 導入圖片圖片框
- 9. 分配繪製到圖片框
- 10. 在Panel-Panel裏面繪製圖片框#
- 11. 如何在c中的圖片框上繪製圖形#
- 12. 如何避免位圖,而在圖片框繪製
- 13. 在C#中的圖片框內繪圖
- 14. 如何繪製縮放圖片框沒有重繪新的點
- 15. 如何將圖片框動態添加到圖片框陣列
- 16. 如何繪製黑白圖片mandelbrot圖
- 17. 乘圖片框
- 18. 在圖片框
- 19. 圖片/相框
- 20. 慢圖片框
- 21. 在圖片框
- 22. 控制移動圖片框
- 23. 如何刷新圖片框
- 24. 如何居中圖片框
- 25. 如何在圖片框
- 26. 如何旋轉圖片框
- 27. 如何製作帶框的圖片?
- 28. 如何製作滑動圖片/框架?
- 29. 繪製圖紙
- 30. 更改圖片框中的圖片C#
Eeeeep,上帝沒有!你會得到這樣的圖形圖形,我知道的唯一一個用於.CreateGraphics()的用例是測量字符串。你想重寫OnPaint。 – Quibblesome 2009-10-08 11:27:26
或使用.Image屬性。如果我們要做自定義繪圖,儘管我會完全停止使用picturebox,並從Control繼承。 – Quibblesome 2009-10-08 11:28:18