[英]How to capture all screen in C# and change the resolution of the bitmap
我试图在 C#(控制台模式下的 .NET 4.6.2)中捕获我的屏幕以获取位图的 base64 字符串。
我的问题是base64在我的情况下太大了,所以我想降低位图的分辨率,我尝试了这段代码:
public static string TakeScreenshotToBase64()
{
Bitmap memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Size size = new Size(memoryImage.Width, memoryImage.Height);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, size);
MemoryStream ms = new MemoryStream();
memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return Convert.ToBase64String(ms.ToArray());
}
如果我尝试编辑宽度和高度值,我会得到一张较小的图片,但遗憾的是我没有完整的图片。
所以我尝试从我现有的位图以另一种分辨率创建一个新的位图,但它不起作用,有什么建议吗? 感谢您的帮助。
根据要求,我希望这会有所帮助。 如果评论不够,我可以更好地解释。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace BitMapResize
{
public partial class Form1 : Form
{
/// <summary>
/// Function to Create the images.
/// </summary>
/// <param name="resizeWidth">If it is null, the application will create an image with the original size.</param>
/// <param name="resizeHeight">If it is null, the application will create an image with the original size.</param>
public static void TakeScreenshotToBase64(float? resizeWidth, float? resizeHeight)
{
using (MemoryStream ms = new MemoryStream())
{
/* First things first, creating a bitmap of the original data to the memorystream, so we can use later.*/
Bitmap memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Size size = new Size(memoryImage.Width, memoryImage.Height);
using (Graphics memoryGraphics = Graphics.FromImage(memoryImage))
{
memoryGraphics.CopyFromScreen(0, 0, 0, 0, size); // Persisting the full screen resolution.
memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // Saving in the memory stream...
}
string imageName = "Print"; // This won't change if the size is the original.
// This conditions checks if the resize parameters are not null. If they are not null, a resizing process begins.
if (!(resizeWidth is null) || !(resizeHeight is null))
{
var bmp = new Bitmap((int)resizeWidth.Value, (int)resizeHeight.Value); // Create a new bitmap with the new dimensions.
using (Graphics memoryGraphics = Graphics.FromImage(bmp)) // Using a Graphics that operates on the new bitmap.
{
// Defining scale factor. This way we maintain aspect ratio.
float scale = Math.Min(resizeWidth.Value / memoryImage.Width, resizeHeight.Value / memoryImage.Height);
int scaleWidth = (int)(memoryImage.Width * scale);
int scaleHeight = (int)(memoryImage.Height * scale);
// Optional: Set this for better output quality.
memoryGraphics.InterpolationMode = InterpolationMode.High;
memoryGraphics.CompositingQuality = CompositingQuality.HighQuality;
memoryGraphics.SmoothingMode = SmoothingMode.AntiAlias;
// Here, choose the background color for the image.
/* Why the background?
* As long as we keep the aspect ratio, if the given dimension does not respect the scale of the original dimension,
* an area of the new image will not be filled. So we can set a background color for these empty areas.
*/
Brush baseColor = new SolidBrush(Color.White);
memoryGraphics.FillRectangle(baseColor, new RectangleF(0, 0, resizeWidth.Value, resizeHeight.Value));
// Process the resize, based on the in-memory buffer that we wrote earlier.
memoryGraphics.DrawImage(Image.FromStream(ms), ((int)resizeWidth.Value - scaleWidth) / 2, ((int)resizeHeight.Value - scaleHeight) / 2, scaleWidth, scaleHeight);
}
imageName = "ResizedPrint";
// Not requested, but now can save this in a file.
using (FileStream fs = new FileStream(Application.StartupPath + "/" + imageName + ".jpg", FileMode.Create))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
using (FileStream fs = new FileStream(Application.StartupPath + "/" + imageName + ".jpg", FileMode.Create))
{
// If no resize was done, save the original data.
memoryImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
// Print event start.
private void button1_Click(object sender, EventArgs e)
{
TakeScreenshotToBase64(1024, 768);
//TakeScreenshotToBase64(null, null);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.