Capturing screenshot of Form and elements on form - c#

I'm trying to capture a screenshot of the MainWindow and content on the main window. The MainWindow will sometimes be behind other elements and need to get screen capture without those other overlaying elements. This code currently returns a bitmap of the blank MainWindow form without any content. The form has a bunch of different dynamic UI elements. How can I take a current screenshot/capture of MainWindow and it's content? Code for the MainWindow content is waay to long to post so I hope that this is enough.
Rectangle bounds = new Rectangle();
bounds.Width = Program.MainWindow.Width;
bounds.Height = Program.MainWindow.Height;
screenShot = new Bitmap(Program.MainWindow.Width,
Program.MainWindow.Height,
PixelFormat.Format32bppRgb);
Program.MainWindow.DrawToBitmap(screenShot, bounds);

You should be able to get it by using the CopyFromScreen method of the Graphics class
Rectangle bounds = Program.MainWindow.Bounds;
Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(b))
{
g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
}
b.Save("YOUR FILE NAME HERE");

Related

C# WinForms - Exclude edge from this.Bounds

I'm using this code to grab an image of my form:
Rectangle bounds = this.Bounds;
bitmapScreen = new Bitmap(bounds.Width, bounds.Height);
using (Graphics gImage = Graphics.FromImage(bitmapScreen))
{
gImage.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
However, in Windows 10, I'm getting the desktop or other windows showing through as the bounds are wide due to the transparent/shaded edges outside of my actual content. How can I limit this bitmap to the actual window/form bounds?
It seems as though these extra edges occur on the left, bottom and right only. (See picture)Extra Edges
This seems to work:
bitmapScreen = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmapScreen, new Rectangle(Point.Empty, bitmapScreen.Size));

C# CopyFromScreen takes a screenshot of whats underneath the active window

I'm trying to make an application that I can open and minimize or just leave it there, then with the help of a hotkey I want it to capture a portion of a screen, I got all that covered and working fine except for when I hit the hotkey and I have a game window opened, instead of taking a screenshot of the game it takes a screenshot of what's underneath that window, I've searched literally everywhere and couldn't find an answer, this is the closest I got to:
c# screenshot of active window but doesn't caputre the window
Here's my code:
Bitmap printscreen = new Bitmap(40, 20);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(775, 515, 0, 0, printscreen.Size);
printscreen = Transform(printscreen);
Bitmap newImage = new Bitmap(100, 50);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(printscreen, new Rectangle(0, 0, 100, 50));
newImage.Save("code.jpg");
}
What I'm trying to achieve is capturing a portion of the screen of the active window or a specific window, thanks!

How to capture the screen behind of a see-through panel

I have a form, containing panel1 and panel1 is of green background and the form TransparencyKey is also set to green and now instead of panel, what's behind it shows up. So I added a button and added this code to capture the screen which I see through the panel:
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(#"F:\Image.bmp");
MessageBox.Show("Save Complete!");
But the image saved is of green color only! Any way to save what's shown behind the panel1 on screen ?
You can use Graphics.CopyFromScreen method to get a screenshot of screen. Here is a method which helps you to get a screenshot of given bounds of the screen:
public Bitmap GetDesktopImage(Rectangle bounds)
{
var bm = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bm))
{
g.CopyFromScreen(bounds.Location, new Point(0, 0), bounds.Size);
}
return bm;
}
Then it's enough to pass the screen bounds of the panel to the method. To get the screen bounds of the panel, you can pass its Bound property to RectangleToScreen method of its Parent:
var bounds = panel1.Parent.RectangleToScreen(panel1.Bounds);
var image = GetDesktopImage(bounds);
//image.Save(#"d:\bm.png", System.Drawing.Imaging.ImageFormat.Png);

Capturing an image behind a rectangle

I have written a small application which will be used in my work environment for cropping images. The windows form (.NET 3.5) that contains the image has a transparent rectangle which I use for dragging over a section of an image and hitting a button to get me whatever was behind the rectangle.
Currently I am using the code below, this is causing me issues because the area that it is capturing is off by quite a few pixels, and I think it's something to do with my CopyFromScreen function.
//Pass in a rectangle
private void SnapshotImage(Rectangle rect)
{
Point ptPosition = new Point(rect.X, rect.Y);
Point ptRelativePosition;
//Get me the screen coordinates, so that I get the correct area
ptRelativePosition = PointToScreen(ptPosition);
//Create a new bitmap
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
//Sort out getting the image
Graphics g = Graphics.FromImage(bmp);
//Copy the image from screen
g.CopyFromScreen(this.Location.X + ptPosition.X, this.Location.Y + ptPosition.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
//Change the image to be the selected image area
imageControl1.Image.ChangeImage(bmp);
}
If anyone can spot why when the image is redrawn its quite a bit out, I'd be eternally grateful at this point. Also, the ChangeImage function is fine - it works if I use a form as a select area but using a rectangle has jazzed things up a bit.
You've retrieved the relative position to the screen as ptRelativePosition, but you never actually use that - you add the rectangle's location to your form's location, which doesn't account for the form's border.
Here's that fixed, with a few optimizations:
// Pass in a rectangle
private void SnapshotImage(Rectangle rect)
{
// Get me the screen coordinates, so that I get the correct area
Point relativePosition = this.PointToScreen(rect.Location);
// Create a new bitmap
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
// Copy the image from screen
using(Graphics g = Graphics.FromImage(bmp)) {
g.CopyFromScreen(relativePosition, Point.Empty, bmp.Size);
}
// Change the image to be the selected image area
imageControl1.Image.ChangeImage(bmp);
}
Interestingly, this was because of the space between the main form and the control that the image was on and the toolbar at the top of the form separating the control and the top of the main form. To get around this I simply modified one line in capture screen to account for those pixels, as shown below:
g.CopyFromScreen(relativePosition.X + 2, relativePosition.Y+48, Point.Empty.X, Point.Empty.Y, bmp.Size);
Cheers

How can I take a picture from screen?

I want to write a program that shows one PC's screen to the others... something like presentation systems. how can i take a picture from current screen?
.NET exposes this functionality via the Screen (System.Windows.Forms) class.
// the surface that we are focusing upon
Rectangle rect = new Rectangle();
// capture all screens in Windows
foreach (Screen screen in Screen.AllScreens)
{
// increase the Rectangle to accommodate the bounds of all screens
rect = Rectangle.Union(rect, screen.Bounds);
}
// create a new image that will store the capture
Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
// use GDI+ to copy the contents of the screen into the bitmap
g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
}
// bitmap now has the screen capture

Categories

Resources