Hi all,

I am just wondering if someone can help me.

Basically, I have the following code that I am using to allow me to draw on a picturebox.
There are a few things that I need to be able to do and I can't work out exactly how to get them done.

1: I need the drawing to stay in the picturebox once the form is minimized and then restored.
2: I need to be able to save the drawing to an image file.

The DrawToBitmap parts of the code are just left in from when I tried to load the bitmap again with the Pain event for the picturebox.

Any help with this would be appreciated.


Thanks

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;
using System.Drawing.Drawing2D;
using System.IO;

namespace Painter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool Drag = false;
        int NewX;
        int NewY;
        int oldX;
        int oldY;
        Bitmap bmp;

        private void Form1_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width,pictureBox1.Height));
            bmp.Save("c:\\out.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Drag = true;
            NewX = e.X;
            NewY = e.Y;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();

            Pen myPen = new Pen(Color.Black, 2);


            if (Drag == true)
            {
                g.DrawLine(myPen, oldX, oldY, e.X, e.Y);
                oldX = e.X;
                oldY = e.Y;
            }
            oldX = e.X;
            oldY = e.Y;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Drag = false;
            pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
        }

      

       
    }
}

Then you need to not draw your picture on the pressing of a button but on the painting of the box, then, you toggle within that method wether you do nothing much, or paint the picture in memory.

This will solve all you problems in one hit

Then you need to not draw your picture on the pressing of a button but on the painting of the box, then, you toggle within that method wether you do nothing much, or paint the picture in memory.

This will solve all you problems in one hit

Hi,
Thanks for the quick repsonse.

I apologise for being thick here, but I don't follow you.

I was just trying to save the image to file when I hit the button (which does save the picturebox to file, but all it shows is grey).

How would you go about putting the drawing that I did on the picturebox into the bitmap I declared and then throwing that back onto the picturebox everytime the paint method is called?

Thanks again.

the problem is you arent drawing to your bitmap you made, you're just drawing on the screen which isnt remembered, a little like the whole if you wave a sparkler in the air you see the picture linger for a little but then its gone.

As I said its the same reason when you minimize and return that the image is gone, because it never really was there..

So, with the way you've done it, you cant. its not real.

You have to change a little about what you did.

Firstly, you need to ensure you have made an image in the first place such as:

Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = b;

Now your picturebox has an image for you to work with.

Then for example, you can draw on it with such as :

Brush bb = Brushes.White;
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            g.FillRectangle(bb, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            Pen p = new Pen(Color.Black, 2);
            g.DrawLine(p, 0, 0, pictureBox1.Width, pictureBox1.Height);
            g.Dispose();
            pictureBox1.Invalidate();
            pictureBox1.Image.Save("C:\\outpt.bmp");

now open output.bmp and you'll find a white box with a diagonal black line.

This article has been dead for over six months. Start a new discussion instead.