2

I'm trying to capture the screen and then output the screenshot as a base64 image but cannot seem to get a usable base64 image out my code.

public static Bitmap bitmap;
    public static string base64;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        Graphics graphics = Graphics.FromImage(bitmap as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Image = bitmap;
        richTextBox1.Text = base64;
    }
    public static string CaptureScreen()
    {
        bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Bitmap bImage = bitmap;
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] byteImage = ms.ToArray();
        base64 = Convert.ToBase64String(byteImage);
        return base64;
    }

I get this output when testing and it should display this or close too this image.

7
  • You're copying 0 width and 0 height...? Commented May 30, 2017 at 15:56
  • do u mean on graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); this is just for displaying what the result should look like. just for development Commented May 30, 2017 at 15:58
  • but: you're allocating an empty bitmap, writing nothing to it, then saving it; you should expect to see a black (or white) rectangle. What do you see? it looks like a black rectangle to me... Commented May 30, 2017 at 16:00
  • when i convert the base64 string back into an image i get a black square. i get a full working screenshot displayed in the picturebox1 Commented May 30, 2017 at 16:02
  • what did you expect, and why? why would you expect to see anything else? You allocated an empty bitmap, and wrote nothing to it... Commented May 30, 2017 at 16:03

1 Answer 1

2

The problem here is timing.

You are creating the base-64 before you copy the screen into the image; you need to move the line:

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

to happen before the line:

bImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

Try literally just changing it to:

graphics.CopyFromScreen(0, 0, 0, 0, bImage.Size);
bImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You very much Marc! :D
I have searched this question, and in my case I have updated with the line bImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); the format, I have used .Bmp, changed to .Jpeg and reached the desired output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.