Can someone help me with code to capture a screenshot of the teststack.white window? I tried the following code
Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size);
}
bitmap.Save(#"C:\Users\Desktop\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
This captures a screenshot of the form. If in place of this.bounds , I key in the teststack.white window bounds, it says cannot convert system.windows.rect to system.drawing.rectangle? Can someone provide me with the code to get this sorted? Thanks in advance.
Sure, it's easy:
Desktop.TakeScreenshot("C:\\white-framework.png", System.Drawing.Imaging.ImageFormat.Png);
If you need to manipulate the image after capturing it, you can do this:
Bitmap bitmap = Desktop.CaptureScreenshot();
Related
This question already has answers here:
C#: how to take a screenshot of a portion of screen
(4 answers)
Closed 1 year ago.
I want to take a screenshot of a specific part of a website using C# or .net Code, I can't create a Windows form application as it must be embedded into something else.
Here is what I got so far:
using System.Drawing.Imaging;
using System.Drawing;
using System;
using System.Windows.Forms;
try
{
//Creating a new Bitmap object
Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
//Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);
//Creating a Rectangle object which will
//capture our Current Screen
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
//Creating a New Graphics Object
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
//Copying Image from The Screen
captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);
//Saving the Image File (I am here Saving it in My E drive).
captureBitmap.Save(#"E:\Capture.jpg",ImageFormat.Jpeg);
//Displaying the Successfull Result
MessageBox.Show("Screen Captured");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
This code works fine as long as I am creating a windows form application, but as soon as I change it to console it doesn't work even after adding the "using System.Windows.Forms;" and it produces this error:
//Screen does not exist in the current context
So Is there any solution for this or any other way I could take a screenshot of a specific part of a website Programmatically without connecting to it at all.
Thanks in advance.
I actually found an answer one minute after posting from the suggested links "which btw didn't show in search I did for 2 days for some reason" but anyway
its This question
and its as simple as
Rectangle rect = new Rectangle(0, 0, 100, 100);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size,
CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Controls;
private QrEncoder Encoder = new QrEncoder(ErrorCorrectionLevel.H);
private Renderer renderer = new Renderer(15);
BitMatrix qr = Encoder.Encode("12345").Matrix;
Size size = Renderer.Measure(qr.Width);
Bitmap result = new Bitmap(size.Width, size.Width);
try
{
using (Graphics graphics = Graphics.FromImage(result))
renderer.Draw(graphics,qr);
return result;
}
catch
{
result.Dispose();
throw;
}
I can successfully generate QR codes with what I've got above. However, It always makes them with whitespace on all sides of the code. Is there an easy way to remove the whitespace, or, even better, generate the codes without it?
If library is not supporting it directly then you can always add one more step to crop the result bitmap.
using (Graphics graphics = Graphics.FromImage(result))
renderer.Draw(graphics,qr);
Rectangle cropRectangle = Rectangle(
borderWidth,borderHeight,result.Width-borderWidth,
result.Height-borderHeight);
return result.Clone(cropRectangle, bmp.PixelFormat);
This one is slightly confusing...
I am using Adobe's PDF Viewer control to view PDFs but I want the user to be able to drag an image onto the PDF and then when they click save it adds the image to the PDF at that location.
Implementing the PDF viewer proved quite difficult but I decided in the end to use Adobe's control, take a picture and then allow the user to draw the image ontop of the picture of the PDF. When they click save I am going to use PDFSharp to put the image onto the PDF once I've worked out where it goes but the problem I have at the moment is that I can't get a picture of the PDF.
The following code is used to get the picture but the Panel that it is attached to just appears with a white background with a red 'X' and border...
using (Bitmap bitmap = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(adobePDFViewer1.Left, adobePDFViewer1.Top), Point.Empty, adobePDFViewer1.Size);
}
panelOverPdfViewer.BackgroundImage = bitmap;
}
I don't think this is the best way of doing it but I couldn't work out any others. Any help would be appreciated!
EDIT:
Following a very helpful answer below this is working code:
Here is the code I used:
Bitmap printscreen = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
int left = this.Left + 396;
int top = this.Top + 30;
graphics.CopyFromScreen(left, top, 0, 0, printscreen.Size);
pictureBoxOverPDFView.Image = printscreen;
Look at this this Print-Screen
and try this for test work of CopyFromScreen
private void PrintScreen()
{
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(#"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
}
Is there a good library I can use in c# to take a screenshot/snapshot?
One where I can drag and drop a region of my screen, to take a screenshot/snapshot of it?
I'm not aware of any libraries that allow you to visually select a region on your screen, but a screenshot of your whole screen can be taken using the Graphics.CopyFromScreen method as follows:
var bounds = Screen.PrimaryScreen.Bounds;
using (var bmp = new Bitmap(bounds.Width,
bounds.Height,
PixelFormat.Format32bppArgb))
using (var gfx = Graphics.FromImage(bmp))
{
gfx.CopyFromScreen(bounds.X,
bounds.Y,
0,
0,
bounds.Size,
CopyPixelOperation.SourceCopy);
bmp.Save("shot.png");
}
How would I create an application that records the users interactions with the desktop in C# and then convert it to a video format such as avi?
To capture screenshots you can use CopyFromScreen.
Simple C# example:
Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
// TODO: Save or use in any other way.
}
However, if you want to capture videos, it would be better to use something like this Stack Overflow question.