Hi there.
I have this application that basically captures a screenshot of the active window. In essence, if I double-click the application from its folder, it will capture a screenshot of the folder. If I create a shortcut to the application and double-click the shortcut it still Works perfectly. However, if I use shortcut keys to start the shortcut, it captures the process bar(start menu, systray etc.) and not the "active folder". I use windows 8, Visual Studio 2012. I have tried to build both in .net framework 3,5 and 4,5. I hope that anyone can help me, my code is below:
using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace HardCopy
{
public partial class HardCopy : Form
{
Bitmap screenShotBMP;
Rectangle rect = new Rectangle();
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public HardCopy()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
Thread.Sleep(200);
GetWindowRect(GetForegroundWindow(), out rect);
screenShotBMP = new Bitmap(rect.Width - rect.X, rect.Height - rect.Y);
using (Graphics gr = Graphics.FromImage(screenShotBMP))
{
gr.CopyFromScreen(rect.Location, Point.Empty, rect.Size);
}
pictureBox1.Image = screenShotBMP;
this.Show();
}
}
}
ThomasPosted Jul 4, 2014, 3:29 AM
it didn't work
It takes a screenshot of the whole screen and not the active window/folder/application as requested.
Thanks anywais.
Vasu GadhiyaPosted Jul 4, 2014, 12:49 AM
void takingscreenshot(int savename)
{
string s2 = savename.ToString();
Rectangle bounds = this.Bounds;
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
bmpScreenCapture.Save("D://" + s2 + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}