Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C# Programming › [Snippet] Capture Window Screenshot

Lightbulb[Snippet] Capture Window Screenshot

Posts 1–3 of 3 · Page 1 of 1
GE
Geometrical
[Snippet] Capture Window Screenshot
Here's my method of taking a screenshot of a window. Not the most efficient way to do it, but hey, it works.

Required namespaces:
Code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
Required external methods:
Code:
        [DllImport("User32.dll")]
        public static extern Int32 SetForegroundWindow(int hWnd);

        [DllImport("user32.dll")]
        static extern bool GetWindowRect(int hwnd, out Rectangle lpRect);
Code:
Code:
        Image CaptureWindowScreenshot(string processName, int processInstanceIndex)
        {
            int processMainWindowHandle = Process.GetProcessesByName(processName)[processInstanceIndex].MainWindowHandle.ToInt32();
            SetForegroundWindow(processMainWindowHandle);
            Thread.Sleep(500);
            Rectangle rectangle = new Rectangle();
            GetWindowRect(processMainWindowHandle, out rectangle);
            Image windowScreenshot = (Image)new Bitmap(rectangle.Width, rectangle.Height);
            using (Graphics graphics = Graphics.FromImage(windowScreenshot))
            {
                //graphics.CompositingMode = CompositingMode.SourceCopy;
                //graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //graphics.CompositingQuality = CompositingQuality.HighQuality;
                //graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.CopyFromScreen(rectangle.Location, rectangle.Location, rectangle.Size);
            }
            return windowScreenshot;
        }
Usage:
Code:
            Image windowScreenshot;
            try
            {
                windowScreenshot = CaptureWindowScreenshot("processName", 0);
                windowScreenshot.Save("windowScreenshot.png");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
#1 · 11y ago
abuckau907
abuckau907
The 2 sources I trust most when it comes to Windows(R) and API declarations:

pinvoke for GetWindowRect: http://www.pinvoke.net/default.aspx/....getwindowrect

<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(ByVal hWnd As HandleRef, ByRef lpRect As RECT) As Boolean
End Function

Notes:
The Win32 RECT is not binary compatible with System.Drawing.Rectangle.
msdn for GetWindowRect(): http://msdn.microsof*****m/en-us/libr...=vs.85%29.aspx

In this, do NOT use System.Drawing.Rectangle as the type for rect. If you do this, the Rectange.Width and Rectangle.Height properties get populated, instead of Rectangle.Right and Rectangle.Bottom.

Tijaytas
1/10/2011
the values for .Right/Width and .Bottom/Height will be wrong in lpRect. Use Win32.RECT instead.
^^unless the form is at 0,0

The region/image returned will likely be too large and contain more than just the window. ?
#2 · edited 11y ago · 11y ago
ZE
ZER0MEM0RY
This would be a way better method, you dont even have to use Pinvoke.
Code:
  using System.Drawing.Imaging;

        private static Bitmap BitmapScreenshot;
        private static Graphics gfx;
        void capScreen()
        {
            BitmapScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            gfx = Graphics.FromImage(BitmapScreenshot);
            gf*****pyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size,
                CopyPixelOperation.SourceCopy);
            BitmapScreenshot.Save("path", ImageFormat.Png);
            //pictureBox1.Image = BitmapScreenshot;
            //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }
#3 · 11y ago
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

  • Windows 7 Build 7127 RTM-Branch 100+ ScreenshotsBy EndRiT in General
    11Last post 17y ago
  • how to give fake screenshot in asc(auto screen capture)By gobarsingh in Call of Duty Modern Warfare 3 Private Server Hacks
    4Last post 13y ago
  • How to take a screenshot (windows only)By thespartan in Combat Arms Discussions
    5Last post 16y ago
  • [Tut][Visual Basic][Snippet][C&P] Resizing a Window to a photosizeBy CoderNever in Visual Basic Programming
    4Last post 16y ago
  • WarrocK "capture the moment" screenshotsBy reaper in WarRock Discussions
    30Last post 16y ago

Tags for this Thread

#c-sharp#coding#programming#screenshot#screenshot capture#window capture#window screenshot