NEWS

[C#] Chuyển chế độ màn hình GrayScale trắng đen một click chuột

[C#] Chuyển chế độ màn hình GrayScale trắng đen một click chuột
Đăng bởi: Thảo Meo - Lượt xem: 1839 10:55:14, 10/10/2022C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách kích hoạt màn hình GrayScale sang chế độ trắng đen trên lập trình C#, Winform.

[C#] Settings Change GrayScale Color Filter in Windows

Dưới đây là giao diện demo ứng dụng C#:

gray_scale

Khi các bạn click vào button GrayScale, màn hình chỉ còn lại màu trắng và đen như hình ảnh mình truy cập website laptrinhvb.net bên dưới:

window_grayscale

Full source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static grayscale.NativeMethods;

namespace grayscale
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float redScale = 0.2126f, greenScale = 0.7152f, blueScale = 0.0722f;
            var magEffectInvert = new MAGCOLOREFFECT
            {
                transform = new[] {
                    redScale,   redScale,   redScale,   0.0f,  0.0f,
                    greenScale, greenScale, greenScale, 0.0f,  0.0f,
                    blueScale,  blueScale,  blueScale,  0.0f,  0.0f,
                    0.0f,       0.0f,       0.0f,       1.0f,  0.0f,
                    0.0f,       0.0f,       0.0f,       0.0f,  1.0f
                }
            };

            MagInitialize();
            MagSetFullscreenColorEffect(ref magEffectInvert);            
           
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            MagUninitialize();
        }
    }
    static class NativeMethods
    {
        const string Magnification = "Magnification.dll";

        [DllImport(Magnification, ExactSpelling = true, SetLastError = true)]
        public static extern bool MagInitialize();

        [DllImport(Magnification, ExactSpelling = true, SetLastError = true)]
        public static extern bool MagUninitialize();

        [DllImport(Magnification, ExactSpelling = true, SetLastError = true)]
        public static extern bool MagSetFullscreenColorEffect(ref MAGCOLOREFFECT pEffect);

        public struct MAGCOLOREFFECT
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
            public float[] transform;
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chuyển chế độ màn hình GrayScale trắng đen một click chuột
Đăng bởi: Thảo Meo - Lượt xem: 1839 10:55:14, 10/10/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.