c#位图绘制不会渲染到屏幕上
本文关键字:屏幕 位图 绘制 | 更新日期: 2023-09-27 18:07:16
我正在尝试写一个绘图程序与平板电脑使用。为此,我需要下降和透明度与压力的使用。所以我使用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完成。
看起来至少有两种方法可以让图像显示在屏幕上;我不能马上说出什么问题,但我肯定会说,去掉objGraphics.Dispose();
行——你没有创建Graphics
(你传递了它),所以你不应该Dispose
它。
我稍微清理了一下你的代码。你可能不应该使用图片框。
这是一个只有面板的表单:
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();
}
}
}
其他海报已经在很大程度上回答了这个问题,但根据我的经验,我想补充一点,你可能会得到一些闪烁的方法。如果您这样做,您可以做的一件事来帮助实现这一点是子类化您的呈现目标并启用双缓冲。对于图片框,它看起来像这样:
public class DoubleBufferedPictureBox : PictureBox
{
/// <summary>
/// Creates an instance of the DoubleBufferedPictureBox.
/// </summary>
public DoubleBufferedPictureBox() : base()
{
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
}
}