C#位图绘制不会呈现到屏幕上

时间:2011-09-02 14:26:04

标签: c# graphics bitmap drawing paint

我正在尝试编写一个用于平板电脑的绘图程序。为此,我需要在压力下使用下降和透明度。所以我在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#有点新意,所以我对任何其他可能引起你注意的问题或事情都持开放态度。

4 个答案:

答案 0 :(得分:2)

看起来至少有两种方法可以让你在屏幕上显示图像;不能马上说什么是错的,但我会说绝对摆脱objGraphics.Dispose();行 - 你没有创建Graphics(你被传递了),所以你不应该{{1它。

答案 1 :(得分:2)

您必须将位图分配到图片框。

myPictureBox.Image = m_bitmap;

您可以在更改位图或分配一次然后使PictureBox无效后执行此操作。

myPictureBox.Invalidate();

这会告诉您的表单刷新屏幕上的图片。无需重写OnPaint。使用您在窗体的构造函数中创建的Graphics对象绘制到位图(如果您想制作比仅绘制单个像素更复杂的东西)。 PictureBox将完成其余的工作。

答案 2 :(得分:1)

我清理了一下你的代码。您可能不应该使用图片框。

这是一个只有一个面板的表单:

  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();
      }
    }  
  }

答案 3 :(得分: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);
    }
}