zoukankan      html  css  js  c++  java
  • GDI+图形图像技术1

    System.Drawing命名空间提供了对GDI+基本图形功能的访问,其中一些子命名空间中提供了更高级的功能。

    GDI+由GDI发展而来,是Windows图形显示程序与实际物理设备之间的桥梁。

    GDI+是GDI的增强版,主要提供以下三类服务:

    1)二维矢量图形:GDI+提供了存储图形基元自身信息的类(或结构体)、存储图形基元绘制方式信息的类以及实际进行绘制的类。

    2)图像处理:提供Bitmap、Image等类。

    3)文字显示:支持使用各种字体,字号和样式来显示文本。

    GDI接口是基于函数的,而GDI+是基于C++类的对象化的应用程序编程接口,因此使用起来比GDI更方便。

    Graphics对象表示GDI+绘图表面,是用于创建图形图像的对象。

    创建Graphics对象一般有三种方式:

    1)Paint事件的PaintEventArgs中Graphics对象

    2)用CreateGraphics方法创建Graphics对象

    3)从Image创建Graphics对象

    namespace WindowsFormsApplication2 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
            private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g1 = e.Graphics;                   //第一种方式
                Bitmap img1 = new Bitmap(300, 300);
                Graphics g2 = Graphics.FromImage(img1);     //第二种方式
                Graphics g3 = this.CreateGraphics();        //第三种方式
                MessageBox.Show("创建成功!");
            }
        }
    }

    创建Pen对象:钢笔

    namespace WindowsFormsApplication2 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
            private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g1 = e.Graphics;
                Pen p = new Pen(Color.Blue, 1);         //蓝色,宽度为1个像素宽
                g1.DrawEllipse(p, 0, 0, 300, 300);      //画椭圆
                g1.Dispose();
            }
        }
    }

    创建Brush对象(笔刷)

    1)SolidBrush            纯颜色填充图形

    2)TextureBrush           使用基于光栅的图像(位图,JPG等)填充图形

    3)LinearGradientBrush       使用颜色渐变填充图形 

    4)PathGradientBrush        使用渐变色填充图形,渐变方向是从有路径定义的图形边界指向图形的中心

    5)HatchBrush             使用各种图案填充图形

    private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g1 = e.Graphics;
                Brush b1 = new SolidBrush(Color.Blue);      //定义蓝色的画笔(纯颜色填充图形)
                g1.FillEllipse(b1, 0, 0, 100, 200);
                g1.Dispose();
            }
    private void Form1_Paint(object sender, PaintEventArgs e) {
                string path = @"e:hello1.jpg";
                Graphics g1 = e.Graphics;
                Bitmap img;
                if (System.IO.File.Exists(path)) {
                    img = new Bitmap(path);
                    Brush br = new TextureBrush(img);
                    g1.FillEllipse(br, 0, 0, 200, 200);
                    br.Dispose();
                } else {
                    MessageBox.Show("图片文件不存在!");
                }
                g1.Dispose();
            }

    LinearGradientBrush使用前要先引入System.Drawing.Drawing2D命名空间

    GDI+提供水平、垂直和对角线方向线性渐变。在默认情况下,线性渐变中的颜色均匀地变化。也可以自定义渐变,使颜色非均匀变化。

    private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g1 = e.Graphics;
                LinearGradientBrush lgb = new LinearGradientBrush(
                        new Point(0, 0),                    //定义起始点
                        new Point(300, 300),                //定义终点
                        Color.FromArgb(255, 0, 0, 255),     //起始颜色
                        Color.FromArgb(255, 0, 255, 0)      //终止颜色
                    );
                g1.FillEllipse(lgb, 0, 0, 200, 200);
                g1.Dispose();
            }

    在GDI+中,路径是由GraphicsPath对象维护的一系列线条和曲线。

    private void Form1_Paint(object sender, PaintEventArgs e) {
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(50, 0, 200, 300);
                PathGradientBrush pgb = new PathGradientBrush(gp);
                pgb.CenterColor = Color.FromArgb(255, 0, 0, 255);
                Color[] colors = { Color.FromArgb(255, 0, 255, 255) };
                pgb.SurroundColors = colors;
                e.Graphics.FillEllipse(pgb, 50, 0, 200, 300);
            }

    阴影图案由两种颜色组成:背景色、在背景上形成图案的线条的颜色。若要用阴影图案填充闭合的形状,需使用HatchBrush类对象。

    private void Form1_Paint(object sender, PaintEventArgs e) {
                //三个参数:阴影样式、阴影线颜色、背景颜色
                HatchBrush hb = new HatchBrush(HatchStyle.LargeCheckerBoard,Color.Red,Color.Yellow);    
                e.Graphics.FillEllipse(hb, 0, 0, 300, 300);
            }

    画矩形

    private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g = e.Graphics;
                Pen p = new Pen(Color.Red, 2);
                g.DrawRectangle(p, 10, 10, 260, 230);       //画笔,左上角坐标,右下角坐标
                p.Dispose();
                g.Dispose();
            }

    画弧、扇形

    private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g = e.Graphics;
                Pen p = new Pen(Color.Red, 2);
    
                g.DrawArc(p, 100, 50, 200, 200, 90, 150);       //
                g.DrawPie(p, 0, 200, 300, 200, 90, 150);        //扇形
    
                p.Dispose();
                g.Dispose();
            }

    画多边形

    private void Form1_Paint(object sender, PaintEventArgs e) {
                Graphics g = e.Graphics;
                Pen p = new Pen(Color.Red, 2);
    
                Point[] pg ={
                               new Point(0,0),
                               new Point(190,30),
                               new Point(260,260),
                               new Point(230,230),
                               new Point(160,260)
                           };
                g.DrawPolygon(p, pg);
    
                p.Dispose();
                g.Dispose();
            }

    ColorDialog控件:调色板

    private void button1_Click(object sender, EventArgs e) {
                if (colorDialog1.ShowDialog() == DialogResult.OK) {
                    this.BackColor = colorDialog1.Color;
                }
            }

    FontDialog控件:字体选择板

    private void button1_Click(object sender, EventArgs e) {
                if (fontDialog1.ShowDialog() == DialogResult.OK) {
                    richTextBox1.Font = fontDialog1.Font;
                }
            }
  • 相关阅读:
    BZOJ 3205 [Apio2013]机器人 ——斯坦纳树
    BZOJ 3782 上学路线 ——动态规划 Lucas定理 中国剩余定理
    HDU 1423 Greatest Common Increasing Subsequence ——动态规划
    BZOJ 3309 DZY Loves Math ——莫比乌斯反演
    POJ 1038 Bugs Integrated, Inc. ——状压DP
    POJ 3693 Maximum repetition substring ——后缀数组
    POJ 2699 The Maximum Number of Strong Kings ——网络流
    POJ 2396 Budget ——有上下界的网络流
    BZOJ 4650 [Noi2016]优秀的拆分 ——后缀数组
    源码安装python
  • 原文地址:https://www.cnblogs.com/fish7/p/4196967.html
Copyright © 2011-2022 走看看