现在的位置: 首页 > 综合 > 正文

C# GDI+入门(2)-画刷的使用

2012年12月28日 ⁄ 综合 ⁄ 共 6367字 ⁄ 字号 评论关闭

二、画刷的使用

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

 

     public Form1()

        {

            InitializeComponent();

            comboBox1.SelectedIndex = 0;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Bitmap bp = new Bitmap("1.jpg");

            TextureBrush tb = new TextureBrush(bp);

            Graphics g = this.CreateGraphics();

            g.FillRectangle(tb, this.ClientRectangle);

            bp.Dispose();

            tb.Dispose();

            g.Dispose();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Bitmap bp = new Bitmap("1.jpg");

            TextureBrush tb = new TextureBrush(bp, new Rectangle(0, 0, 100, 100));

            Graphics g = this.CreateGraphics();

            switch (comboBox1.SelectedItem.ToString().Trim())

            {

                case "Clamp":

                    tb.WrapMode = WrapMode.Clamp;

                    g.FillRectangle(tb, this.ClientRectangle);

                    bp.Dispose();

                    tb.Dispose();

                    g.Dispose();

                    break;

                case "Tile":

                    tb.WrapMode = WrapMode.Tile;

                    g.FillRectangle(tb, this.ClientRectangle);

                    bp.Dispose();

                    tb.Dispose();

                    g.Dispose();

                    break;

                case "TileFlipX":

                    tb.WrapMode = WrapMode.TileFlipX;

                    g.FillRectangle(tb, this.ClientRectangle);

                    bp.Dispose();

                    tb.Dispose();

                    g.Dispose();

                    break;

                case "TileFlipXY":

                    tb.WrapMode = WrapMode.TileFlipXY;

                    g.FillRectangle(tb, this.ClientRectangle);

                    bp.Dispose();

                    tb.Dispose();

                    g.Dispose();

                    break;

                case "TileFlipY":

                    tb.WrapMode = WrapMode.TileFlipY;

                    g.FillRectangle(tb, this.ClientRectangle);

                    bp.Dispose();

                    tb.Dispose();

                    g.Dispose();

                    break;

            }

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            Bitmap bp = new Bitmap("1.jpg");           

            float[][] matrixitems ={

                                    new float[]{0.2f,0,0,0,0},

                                    new float[]{0,0.8f,0,0,0},

                                    new float[]{0,0,1,0,0},

                                    new float[]{0,0,0,1,0},

                                    new float[]{0,0,0,0,1},

                                    };

            ColorMatrix colotMatrix = new ColorMatrix(matrixitems);

 

            ImageAttributes imgAtt = new ImageAttributes();

            imgAtt.SetColorMatrix(colotMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            TextureBrush tb = new TextureBrush(bp,new Rectangle(0,0,bp.Width,bp.Height),imgAtt);

            tb.WrapMode = WrapMode.Tile;

            Graphics g = this.CreateGraphics();

            g.FillRectangle(tb, this.ClientRectangle);

            bp.Dispose();

            tb.Dispose();

            g.Dispose();

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(300, 300), Color.Wheat, Color.Black);

            g.FillRectangle(lgb, this.ClientRectangle);

            g.Dispose();

            lgb.Dispose();

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(10, 10, 110, 15);

            gp.AddLine(110,15,100,96);

            gp.AddLine(100, 96, 15, 110);

            gp.CloseFigure();

            g.FillRectangle(Brushes.AliceBlue, this.ClientRectangle);

            g.DrawPath(Pens.AntiqueWhite, gp);

            gp.Dispose();

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(10, 10, 110, 15);

            gp.AddLine(110, 15, 100, 96);

            gp.AddLine(100, 96, 15, 110);

            gp.CloseFigure();

            g.FillRectangle(Brushes.Blue, this.ClientRectangle);

            g.SmoothingMode = SmoothingMode.AntiAlias;

            PathGradientBrush pgb = new PathGradientBrush(gp);

            pgb.CenterColor = Color.White;

            pgb.SurroundColors=new Color[]

            {

                //Color.Yellow,

                Color.Blue

                //Color.Yellow,

                //Color.Red

            };

            g.FillPath(pgb,gp);

            g.DrawPath(Pens.Blue,gp);

            pgb.Dispose();

            gp.Dispose();

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.White, Color.Blue);

            g.FillRectangle(hb, this.ClientRectangle);

            hb.Dispose();

        }

 

        private void button8_Click(object sender, EventArgs e)

        {

            Graphics g=this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            HatchBrush hb = new HatchBrush(HatchStyle.WideUpwardDiagonal,

                Color.White,

                Color.Black);

            Pen p = new Pen(hb, 8);

            g.DrawRectangle(p, 15, 15, 70, 70);

            hb.Dispose();

            p.Dispose();

            g.Dispose();

        }

说明:

1. button1_Click用简单的方式对工作窗口进行填充

this.ClientRectangle当前的工作窗口

 


2. button2_Click:更改TextureBrush画刷的平铺方式

Tile 平铺渐变或纹理。

 TileFlipX 水平反转纹理或渐变,然后平铺该纹理或渐变。

 TileFlipY 垂直反转纹理或渐变,然后平铺该纹理或渐变。

 TileFlipXY 水平和垂直反转纹理或渐变,然后平铺该纹理或渐变。

 Clamp 纹理或渐变没有平铺。

Clamp

 

Tile


TileFlipX

 

TileFlipXY

 

TileFlipY

 

button3_Click颜色矩阵的使用

ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。

ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。ImageAttributes 对象维护多个颜色调整设置,包括颜色调整矩阵、灰度调整矩阵、灰度校正值、颜色映射表和颜色阈值。呈现过程中,可以对颜色进行校正、调暗、调亮和移除。要应用这些操作,应初始化一个 ImageAttributes 对象,并将该 ImageAttributes 对象的路径(连同 Image 的路径)传递给 DrawImage 方法。

 

button4_ClickLinearGradientBrush渐变画刷的使用

 

button5_ClickGraphicsPath表示一系列相互连接的直线和曲线

CloseFigure闭合当前图形并开始新的图形。如果当前图

抱歉!评论已关闭.