0

My problem is: When I try to save the graphics object to a bitmap image it does not save correctly, instead the image is black in color and there is nothing else in the file.

I've seen other answers, but I think it's different when you draw multiple times in the graphics object.

So, here's my attempt, please let me know where my issue is.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace PenFlip
{
    public partial class Match : Form
    {
        Graphics g;
        private int x = -1;
        private int y = -1;
        private bool moving;
        private Pen pen;
        private Bitmap testBmp;
        public Match()
        {
            InitializeComponent();
            g = panel1.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            pen = new Pen(Color.Black, 5);
            pen.StartCap = pen.EndCap = LineCap.Round;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox) sender;
            pen.Color = pictureBox.BackColor;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            moving = true;
            x = e.X;
            y = e.Y;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (moving && x != -1 && y != -1)
            {
                g.DrawLine(pen, new Point(x, y), e.Location);
                x = e.X;
                y = e.Y;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            moving = false;
            x = -1;
            y = -1;
            g.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // prints out the black image
            testBmp = new Bitmap(400, 200, g);
            testBmp.Save(@"test.bmp", ImageFormat.Bmp);
        }
    }
}
Jack
  • 31
  • 1
  • 3
  • 3
    [How to use the Paint event to draw shapes at mouse coordinates](https://stackoverflow.com/a/53708936/7444103) -- Also includes saving the drawings to a Bitmap. -- Don't use `Control.CreateGraphics()` to draw stuff. This method has very specific uses, you have to know when it's actually needed. Not for drawing. – Jimi Feb 08 '22 at 19:13
  • _g = panel1.CreateGraphics();_ Winforms graphics basic rule #1 : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object that is bound to a control! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. - Please note: The `Graphics` object does not __contain__ any graphics; it is a **tool** that lets you draw onto a related bitmap, including a control's surface. – TaW Feb 08 '22 at 21:12

1 Answers1

0

Uh, usage of Bitmap is tricky and has a lot of pitfalls (that's why it's use is deprecated, btw). Try to create the Graphics object from an in-memory bitmap, instead of grabbing it from a control. The bitmap should be the primary object, not the Graphics instance. You don't know what the control does to your bitmap.

So in the constructor, do something like:

        public Match()
        {
            InitializeComponent();
            bitmap = new Bitmap(panel1.Width, panel1.Height, PixelFormat.Format32bppArgb);
            pen = new Pen(Color.Black, 5);
            pen.StartCap = pen.EndCap = LineCap.Round;
        }
        

and in each function, create a graphics object from the bitmap:

using (var graphics = Graphics.FromImage(image))
                    {
                        graphics.Draw(...);
                    }

You need to update the panel manually now, e.g. by attaching to it's OnPaint event.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • 1
    No, that's not why it's deprecated. It's deprecated [because it uses GDI which only works on Windows](https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#reason-for-change), so can't be ported over to Linux etc. – Charlieface Feb 08 '22 at 19:59
  • @Charlieface: Well, that too. But a rewrite (or replacement) is in order anyway, as also GDI is no longer state of the art, really. Winforms also won't be developed much further. Of course, support for Windows won't go away any time soon, but I wouldn't start on System.Drawing.Bitmap in a new project any more. – PMF Feb 08 '22 at 20:19