C# 位图绘图不呈现到屏幕

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

最佳答案

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

myPictureBox.Image = m_bitmap;

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

myPictureBox.Invalidate();

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

关于C# 位图绘图不呈现到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7284903/

相关文章:

c# - linq to sql使用lambda连接多列

C# 搜索具有相似性/相似性

c# - ASP/VB/C# .NET 动态计算和引用

c# - Image.GetThumbnailImage 方法和质量

c# - GDI+:将所有像素设置为给定颜色,同时保留现有的 alpha 值

c# - 连接到远程共享文件夹导致 "multiple connections not allowed"错误,但尝试断开连接导致 "connection does not exist"

svg - 如何分割SVG中的不规则多边形

c# - 两个圆的切线

android - 如何在两个 Activity 之间传递位图数组

c# - 在 C# 中读取 ROS CompressedImage 数据字节数组