我在不同的屏幕分辨率上得到了两个不同的屏幕截图结果,首先我尝试使用1920 * 1080(这是我的电脑默认分辨率)得到缩放不理想的结果后来我尝试更改我的电脑分辨率1280 * 720这次工作精细。 这是我的示例代码:
private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Width);
using (Graphics G = Graphics.FromImage(bmp))
{
G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), ClientSize);
double percent = 0.60;
Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
using (Brush brsh = new SolidBrush(darken))
{
G.FillRectangle(brsh, this.ClientRectangle);
}
}
saveDialog.FileName = "test";
saveDialog.DefaultExt = "jpg";
saveDialog.Filter = "JPG images (*.jpg)|*.jpg";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
var fileName = saveDialog.FileName;
if (!System.IO.Path.HasExtension(fileName) || System.IO.Path.GetExtension(fileName) != "jpg")
fileName = fileName + ".jpg";
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
以1920 * 1080分辨率拍摄的图像:zoomed image
以1280 * 720分辨率拍摄的图像:Fine image
屏幕分辨率如何影响截屏?如何在我的电脑默认分辨率(1920 * 180)上获得所需的结果?请帮忙。感谢。
答案 0 :(得分:1)
仅供参考,我得到的代码非常适合拍摄截图。但我没有仔细检查有多少功能。所以我只是分享我的代码作为答案,我希望它对你有用。有些dll需要你可以轻松添加。
CODE:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace myScreenCapture
{
public enum CaptureMode
{
Screen, Window
}
public static class ScreenCapturer
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left, Top, Right, Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
public static void CaptureAndSave(string filename, string path, CaptureMode mode = CaptureMode.Window, ImageFormat format = null)
{
ImageSave(filename, format, Capture(mode), path);
}
public static void CaptureAndSave(string filename, IntPtr handle, string path, ImageFormat format = null)
{
ImageSave(filename, format, Capture(handle), path);
}
public static void CaptureAndSave(string filename, Control c, string path, ImageFormat format = null)
{
ImageSave(filename, format, Capture(c), path);
}
public static Bitmap Capture(CaptureMode mode = CaptureMode.Window)
{
return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
}
public static Bitmap Capture(Control c)
{
return Capture(c.Handle);
}
public static Bitmap Capture(IntPtr handle)
{
Rectangle bounds;
var rect = new Rect();
GetWindowRect(handle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return result;
}
/// <summary> Position of the cursor relative to the start of the capture </summary>
public static Point CursorPosition;
static void ImageSave(string filename, ImageFormat format, Image image, string path)
{
format = format ?? ImageFormat.Png;
if (!filename.Contains("."))
filename = filename.Trim() + "." + format.ToString().ToLower();
if (!filename.Contains(@"\"))
{
// filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @"C:\Temp", filename);
filename = Path.Combine(path, filename);
}
filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd@hh.mm.ss"));
image.Save(filename, format);
}
}
}
使用类似:
ScreenCapturer.CaptureAndSave("ScreenShot_" + DateTime.Now.ToString("dd-MM-yyyy@TT@HH_mm_ss"), path, CaptureMode.Screen, System.Drawing.Imaging.ImageFormat.Jpeg);