注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

Graphics Programming with GDI+ 学习笔记(三)  

2011-10-25 19:22:43|  分类: C# & .NET |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 Graphics Programming with GDI+ 学习笔记(一) - Castor - 趁年轻,多折腾

第三章   Graphics对象(1)

Graphics对象是GDI+的核心。

这一章介绍了一个示例程序,用于阐述Graphics类的使用。

示例名称为GDI+ Painter,我大概看懂了,然后是自己的版本,和原来的差不错,不过没有实现拖拉功能。直接上源代码:

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

namespace LearnWindows

{

    public partial class Form1 : Form

    {

        private Point[] ps;

        Graphics gr;

        Bitmap bmp;

        Pen p;

        SolidBrush sb;

        private bool IsFill = false;

        //false 绘图

        //true 填充

        private int count=0;//鼠标点击次数

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);

            ps = new Point[4];

            Rectangle rect = this.ClientRectangle;

            bmp = new Bitmap(rect.Width, rect.Height, this.CreateGraphics());

            gr = Graphics.FromImage(bmp);

            gr.Clear(this.BackColor);

            toolStripComboBox1.SelectedIndex = 0;

            p = new Pen(Color.Blue, 3);

            sb = new SolidBrush(Color.Blue);

            toolStripButton2.BackColor = Color.Blue;

            IsFill=false;

            count = 0;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

 

        }

 

        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            if (IsFill)//填充

            {

                switch (toolStripComboBox1.SelectedIndex)

                {

                    case 0://圆形

                        {

                            if (count != 1)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[1] = new Point(e.X, e.Y);

                                int r = (int)(Distance(ps[0], ps[1]));

                                Rectangle rect = new Rectangle(ps[0].X - r, ps[0].Y - r, 2 * r, 2 * r);

                                gr.FillEllipse(sb, rect);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

                    case 1://三角形

                        {

                            if (count != 2)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[2] = new Point(e.X, e.Y);

                                ps[3] = ps[0];

                                gr.FillPolygon(sb, new Point[] { ps[0], ps[1], ps[2], ps[3] });

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

                    case 2://矩形

                        {

                            if (count != 1)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[1] = new Point(e.X, e.Y);

                                Rectangle rect;

                                if (ps[0].X < ps[1].X || ps[0].Y < ps[1].Y)

                                    rect = new Rectangle(ps[0].X, ps[0].Y, (int)Math.Abs(ps[0].X - ps[1].X), (int)Math.Abs(ps[0].Y - ps[1].Y));

                                else

                                    rect = new Rectangle(ps[1].X, ps[1].Y, (int)Math.Abs(ps[0].X - ps[1].X), (int)Math.Abs(ps[0].Y - ps[1].Y));

                                gr.FillRectangle(sb, rect);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

                    default: return;

                }

            }

            else//绘图

            {

                switch (toolStripComboBox1.SelectedIndex)

                {

                    case 0://直线

                        {

                            if (count != 1)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[1] = new Point(e.X, e.Y);

                                gr.DrawLine(p, ps[0], ps[1]);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

                    case 1://贝塞尔曲线

                        {

                            if (count != 3)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[3] = new Point(e.X, e.Y);

                                gr.DrawBezier(p, ps[0], ps[1], ps[2], ps[3]);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

 

                    case 2://圆形

                        {

                            if (count != 1)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[1] = new Point(e.X, e.Y);

                                int r = (int)(Distance(ps[0], ps[1]));

                                Rectangle rect = new Rectangle(ps[0].X - r, ps[0].Y-r, 2 * r, 2 * r);

                                gr.DrawEllipse(p, rect);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

 

                    case 3://三角形

                        {

                            if (count != 2)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[2] = new Point(e.X, e.Y);

                                ps[3] = ps[0];

                                gr.DrawLines(p,new Point[]{ps[0],ps[1],ps[2],ps[3]});

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

 

                    case 4://矩形

                        {

                            if (count != 1)

                                ps[count++] = new Point(e.X, e.Y);

                            else

                            {

                                ps[1] = new Point(e.X, e.Y);

                                Rectangle rect;

                                if((ps[0].X<ps[1].X)||ps[0].Y<ps[1].Y)

                                    rect = new Rectangle(ps[0].X, ps[0].Y,(int)Math.Abs(ps[0].X - ps[1].X), (int)Math.Abs(ps[0].Y - ps[1].Y));

                                else

                                    rect = new Rectangle(ps[1].X, ps[1].Y, (int)Math.Abs(ps[0].X - ps[1].X), (int)Math.Abs(ps[0].Y - ps[1].Y));

                                gr.DrawRectangle(p, rect);

                                count = 0;

                                RefreshFormBackground();

                            }

                            return;

                        }

                    default: return;

                }

            }

        }

 

        private void RefreshFormBackground()

        {

            Bitmap curBmp = bmp.Clone(new Rectangle(0,0,bmp.Width, bmp.Height), bmp.PixelFormat);

            this.BackgroundImage = curBmp;

        }

 

        private void toolStripButton1_Click(object sender, EventArgs e)

        {

            if (IsFill)//为填充,则切换为绘图

            {

                toolStripButton1.Image = Properties.Resources.pen_yellow;

                toolStripLabel1.Text = "绘图";

                toolStripComboBox1.Items.Insert(0,"贝塞尔曲线");

                toolStripComboBox1.Items.Insert(0, "直线");

                toolStripComboBox1.SelectedIndex = 0;

            }

            else//为绘图,则切换为填充

            {

                toolStripButton1.Image = Properties.Resources.pie_chart;

                toolStripLabel1.Text = "填充";

                toolStripComboBox1.Items.RemoveAt(0);

                toolStripComboBox1.Items.RemoveAt(0);

                toolStripComboBox1.SelectedIndex = 0;

            }

            IsFill = !IsFill;

        }

 

        private void toolStripButton2_Click(object sender, EventArgs e)

        {

            ColorDialog cd = new ColorDialog();

            if (cd.ShowDialog(this) == DialogResult.OK)

            {

                p = new Pen(cd.Color,3);

                sb = new SolidBrush(cd.Color);

                toolStripButton2.BackColor = cd.Color;

            }

        }

        private void toolStripButton3_Click(object sender, EventArgs e)

        {

            //保存

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "BMP文件(*.bmp)|*.bmp";

            if (sfd.ShowDialog(this) == DialogResult.OK)

                bmp.Save(sfd.FileName,System.Drawing.Imaging.ImageFormat.Bmp);          

        }

 

        private void toolStripButton4_Click(object sender, EventArgs e)

        {

            gr.Clear(this.BackColor);

        }

 

        private double Distance(Point p1, Point p2)

        {

            return System.Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));

        }

    }

}

运行效果如下:

Graphics Programming with GDI+ 学习笔记(三) - Castor - 趁年轻,多折腾

新建,再画一张: 

Graphics Programming with GDI+ 学习笔记(三) - Castor - 趁年轻,多折腾

画一个,然后保存: 

Graphics Programming with GDI+ 学习笔记(三) - Castor - 趁年轻,多折腾

 注意事件的绑定都是用代码自动生成的。

  评论这张
 
阅读(1105)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016