C
C#9mo ago
xml

All Monitor ScreenShot

Even if I have more than one monitor, this code only takes the screenshot of the main monitor, how can I fix this?
public class ScreenCapture
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

public static Bitmap CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}

public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new User32.Rect();
User32.GetWindowRect(handle, ref rect);

int width = rect.right - rect.left;
int height = rect.bottom - rect.top;

var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

return bmp;
}
}
public class ScreenCapture
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

public static Bitmap CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}

public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new User32.Rect();
User32.GetWindowRect(handle, ref rect);

int width = rect.right - rect.left;
int height = rect.bottom - rect.top;

var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

return bmp;
}
}
14 Replies
leowest
leowest9mo ago
GetWindowRect only gives u the size of one monitor
xml
xml9mo ago
Just wondering if I will use Windows Forms or can I run this code in a console application? i dont understand :c
leowest
leowest9mo ago
The code u post above wont run on console if u dont reference Drawing library either but its possible yes
xml
xml9mo ago
using System.Drawing.Imaging; using System.Drawing; I added it and it works fine but i dont understand System.Windows.Forsm
leowest
leowest9mo ago
how are u adding
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing;
did u manually reference the DLL?
xml
xml9mo ago
i use nuget also i currently use this code and ss one screen
leowest
leowest9mo ago
can you show me your csproj content
xml
xml9mo ago
i dont love c# :sadge: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> <ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="System.Drawing.Common" Version="8.0.1" /> <PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0" /> </ItemGroup> </Project> this code currently work but kust one screen
leowest
leowest9mo ago
well normally u dont need to nuget those libraries what u do is u change your csproj to include
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
just like u almsot have it there u just missed the -windows then u should be able to use System.Windows.Forms
xml
xml9mo ago
alright i will try thx
leowest
leowest9mo ago
if u have trouble using the link I posted above let me know is very simple
xml
xml9mo ago
oki dokie thx its work thx @leowest
leowest
leowest9mo ago
no worries glad u got it working
Want results from more Discord servers?
Add your server