How to copy from screen 9 different Images using C#?

69 Views Asked by At

I'm doing a school project using C# in which I designed a Form in C# that I can draw on and now I need to copy what I draw into a 9 different Images and save them. So far, I tried using the copy from screen function like that:

Size s = this.Size;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, this.CreateGraphics());
Graphics.FromImage(memoryImage).CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
memoryImage.Save(@"C:\Users\omerm\Desktop\projectFinals\ProjectFiles\C-Sharp\Drawing\WinFormsApp1\Digits\1.jpg");

My problem is that the image I get from this code includes parts of my screen that aren't part of my Form. And I also need to copy 9 different images who are fractions of this one instead of just the one and make sure those 9 are around the same shape(squares). does anyone know how to do that?

I'm terrible at explaining this so think that I need to take the original image and cut into 9 like in a tic tac toe board and save each one of them as a different image in the shape of squares instead of the original.

full code:

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        Pen Pen = new Pen(Color.Black, 2);
        int pX, pY;
        Graphics g;

        public object MemoryImage { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pX = e.X;
            pY = e.Y;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                Graphics g = pictureBox1.CreateGraphics();
                g.DrawLine(Pen, pX, pY, e.X, e.Y);

                pX = e.X;
                pY = e.Y;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Size s = this.Size;
            Bitmap memoryImage = new Bitmap(s.Width, s.Height, this.CreateGraphics());
            Graphics.FromImage(memoryImage).CopyFromScreen(this.Location.X, this.Location.Y, 0,         0, s);
            memoryImage.Save(@"C:\Users\omerm\Desktop\projectFinals\ProjectFiles\C-Sharp\Drawing\WinFormsApp1\Digits\1.jpg");

        }

        private void ChooseColor(object sender, EventArgs e)
        {
            Pen.Color = ((PictureBox)sender).BackColor;
        }
        
    }
}

Tried looking online but what I look for is really specific.

1

There are 1 best solutions below

0
Idle_Mind On

Here's an example of persisting the data in a structure and drawing it in the Paint() event. Additionally, I've demonstrated how to tell the PictureBox to draw itself instead of copying the screen:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }        

    private List<Point> curSquiggle;
    private List<List<Point>> Squiggles = new List<List<Point>>();
    
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            curSquiggle = new List<Point>();
            curSquiggle.Add(e.Location);
            Squiggles.Add(curSquiggle);
        }            
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            curSquiggle.Add(e.Location);
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        foreach(List<Point> squiggle in Squiggles)
        {
            e.Graphics.DrawLines(Pens.Black, squiggle.ToArray());
        }
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);

        // Do something with the bmp, like display it in another form:
        Form frm = new Form();
        frm.AutoSize = true;
        PictureBox pb = new PictureBox();
        pb.SizeMode = PictureBoxSizeMode.AutoSize;
        pb.Image = bmp;
        pb.Location = new Point(0, 0);
        frm.Controls.Add(pb);
        frm.Show();
    }

}

Example run:

enter image description here

Here's a different version of the save code which splits the main picture into 9 pieces:

    private void button1_Click_1(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);

        int cols = 3;
        int rows = 3;
        int cellWidth = bmp.Width / cols;
        int cellHeight = bmp.Height / rows;

        Rectangle destRect = new Rectangle(0, 0, cellWidth, cellHeight);

        List<Bitmap> cells = new List<Bitmap>();
        for(int r=0; r<rows; r++)
        {
            for(int c=0; c<cols; c++)
            {
                Bitmap cell = new Bitmap(cellWidth, cellHeight);
                Rectangle srcRect = new Rectangle(c * cellWidth, r * cellHeight, cellWidth, cellHeight);
                using (Graphics g = Graphics.FromImage(cell))
                {
                    g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
                }
                cells.Add(cell);
            }
        }

        foreach(Bitmap cell in cells)
        {
            Form frm = new Form();
            frm.AutoSize = true;
            frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            PictureBox pb = new PictureBox();
            pb.SizeMode = PictureBoxSizeMode.AutoSize;
            pb.Image = cell;
            pb.Location = new Point(0, 0);
            frm.Controls.Add(pb);
            frm.Show();
        }            
    }

Splitting into nine pieces:

enter image description here