Suppose I have the following winform interface:
I try to get a screenshot of the whole interface, but I don't need to include the window information in the upper left corner.
I try to take a screenshot with CopyFromScreen:
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
Bitmap bit = new Bitmap(s.Width, s.Height, myGraphics);
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;
Point screenPoint = this.PointToScreen(this.Location);
g.CopyFromScreen(screenPoint, new Point(0, 0), s);
bit.Save("temp.png", System.Drawing.Imaging.ImageFormat.Png);
The result I get looks like this:
I know it might be because of screen scaling issues, mine is 150%. I tried adjusting the screen zoom to 100%, but that didn't work. If you have any comments, comments are welcome, I'm very grateful for that!
According to How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?, I made the following changes: My app.manifest file:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
My App.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
My Program.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Diploma
{
internal static class Program
{
/// <summary>
///
/// </summary>
[STAThread]
static void Main()
{
if (Environment.OSVersion.Version.Major >= 6) {
SetProcessDPIAware();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PhD_CN_B());
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
}
}
My Form file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace Diploma {
public partial class PhD_CN_B : Form {
public PhD_CN_B() {
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void CaptureScreen() {
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
Bitmap bit = new Bitmap(s.Width, s.Height, myGraphics);
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;
Point screenPoint = this.PointToScreen(this.Location);
//Console.WriteLine(screenPoint);
g.CopyFromScreen(screenPoint, new Point(0, 0), s);
bit.Save("temp.png", System.Drawing.Imaging.ImageFormat.Png);
}
private void button1_Click(object sender, EventArgs e) {
CaptureScreen();
}
private void PhD_CN_B_Load(object sender, EventArgs e) {
}
}
}
But I still can't get a full screenshot!