2011-09-02 76 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