简单的画图
关键代码
/// <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);