GDI+入门(十八、GDI+实例——颜色矩阵)

十八、GDI+实例——颜色矩阵

颜色矩阵我一直没搞清楚,哪位兄弟姐妹懂的朋友教教我。我只懂得变换颜色,变形和平移还清楚怎么利用颜色矩阵来做。

private void button2_Click(object sender, EventArgs e)

        {

            float[][] colormatrix = new float[5][];

            colormatrix[0] = new float[5];

            colormatrix[0][0] = GetFloatNum("textBox1");

            colormatrix[0][1] = GetFloatNum("textBox6");

            colormatrix[0][2] = GetFloatNum("textBox11");

            colormatrix[0][3] = GetFloatNum("textBox16");

            colormatrix[0][4] = GetFloatNum("textBox21");

 

            colormatrix[1] = new float[5];

            colormatrix[1][0] = GetFloatNum("textBox2");

            colormatrix[1][1] = GetFloatNum("textBox7");

            colormatrix[1][2] = GetFloatNum("textBox12");

            colormatrix[1][3] = GetFloatNum("textBox17");

            colormatrix[1][4] = GetFloatNum("textBox22");

 

            colormatrix[2] = new float[5];

            colormatrix[2][0] = GetFloatNum("textBox3");

            colormatrix[2][1] = GetFloatNum("textBox8");

            colormatrix[2][2] = GetFloatNum("textBox13");

            colormatrix[2][3] = GetFloatNum("textBox18");

            colormatrix[2][4] = GetFloatNum("textBox23");

 

            colormatrix[3] = new float[5];

            colormatrix[3][0] = GetFloatNum("textBox4");

            colormatrix[3][1] = GetFloatNum("textBox9");

            colormatrix[3][2] = GetFloatNum("textBox14");

            colormatrix[3][3] = GetFloatNum("textBox19");

            colormatrix[3][4] = GetFloatNum("textBox24");

 

            colormatrix[4] = new float[5];

            colormatrix[4][0] = GetFloatNum("textBox5");

            colormatrix[4][1] = GetFloatNum("textBox10");

            colormatrix[4][2] = GetFloatNum("textBox15");

            colormatrix[4][3] = GetFloatNum("textBox20");

            colormatrix[4][4] = GetFloatNum("textBox25");

 

            Graphics g = panel2.CreateGraphics();

            Bitmap bmp1 = new Bitmap(Application.StartupPath + "//2.jpg");

            ColorMatrix colorMatrix = new ColorMatrix(colormatrix);

            ImageAttributes imageAtt = new ImageAttributes();

            try

            {

                if (radioButton1.Checked)

                    // colorMatrixFlags = ColorMatrixFlags.Default;

                    imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);

                if (radioButton2.Checked)

                    //colorMatrixFlags = ColorMatrixFlags.AltGrays;

                    imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);

                if (radioButton3.Checked)

                    //colorMatrixFlags = ColorMatrixFlags.SkipGrays;

                    imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);

                g.DrawImage(bmp1, panel1.ClientRectangle, 0, 0, bmp1.Width, bmp1.Height, GraphicsUnit.Pixel, imageAtt);

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

            g.Dispose();

        }

 

        private float GetFloatNum(string textboxId)

        {

            foreach (Control c in this.Controls)

            {

                if (c.Name == textboxId)

                {

                    try

                    {

                        TextBox tb = (TextBox)c;

                        return float.Parse(tb.Text);

                    }

                    catch { return 0.0f; }

                }

            }

            return 0.0f;

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

 

            textBox1.Text = "1.0";

            textBox6.Text = "0.0";

            textBox11.Text = "0.0";

            textBox16.Text = "0.0";

            textBox21.Text = "0.0";

 

            textBox2.Text = "0.0";

            textBox7.Text = "1.0";

            textBox12.Text = "0.0";

            textBox17.Text = "0.0";

            textBox22.Text = "0.0";

 

            textBox3.Text = "0.0";

            textBox8.Text = "0.0";

            textBox13.Text = "1.0";

            textBox18.Text = "0.0";

            textBox23.Text = "0.0";

 

            textBox4.Text = "0.0";

            textBox9.Text = "0.0";

            textBox14.Text = "0.0";

            textBox19.Text = "1.0";

            textBox24.Text = "0.0";

 

            textBox5.Text = "0.0";

            textBox10.Text = "0.0";

            textBox15.Text = "0.0";

            textBox20.Text = "0.0";

            textBox25.Text = "1.0";

        }

单位矩阵(原图):

在红色分量上增加255

我确实没搞懂,呵呵。

本人也在学习GDI+,写得比较简单,让高手见笑了。欢迎高手给我指点

邮箱:bobui@163.com

QQ125941562

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值