2011-09-02 77 views
4

我想寫一個繪圖程序以用於平板電腦。爲此,我需要在壓力下使用降落和透明度。所以我使用C#中的位圖系統進行圖像構建。C#位圖繪圖不能渲染到屏幕

我似乎無法得到我的繪圖代碼在此刻顯示任何東西。它正在呈現給一個圖片框。我知道有一些東西被輸入到位圖中,就像我在做位圖保存時顯示的那樣。

我有圍繞一看幾乎所有的C#繪圖問題,請參閱使用畫線或橢圓形繪圖的東西,而不是位圖

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace paint1 
{ 
    public partial class Form2 : Form 
    { 
     public Bitmap m_bitmap; 
     public bool m_penDown; 
     public int m_lastX; 
     public int m_lastY; 
     public int m_currentX; 
     public int m_currentY; 

     public Form2() 
     { 
      InitializeComponent(); 

      // Create the bitmap area 
      m_bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
      m_penDown = false; 

      Graphics m_graphics = Graphics.FromImage(m_bitmap); 
      m_lastX = System.Windows.Forms.Cursor.Position.X; 
      m_lastY = System.Windows.Forms.Cursor.Position.Y; 
      m_currentX = System.Windows.Forms.Cursor.Position.X; 
      m_currentY = System.Windows.Forms.Cursor.Position.Y; 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 

     } 

     private void Form2_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics objGraphics; 
      //You can't modify e.Graphics directly. 
      objGraphics = e.Graphics; 
      // Draw the contents of the bitmap on the form. 
      objGraphics.DrawImage(m_bitmap, 0, 0, 
       m_bitmap.Width, 
       m_bitmap.Height); 
      objGraphics.Dispose(); 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      m_penDown = true; 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      m_penDown = false; 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      m_lastX = m_currentX; 
      m_lastY = m_currentY; 

      m_currentX = System.Windows.Forms.Cursor.Position.X; 
      m_currentY = System.Windows.Forms.Cursor.Position.Y; 

      if(m_penDown) 
       m_bitmap.SetPixel(m_currentX, m_currentY, Color.Gray); 
     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 

      Form2_Paint(sender, e); 
      this.pictureBox1.Image = m_bitmap; 
     } 

     private void Form2_KeyUp(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Space) 
      { 
       m_bitmap.Save(@"C:\Users\rpettefar\Documents\My Dropbox\Programming\paint1\preview.bmp", System.Drawing.Imaging.ImageFormat.Bmp); 
      } 
     } 
    } 
} 

我有點新的C#,所以我很開放也可能引起您的注意。

回答

2

您將不得不將您的位圖分配給圖片框。

myPictureBox.Image = m_bitmap; 

您可以在更改位圖或將其分配一次後使其無效,然後使PictureBox失效。

myPictureBox.Invalidate(); 

這會告訴您的表單刷新屏幕上的圖片。沒有必要重寫OnPaint。使用在窗體的構造函數中創建的Graphics對象繪製位圖(如果您想繪製比繪製單個像素更復雜的東西)。 PictureBox將完成剩下的工作。

+0

謝謝你。無效的圖片框似乎很好地畫出:D – Pyro

2

看起來你至少有兩種方法試圖在屏幕上顯示圖像;不能馬上說出了什麼問題,但我會說絕對不會出現objGraphics.Dispose();這一行 - 你沒有創建Graphics(你已經通過了它),所以你不應該Dispose吧。

+0

是的,那是我的一個挫折的結果。有一種方法不起作用,所以我用另一種方式來看看它是否會有所幫助。 另外我想我必須擺脫那裏創建的圖形對象。那麼讓它超出範圍也好嗎? – Pyro

+0

@Pyro但它不是在那裏創建的,它是在'PaintEventArgs'中給你的。對你創建的'IDisposable'對象的'Dispose'完全正確,但只有*這些。 – AakashM

1

我清理了一下你的代碼。你可能不應該爲此使用一個picturebox。

這裏是剛剛面板的形式:

public partial class Form1 : Form 
    { 
    public Bitmap m_bitmap; 
    public Point m_lastPoint = Point.Empty; 

    public Form1() 
    { 
     InitializeComponent(); 
     m_bitmap = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
     using (Graphics g = Graphics.FromImage(m_bitmap)) 
     g.Clear(SystemColors.Window); 
    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.DrawImage(m_bitmap, new Point(0, 0)); 
    } 

    private void panel1_MouseDown(object sender, MouseEventArgs e) 
    { 
     m_lastPoint = e.Location; 
    } 

    private void panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
     using (Graphics g = Graphics.FromImage(m_bitmap)) 
      g.DrawLine(Pens.Black, m_lastPoint, e.Location); 

     m_lastPoint = e.Location;  
     panel1.Invalidate(); 
     } 
    } 
    } 
+0

非常感謝。積分和失效的使用很好地工作。 雖然我有點迷惑 「使用(圖形g = Graphics.FromImage(m_bitmap))」 我想我應該閱讀更多關於c#,嘿嘿。 – Pyro

+2

需要處理實現IDisposable接口的對象,並且using(){}語句會爲您執行此操作。 – LarsTech

1

其他海報在很大程度上回答了這個問題,但我的經驗,我想補充一點,你可能會得到一些閃爍用這種方法。如果你這樣做,你可以做的一件事就是爲你的渲染目標子類化並啓用雙緩衝。對於圖片框,它看起來像這樣:

public class DoubleBufferedPictureBox : PictureBox 
{ 
    /// <summary> 
    /// Creates an instance of the DoubleBufferedPictureBox. 
    /// </summary> 
    public DoubleBufferedPictureBox() : base() 
    { 
     this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); 
    } 
} 
+0

哇,我不知道你可以雙緩衝一個C#組件。謝謝! – Pyro