簡體   English   中英

如何在 C# 中捕獲所有屏幕並更改位圖的分辨率

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM