使用SolidBrush 单色画笔

Bitmap bitmap = new Bitmap(800, 600);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            SolidBrush mySolidBrush = new SolidBrush(Color.Yellow);
            graphics.FillEllipse(mySolidBrush, 70, 20, 100, 50);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());

img派生类的对象->画布->清理画布->声明画笔->画椭圆->把img存到内存流中->二进制数组从服务器发送到浏览器上

使用HatchBrush绘制简单图案

Bitmap bitmap = new Bitmap(200, 100);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            HatchBrush myhatchBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Green, Color.Orange);
            graphics.FillEllipse(myhatchBrush, 0, 0, 200, 100);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());

img派生类的对象->画布->清理画布->声明画笔->画椭圆->把img存到内存流中->二进制数组从服务器发送到浏览器上

使用TextureBrush类绘制复杂图案

     Bitmap bitmap = new Bitmap(400, 200);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            TextureBrush myTextureBrush = new TextureBrush(System.Drawing.Image.FromFile(Server.MapPath("~/4.jpg")));
            graphics.FillEllipse(myTextureBrush, 0, 0, 400, 200);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());

使用LinearGradientBrush类定义现行渐变

     Bitmap bitmap = new Bitmap(200, 100);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            Rectangle recctangle = new Rectangle(0, 0, 200, 100);
            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(recctangle, Color.White, Color.Green, LinearGradientMode.ForwardDiagonal);
            graphics.FillRectangle(myLinearGradientBrush, 0, 0, 200, 100);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());

img派生类的对象->画布->清理画布->声明矩形->声明画笔->画矩形->把img存到内存流中->二进制数组从服务器发送到浏览器上

使用PathGradientBrush 类 实现彩色渐变

Bitmap bit = new Bitmap(400, 200);
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.White);
            Point centerPoint = new Point(100, 100);
            int R = 100;
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R);
            PathGradientBrush myPathGradientBrush = new PathGradientBrush(path);
            //指定路径中心点
            myPathGradientBrush.CenterPoint = centerPoint;
            //指定路径中心点的颜色
            myPathGradientBrush.CenterColor = Color.DarkGreen;
            //Color类型的数组指定与路径上每个顶点对应的颜色
            myPathGradientBrush.SurroundColors = new Color[] { Color.Gold };
            g.FillEllipse(myPathGradientBrush, centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R);
            centerPoint = new Point(300, 100);
            R = 33;
            path = new GraphicsPath();
            path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R);
            path.AddEllipse(centerPoint.X - 2 * R, centerPoint.Y - 2 * R, 4 * R, 4 * R);
            path.AddEllipse(centerPoint.X - 3 * R, centerPoint.Y - 3 * R, 6 * R, 6 * R);
            myPathGradientBrush = new PathGradientBrush(path);
            myPathGradientBrush.CenterPoint = centerPoint;
            myPathGradientBrush.CenterColor = Color.Gold;
            myPathGradientBrush.SurroundColors = new Color[] { Color.Black, Color.Blue, Color.DarkGreen };
            g.FillPath(myPathGradientBrush, path);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());

绘制直线DrawLines

 Pen pen = new Pen (Color.Black,3);

Point[] points={new Point(10,10),new Point(10,100),new Point(200,50),new Point(260,120)};

g.DradLines(pen,points);

绘制矩形Rectangle

1)

Rectangle rect= new Rectangle(0,0,80,50);

2)

Pen pen = new Pen (Color.Black,3);

Rectangles[] rects={new Rectangle(10,10,100,200),new Rectangle(100,200,250,50),new Rectangle(300,10,50,100)}

g.DrawRectangles(pen,rects);

绘制椭圆

DrawEllipse(Pen pen,Rectangle rect);

DrawEllipse(Pen pen,Int x,Int y,int width,int height);

绘制圆弧

DrawArc(Pen pen,Rectangle rec,float starAngle,float sweepAngle)

DrawArc(Pen pen,int x,int y,int width,int height,int startAngle,int sweepAngle)

绘制扇形

DrawPie

画直线

1)public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)

2)public void DrawLine(Pen pen,Point pt1,Point pt2)

画矩形

DrawRectangle

画椭圆

DrawEllipse

画圆弧

DrawArc

画扇形

DrawPie

多边形

Graphics ghs = Graphics.FromImage(bitmap);
            ghs.Clear(Color.White);
            Pen myPen = new Pen(Color.Black, 3);
            Point point1 = new Point(80, 20);
            Point point2 = new Point(40, 50);
            Point point3 = new Point(80, 80);
            Point point4 = new Point(160, 80);
            Point point5 = new Point(200, 50);
            Point point6 = new Point(160, 20);
            Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
            ghs.DrawPolygon(myPen, myPoints);

柱状图(投票例子):

protected void Page_Load(object sender, EventArgs e)
        {
            int Sum = 100;
            int v1 = 12;
            int v2 = 20;
            int v3 = 50;
            int v4 = 15;
            float p1 = 100 * Convert.ToSingle(v1) / Convert.ToSingle(Sum);
            float p2 = 100 * Convert.ToSingle(v2) / Convert.ToSingle(Sum);
            float p3 = 100 * Convert.ToSingle(v3) / Convert.ToSingle(Sum);
            float p4 = 100 * Convert.ToSingle(v4) / Convert.ToSingle(Sum);
            int width = 300, height = 300;//宽度和高度
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            try
            {
                g.Clear(Color.White);
                //创建6个brush对象 ,用于填充颜色
                Brush brush1 = new SolidBrush(Color.White);
                Brush brush2 = new SolidBrush(Color.Black);
                Brush brush3 = new SolidBrush(Color.Red);
                Brush brush4 = new SolidBrush(Color.Green);
                Brush brush5 = new SolidBrush(Color.Orange);
                Brush brush6 = new SolidBrush(Color.DarkBlue);
                //创建连个Font对象,用于设置字体
                Font font1 = new Font("Courier New", 16, FontStyle.Bold);
                Font font2 = new Font("Courier New", 8);
                g.FillRectangle(brush1, 0, 0, width, height);
                g.DrawString("投票结果", font1, brush2, new Point(90, 20));
                //设置坐标
                Point point1 = new Point(70, 50);
                Point point2 = new Point(230, 50);
                g.DrawLine(new Pen(Color.Black), point1, point2);
                //设置文字
                g.DrawString("文字111", font2, brush2, new Point(10, 80));
                g.DrawString("文字222", font2, brush2, new Point(32, 110));
                g.DrawString("文字333", font2, brush2, new Point(32, 140));
                g.DrawString("文字444", font2, brush2, new Point(54, 170));
                //绘制柱状图
                g.FillRectangle(brush3, 95, 80, p1, 17);
                g.FillRectangle(brush4, 95, 110, p2, 17);
                g.FillRectangle(brush5, 95, 140, p3, 17);
                g.FillRectangle(brush6, 95, 170, p4, 17);
                //绘制所有选项的票数显示
                g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80);//绘制范围框
                g.DrawString("文字111" + v1.ToString() + "票", font2, brush2, new Point(15, 220));
                g.DrawString("文字222" + v2.ToString() + "票", font2, brush2, new Point(150, 220));
                g.DrawString("文字333" + v3.ToString() + "票", font2, brush2, new Point(15, 260));
                g.DrawString("文字444" + v4.ToString() + "票", font2, brush2, new Point(150, 260));
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.ClearContent();
                Response.ContentType = "image/Jpeg";
                Response.BinaryWrite(ms.ToArray());
            }
            catch (Exception ex)
            {
            }
        }

GDI+(Graphics Device Interface)例子的更多相关文章

  1. GDI+(Graphics Device Interface)

    1创建画布(创建Graphics对象) Bitmap bitmap = new Bitmap(80,80); Graphics g=Graphics.FromImage(bitmap); 2创建Pen ...

  2. [转]C#中基于GDI+(Graphics)图像处理系列之前言

    直接给出原文链接吧: C#中基于GDI+(Graphics)图像处理系列之前言 链接:https://pan.baidu.com/s/1zm5TCOHqkqEfiLZuqO0UMA 提取码:qz0h

  3. day20 GUI(Graphics User Interface)

    顶层容器:JWindow.JFrame.JDialge.JAsplet JFrame,默认布局是边界布局 JFrame的内容面板是:Container. 面板容器:JPanel,默认布局是流布局. 布 ...

  4. C# GDI graphics.DrawImage 的参数问题

    graphics.DrawImage(imageSource, new System.Drawing.Point[] { ,), , ), , ), } ); graphics.DrawImage(i ...

  5. C#中基于GDI+(Graphics)图像处理系列

    https://blog.csdn.net/lhtzbj12/article/details/54024821

  6. 超全面的.NET GDI+图形图像编程教程

    本篇主题内容是.NET GDI+图形图像编程系列的教程,不要被这个滚动条吓到,为了查找方便,我没有分开写,上面加了目录了,而且很多都是源码和图片~ (*^_^*) 本人也为了学习深刻,另一方面也是为了 ...

  7. [转]超全面的.NET GDI+图形图像编程教程

    本篇主题内容是.NET GDI+图形图像编程系列的教程,不要被这个滚动条吓到,为了查找方便,我没有分开写,上面加了目录了,而且很多都是源码和图片~ GDI+绘图基础 编写图形程序时需要使用GDI(Gr ...

  8. 【笨嘴拙舌WINDOWS】GDI(1)

    GDI:Graphics Device Interface 图形设备接口. 操作系统从命令行界面到图形界面的过度是施乐公司实验室对计算机普及作出的不可估量的贡献,苹果公司乔布斯与微软公司比尔盖茨对其的 ...

  9. Winform GDI+

    什么是GDI+ GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface). 因为应用程序不能直 ...

随机推荐

  1. SqlServer 不同服务器之间数据库连接、数据库登录、数据传递

    需求:我是本地数据库想纯SQL访问其它服务器上的数据库,而不使用数据库客户端的连接.这里面就想到了数据库link,通过下面的代码进行创建以后,就可以在本地对链接的服务器数据库进行操作了--添加SQLS ...

  2. cocos2dx 帧动画(iOS)

    植物大战僵尸的植物摇摆效果 //帧动画 Animation *animation = Animation::create(); Sprite *sprite = Sprite::create(&quo ...

  3. C#--方法的参数类型

    在C#中,方法的参数类型有四种: 值类型 引用类型 输出类型 数组型参数 值参数: 所谓值参数,就是利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并将此拷贝传递给该方法,这样做的结果就是被调用 ...

  4. poj2409

    用n个颜色的珠子编项链,求有多少种情况 由N(G,C) = 所有f的稳定核的和/|G| m边形有m种旋转m种翻转 首先说旋转,有模线性方程可知每种旋转都有gcd(m,i)个循环节且每个循环节长度为n/ ...

  5. php 如何写入、读取word,excel文档

    如何在php写入.读取word文档 <? //如何在php写入.读取word文档 // 建立一个指向新COM组件的索引 $word = new COM("word.applicatio ...

  6. .net转php laraval框架学习系列(二)项目实战---Models

    上一篇已经介绍开发环境的搭建,如果有问题可以在文章后留言. 这篇将从项目实战开发,一步一步了解laravel框架. 在开发mvc项目时,models都是第一步. 下面就从建模开始. 实体关系图 由于不 ...

  7. 给大家推荐几款OSX上非常好工具(针对程序员)

    前两天,在App Store无意中发现几款免费工具,感觉非常好用,推荐给大家,希望大家喜欢. 一个是帮助文档管理软件,Dash.以前在Windows上开发,经常要自己搜集一些手册,文档,什么html手 ...

  8. 类和对象:给大家介绍对象 - 零基础入门学习Python036

    类和对象:给大家介绍对象 让编程改变世界 Change the world by program 我们之前说过Python无处不对象,Python到处都是对象,然后你会发现很多童鞋其实并不知道对象是什 ...

  9. 解决“您必须先更新GOOGLE play才能运行此应用”的问题

    可以手机FQ然后更新,但是这样更新速度很慢,而且google商店上面的版本还是老版本. 正确的方法:去https://www.pushbullet.com/channel-popup?tag=am21 ...

  10. C++中,如何在标准库的std::string和常用库(Qt,VC等)的QString之间进行选择?

    假设一个场景:在写GUI程序的时候,如果GUI库和STL都提供了某个功能(比如容器字符串),应该如何在两个库之间选择? 做法是分层,比如分为frontend+core.开发core的时候只用STL,保 ...