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#有点新意,所以我对任何其他可能引起你注意的问题或事情都持开放态度.

解决方法:

您必须将位图分配给图片框.

myPictureBox.Image = m_bitmap;

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

myPictureBox.Invalidate();

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

上一篇:具有抗锯齿功能的GUI可嵌入Python绘图窗口小部件


下一篇:javascript – SVG – 如何切成两半?