0

I want to show an image from a console application and I have written code to do so. But when it shows the image, it shows a weird Form box. And if I save the image and open it normally, it shows the image.

Code:

Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics g = Graphics.FromImage(bmpScreenCapture);

g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
    Screen.PrimaryScreen.Bounds.Y,
    0, 0,
    bmpScreenCapture.Size,
    CopyPixelOperation.SourceCopy);

bmpScreenCapture.Save("potato.png", ImageFormat.Png);

Form imageForm = new Form();
//imageForm.FormBorderStyle = FormBorderStyle.None;
imageForm.Controls.Add(new PictureBox() { Image = bmpScreenCapture, Visible = true });
imageForm.Show();
Ian
  • 30,182
  • 19
  • 69
  • 107
  • 2
    A console mode app is a pretty hostile place for a GUI. You'll at a minimum have to use ShowDialog(). Trying to get a little pregnant is a bad idea. – Hans Passant Dec 16 '15 at 23:09
  • Just don't use a form. Instead look to actually open the image. I think all you need is `Process.Start(filename)` to open it in the default application. (If that's what you wanted to do) – TyCobb Dec 16 '15 at 23:11
  • It's isn't a duplicate because they don't want it in a form they want it literally outputted to the console. – Avid Programmer Dec 16 '15 at 23:33
  • 1
    Yes it is a duplicate! - If you read __all__ answers you will see that this request is covered there as well! – TaW Dec 17 '15 at 08:35
  • @AvidProgrammer specifically, [this answer](http://stackoverflow.com/a/33652557/80274) is the duplicate. – Scott Chamberlain Dec 17 '15 at 16:40

2 Answers2

0

This is because you are seeing the form border. You were on the right track with:

imageForm.FormBorderStyle = FormBorderStyle.None;

However you will need to size the window appropriately. But you don't really even need a window. After calling

bmpScreenCapture.Save("potato.png", ImageFormat.Png);

simply do System.Diagnostics.Process.Start("Potato.png"); and it will open the image in default program.

schmoopy
  • 6,419
  • 11
  • 54
  • 89
  • I was testing it but the images are only needed for a short while. I also adjusted the size but it still showed a weird message box. Code used was imageForm.Size = bmpScreenCapture.Size; I also plan to have the image constantly change to make it look as if it's moving hopefully :) – Avid Programmer Dec 16 '15 at 23:24
0
        Form imageForm = new Form();
        imageForm.Text = "Screenshot";
        //imageForm.FormBorderStyle = FormBorderStyle.None;
        imageForm.Size = bmpScreenCapture.Size;
        imageForm.BackgroundImageLayout = ImageLayout.Zoom;
        imageForm.BackgroundImage = bmpScreenCapture;
        imageForm.ShowDialog();