C # bitmap image not showing on screen

I am trying to write a drawing program for use with a tablet. To do this, I need recession and transparency to use with pressure. Therefore, I use a raster system in C # to build an image.

It seems I cannot get the drawing code at the moment to display anything. It is displayed in a frame with an image. I know that there are some things that are introduced into a bitmap as it appears when I do a bitmap.

I looked at almost all of the drawing questions in C #, citing the use of a line drawing or an ellipse drawing element, rather than bitmap images

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

I'm a little new to C #, so I'm very open to any other questions or things that might come in handy.

+4
source share
4 answers

You need to assign a bitmap to the image field.

 myPictureBox.Image = m_bitmap; 

You can do this after you modify the bitmap or assign it once and then invalidate the PictureBox.

 myPictureBox.Invalidate(); 

It tells your form to refresh the image on the screen. There is no need to override OnPaint. Draw a bitmap using the Graphics object that you created in the form constructor (if you want to do more complex things than just drawing single pixels). PictureBox does the rest.

+2
source

It seems that there are two ways that you are trying to get an image on the screen; I can’t immediately say what is wrong, but I would say that I definitely get rid of this line objGraphics.Dispose(); - you did not create Graphics (you were given it), so you should not Dispose it.

+2
source

I cleaned up your code a bit. You should probably not use a picture file for this.

Here is a form with only a panel:

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

Other posters pretty much answered the question, but in my experience, I would add that you are likely to get some flicker using this method. If you do, one thing you can do to help with this is to subclass your render target and activate double buffering. For a window with pictures, it looks something like this:

 public class DoubleBufferedPictureBox : PictureBox { /// <summary> /// Creates an instance of the DoubleBufferedPictureBox. /// </summary> public DoubleBufferedPictureBox() : base() { this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); } } 
+1
source

All Articles