天天看點

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+,寫得比較簡單,讓高手見笑了。歡迎高手給我指點

郵箱:[email protected]

QQ:125941562

繼續閱讀