0

im trying to learn everyday something new about c#. Now im trying to get Image from Desktop and I did it in another class. Now i dont know how this is gonna work. How can i get the image which was created in my screenshot.cs so that i can set it to picturebox which is in my Form1.cs

My Code is this:

namespace CatchAreaToImage
{

    public partial class Form1 : Form
    {

        Bitmap screen;

          
        public Form1()
        {

            InitializeComponent();
        }
        Bitmap screen2;
        private void button1_Click(object sender, EventArgs e)
        {
            Screenshot.CaptureScreen(screen2);

            pBArea.Image = screen2;


        }

My Screenshot.cs ist this:

    class Screenshot
    {

        public static void CaptureScreen() //do screenshot of desktop
        {
            // Take an image from the screen
            Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Create an empty bitmap with the size of the current screen 

            Graphics graphics = Graphics.FromImage(screen as Image); // Create a new graphics objects that can capture the screen

            graphics.CopyFromScreen(0, 0, 0, 0, screen.Size); // Screenshot moment → screen content to graphics object

        }

    }
        public void Image(Bitmap screen)
        {
            screen2 = screen;
        }

I hope i could describe my problem correctly.

  • have you considered returning the bitmap to the caller? – JonasH Nov 23 '21 at 19:21
  • Tried that... but i get always null for my variable. like that: in form1.cs ( public void Image(Bitmap screen) { screen2 = screen; } and i change my class method to CaptureScreen(Bitmap bitmap) and return bitmap with Form1 f = new Form1(); f.Image(bitmap); -> but this isnt working because i get null for my variable //edit i edited my code in first post – Winchester Nov 23 '21 at 19:43

1 Answers1

3

You can modify your CaptureScreen method to return the Bitmap variable screen and assign it to pictureBox.Image property.

class Screenshot
{
    public static Image CaptureScreen()
    {
        
        Bitmap screen = new Bitmap(
            Screen.PrimaryScreen.Bounds.Width, 
            Screen.PrimaryScreen.Bounds.Height);

        Graphics graphics = Graphics.FromImage(screen as Image);

        graphics.CopyFromScreen(0, 0, 0, 0, screen.Size); 

        return screen;
    }
}

And then in form class you can do assignment as the Bitmap inherits from Image

namespace CatchAreaToImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Image screen = Screenshot.CaptureScreen(); 
            this.pictureBox1.Image = screen;
            // other uses of screen possible
        }
    }
}
Maciej Hoga
  • 56
  • 1
  • 4
  • yes thanks, that worked :) but is it possible to set the value of screen another variable and then put it after picturebox1.image? like picturebox1.image = screen2? – Winchester Nov 23 '21 at 19:49
  • Yes you can assign the return value from function to a new variable in scope of button1_Click and assign it multiple times like this Image screen = Screenshot.CaptureScreen(); this.pictureBox1.Image = screen; this.pictureBox2.Image = screen; Is this is what you want? – Maciej Hoga Nov 23 '21 at 20:10
  • Yes this is what i wanted. Thanks – Winchester Nov 23 '21 at 20:24