简体   繁体   中英

How can I save a graphics object to a bitmap in a "paint" winforms application?

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

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, eg by attaching to it's OnPaint event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM