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

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!

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));

.NET 4.0 Graphics library - How can I capture the "whole" screen?

I'm coding a small app to take a screenshot every X seconds and I've ran into a small but annoying roadblock. Take this image, for example:
Screen captured using the default 'Print Screen' function on Windows 7
If I try to take the same screenshot by using the default .NET 4 Graphics library, the circled area doesn't show up. Same happens with Visual Studio tabbed menus and some other apps I can't remember. The rest of the image comes out fine, tho.
This is the code I'm using. I might be screwing something up but I can't figure it for the life of me. Any help would be appreciated:
memoryImage = new Bitmap(resolution.Width, resolution.Height);
Size s = new Size(memoryImage.Width, memoryImage.Height);
// Create graphics
Console.WriteLine("Creating Graphics...");
Console.WriteLine();
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
// Copy data from screen
Console.WriteLine("Copying data from screen...");
Console.WriteLine();
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
This test saves correctly the whole screen on my configuration (Windows 10, VS 2015) =>
Rectangle screenBounds = Screen.GetBounds(System.Drawing.Point.Empty);
using (Bitmap bitmap = new Bitmap(screenBounds.Width, screenBounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(0, 0, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
}
bitmap.Save("e:\\ScreenCopy.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

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 screenshot of Form and elements on form

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");

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