C# 制作个性签名

来源:互联网 发布:怎么安装软件,文件 编辑:程序博客网 时间:2024/05/01 09:02

想必看到这个标题,大致内容已经很清楚了。

现在说说具体实现:

首先有一个Plist<Point>,来保存鼠标的轨迹。紧接着用Graphic类进行绘图,Bitmap进行保存。

这里面涉及的问题是在绘画过程中,窗体出现闪烁,而用           

this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint,
                           true);   //开启双缓冲

 this.UpdateStyles();

这俩句就可以解决问题。 这点希望高手给予解释。

 

下面贴下代码:

 

namespace DrawBMP
{
    public partial class Form1 : Form
    {
       private bool initial = true, startDraw; 
        List<Point> pList = new List<Point>();
        Bitmap bmp = new Bitmap(300, 300); //保存图像

        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint,
                           true);   //开启双缓冲
            this.UpdateStyles();

 

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
             if (e.Button == MouseButtons.Left && startDraw) 
                { 
                    pList.Add(e.Location); 
                    this.Refresh(); 
                } 

        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
             if (e.Button == MouseButtons.Left) 
             { 
                 startDraw = false; 
             }
             pList.Clear();

 


             bmp.Save("C:\\yue.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(this.Width,this.Height);  //重新建一个图片
           
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(System.Drawing.Color.White);  //清除背景
            g.Dispose();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            if (initial) //窗体刚加载时不重绘  
            {
                return;
            }


            Graphics g;

            g = Graphics.FromImage(bmp);
           

            //g.Clear(System.Drawing.Color.White);  //清除背景

            float tension = 0.00F;


            using (Pen p = new Pen(Color.Red, 3))
            {
                //g.SmoothingMode=
                //g.DrawClosedCurve(p,pList.ToArray());
                g.DrawCurve(p, pList.ToArray(), tension); //画平滑曲线 
            }
            g.Dispose();
            this.BackgroundImage = bmp;


        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            initial = false;  //.判断窗体是否第一次加载
            startDraw = true;
            pList.Add(e.Location);
           
        }

 

 

    }
}

原创粉丝点击