C# screenshot application - c#

What should I use in C# windows forms to create a app to make screenshot like Yahoo Messenger does?
This is an example:
How is that transparent cover made? A form?

First of all you need a global keyboard hook and check for the input of the printscreen key (prt sc)
Take a look at this:
Global keyboard capture in C# application
Second thing is to take a screenshot of the whole screen.
Size res = new Size(
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height
);
Point ptr = new Point(
Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y
);
using (var bmp = new Bitmap(res.Width, res.Height))
{
using (var gfx = Graphics.FromImage(gmp))
{
gfx.CopyFromScreen(ptr.X, ptr.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
}
}
Now all you have to is implement some way to crop the screenshot. This is not a spoonfeeding community, so the rest is up to you, besides you have to be more specific.

Related

Take a screenshot of part of screen in c# [duplicate]

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

How can take an screenshot for an specific section in a windows universal app (doesn't matter the computer resolution)

I'm working in a company where the owner wish take a screenshot of a windows mail app; specifically the section where the e-mail are show; and if the window or the section have an scroll, then must avoid it and take the screenshot of that whole section.
I'm building it on a .net console app, and I have download a lot of examples where shows just how to take a screeshot of an specific or any window.
The closest code (I think) that I found was this:
IntPtr current = IntPtr.Zero;
IntPtr handle = IntPtr.Zero;
List<IntPtr> thumbs = new List<IntPtr>();
if (handle == IntPtr.Zero)
handle = ((System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(this)).Handle;
current = DWM.GetWindow(handle, DWM.GetWindowCmd.First);
do
{
int GWL_STYLE = -16;
int TASKSTYLE = 0x10000000 | 0x00800000;
if (TASKSTYLE == (TASKSTYLE & DWM.GetWindowLong(current, GWL_STYLE)))
{
thumbs.Add(current);
}
current = DWM.GetWindow(current, DWM.GetWindowCmd.Next);
if (current == handle)
current = DWM.GetWindow(current, DWM.GetWindowCmd.Next);
}
while (current != IntPtr.Zero);
this.DataContext = thumbs;
What customer expects it's to take an screenshot of a windows mail app but as I said before, the section that shows the e-mail in fact. So, it must looks something like:
Result
I am not sure how to do it from a Console Application, as that is very limited, but you can create a screen capture program from Windows Forms significantly easier.
The course I took from Huw Collingbourne who will teach you how to do it. It is not a small program to type out in this box though.
The program he teaches had a mainform, then would pop up a smaller transparent form that you would maneuver over the area you want to capture. To capture you would press the button which would capture the transparent form with this code:
formRect = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);
this.Hide();
mainForm.GrabRect(formRect);
Going back to the main form the program would put the picture on the picturebox for you to view.
public void GrabRect(Rectangle rect)
{
int rectWidth = rect.Width - rect.Left;
int rectHeight = rect.Height - rect.Top;
Bitmap bm = new Bitmap(rectWidth, rectHeight);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, new Size(rectWidth, rectHeight));
this.pb_screengrab.Image = bm;
Clipboard.SetImage(bm);
g.Dispose();
}
Here are the links to his course
https://bitwisecourses.com/p/program-a-screen-capture-tool-in-c-sharp
and his course sold through udemy
https://www.udemy.com/course/program-a-screen-capture-tool-in-c/learn/lecture/15760316#content
bitwisecourses.com is his direct website I believe. Otherwise Udemy will offer nice discounts like $10-$13 for their various classes. Once you bought a bunch and don’t do anything for a few months they will try to charge you full price, but you only need to email them and they will start discounting you again.
I put everything I did here: https://github.com/johnbnstx/HuwBurn_ScreenGrab
I hope this helps.

C# drawing on picturebox, not persistent

I have a List<> object, "imagelist", that contains the paths of many images such as .png's. Now, with the following code:
private void paint_picture(PictureBox picture, string pathofpic)
{
Graphics g = picture.CreateGraphics();
Bitmap drawnpic = null;
if (imagelist.ContainsKey(picture.Name))
{
drawnpic = new Bitmap(pathofpic);
g.DrawImage(drawnpic, 0, 0, picture.Size.Width, picture.Size.Height);
imagelist[picture.Name] = pathofpic;
}
drawnpic.Dispose();
g.Dispose();
}
I call this every time the card's image is changed, but I can't seem to make the image persist on the picturebox, when I drag the picturebox across the form (for example, over other pictureboxes). The click and drag code is just moving the picturebox with the mouse, not really relevant.
I've tried invalidating the form when I de-select the image, but it doesn't do anything.
Is there something I'm missing? Screenshot below, I dragged one image around the form and it overwrote the other images it moved across:
That’s how painting works – you have to handle its Paint event and keep painting the same thing each time it needs repainting.
What you can do is draw on top of your original image:
Bitmap b = new Bitmap(picture.Image);
using (Graphics g = Graphics.FromImage(b)) {
using (Bitmap drawnpic = new Bitmap(pathofpic)) {
g.DrawImage(drawnpic, 0, 0, b.Width, b.Height);
}
}
picture.Image = b;
Then you’d save the original image somewhere and probably use it instead of picture.Image in the new Bitmap line.
And PascalCase for method names, please. ;)

Mark correct sign on file or folder icon at bottom left after upload same as Dropbox does

Assuming that you know about drop box(no problem if you don't know).
In my desktop application there is an upload functionality. I want to mark correct sign on file icon at bottom left after upload.(same as Dropbox does).
How can I do that? What's that trick?
DropBox is a shell extension, so it uses the OS icons and overlay them.
In your case, if it's a desktop application you can overlay your icons using something similar to this:
private static object mOverlayLock = new object();
public static Image GetOverlayedImage(Image baseImage, Image overlay)
{
Image im = null;
lock (mOverlayLock)
{
try
{
im = baseImage.Clone() as Image;
Graphics g = Graphics.FromImage(im);
g.DrawImage(overlay, 0, 0, im.Width, im.Height);
g.Dispose();
}
catch
{
// LOG EXCEPTION!!
}
}
return im;
}
This is a basic example. You can also play with the overlay position, (topleft, middleleft ...), that requires a little bit more programming.
Then, from your application you can call this method to get the result image. For example
...
Image folderIcon = GetFolderIcon();
Image upToDateOverlay = GetUpToDateOverlay();
Image folderUptoDate = GetOlverlayedImage(folderIcon, upToDateOverlay);
// Then assign this image to your control item (treelistnode, listViewnode, whatever)

C# library to take screenshots?

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

Categories