码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm画图

时间:2016-08-04 14:48:32      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

简单的画图

技术分享

关键代码

/// <summary>
        /// 生成图片
        /// </summary>
        /// <param name="imgWidth">图片的宽</param>
        /// <param name="imgHeight">图片的高</param>
        /// <param name="leftPercent">左图占百分比</param>
        /// <param name="rightPercent">右图占百分比</param>
        /// <param name="percent">图片文本</param>
        /// <returns></returns>
        private Image DrawMultyProcessBar(int imgWidth, int imgHeight, float leftPercent, float rightPercent, float percent)
        {
            if (imgWidth <= 0 || imgHeight <= 0)
                throw new Exception("参数imgWidth必须大于0,参数imgHeight必须大于0");

            if(leftPercent>1 || rightPercent>1)
                throw new Exception("参数leftPercent必须小于1,参数rightPercent必须小于1");

            if (leftPercent + rightPercent != 1.0)
                throw new Exception("参数leftPercent+rightPercent必须等于1");

            string str = percent.ToString() + "%";
            Bitmap bmp = new Bitmap(imgWidth, imgHeight);
            Graphics grp = Graphics.FromImage(bmp);

            grp.FillRectangle(SystemBrushes.HotTrack, new Rectangle(0, 0, (int)(leftPercent * imgWidth), imgHeight));
            grp.FillRectangle(SystemBrushes.Highlight, new Rectangle((int)(leftPercent * imgWidth), 0, (int)(rightPercent * imgWidth), imgHeight));

            PointF point = new PointF(bmp.Width / 2 - str.Length * 9 / 2, bmp.Height / 2 - 9 / 2);
            grp.DrawString(str, new Font(new FontFamily("宋体"), 9), Brushes.White, point);
            grp.Dispose();

            return bmp;
        }

 调用部分关键代码

pictureBox1.Image = DrawMultyProcessBar(pictureBox1.Width, pictureBox1.Height, 0.5f, 0.5f, 50.0f);

 

WinForm画图

标签:

原文地址:http://www.cnblogs.com/jzblogs/p/5736544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!