開発環境 Microsoft Visual Studio Community 2017
実行環境 Microsoft Windows 10 Home (64-bit)
プロジェクトの種類 Visual C# / Windows フォーム アプリケーション (.NET Framework)
プロジェクト名 cmat

Form1.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
 
namespace cmat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            var pic = pictureBox1;
            var bmp = new Bitmap(pic.Width, pic.Height);
            pic.Image = bmp;
            var g = Graphics.FromImage(bmp);
            for (var i = 0; i < 8; i++)
            {
                var color = Color.FromArgb(0xff,
                    0xff * ((i >> 1) & 1), 0xff * ((i >> 2) & 1), 0xff * (i & 1));
                var brush = new SolidBrush(color);
                g.FillRectangle(brush, 32 * i, 0, 32, 256);
            }
            {
                g.CompositingMode = CompositingMode.SourceCopy;
                var brush = new SolidBrush(Color.FromArgb(0, 0, 0, 0));
                g.FillRectangle(brush, 0, 128, 256, 64);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            var img = pictureBox1.Image;
            var bmp = new Bitmap(img.Width, img.Height);
            pictureBox2.Image = bmp;
            var g = Graphics.FromImage(bmp);
 
            var cr = 0x00 / 256f;
            var cg = 0x8c / 256f;
            var cb = 0xff / 256f;
            var ca = 0x80 / 256f;
            var cm = new ColorMatrix(new float[][] {
                new float[]{ 0, 0, 0, 0, 0 },
                new float[]{ 0, 0, 0, 0, 0 },
                new float[]{ 0, 0, 0, 0, 0 },
                new float[]{ -cr, -cg, -cb, -ca, 0 },
                new float[]{ cr, cg, cb, ca, 1 },
            });
            var ia = new ImageAttributes();
            ia.SetColorMatrix(cm);
            g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height),
                0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
        }
    }
}
 

最終更新:2018年05月16日 09:39
添付ファイル