c#画图实现立方体(压缩文件体积图)

来源:互联网 发布:远行星号舰船数据 编辑:程序博客网 时间:2024/05/21 06:59

c#winform绘制的立方体方法,时间关系没做成控件,大家在界面上拖一个picturebox即可调用方法即可


 //绘制立方体
        public void drawVolume(PictureBox picbox,double sc)  //sc立方体占用的体积
        {
            mybitmap = new Bitmap(picbox.Width, picbox.Height);//设定位图大小
            Graphics g = Graphics.FromImage(mybitmap);//从位图上获取画布
            g.Clear(Color.White);//用背景色刷新
            //pictureBox1填充为白色,便于显示图像
            Rectangle rect = new Rectangle(0, 0, picbox.Width, picbox.Height);
            g.FillRectangle(new SolidBrush(Color.WhiteSmoke), rect);
            Pen pen = new Pen(Color.White, 2);//画笔
            Pen penline = new Pen(Color.White, 1);//画笔
            SolidBrush sb = new SolidBrush(Color.FromArgb(92,0,128));//标注画刷 
            int htmp = 7;


            /*
             * 下面是立方体框架 对于立方体可不画因为会覆盖
           g.DrawLine(pen, 20, picbox.Height - 20, (picbox.Width - 5)/2, picbox.Height - 13);//绘制X斜下
           g.DrawLine(pen, (picbox.Width - 5) / 2, picbox.Height - 13, picbox.Width - 20, picbox.Height - 20);//绘制X斜上
           g.DrawLine(pen, 20, picbox.Height -20, 20, 20);
           g.DrawLine(pen, 20, 20, (picbox.Width - 5) / 2, 20 + htmp);
           //中间竖线
           g.DrawLine(pen, (picbox.Width - 5) / 2, 20 + htmp, (picbox.Width - 5) / 2, picbox.Height - 20+htmp);
           g.DrawLine(pen, (picbox.Width - 5) / 2, 20 + htmp, picbox.Width - 20, 20);
           g.DrawLine(pen, picbox.Width - 20, 20, picbox.Width - 20, picbox.Height - 20);
           //顶斜上
           g.DrawLine(pen, 20, 20, (picbox.Width - 5) / 2, 20 - htmp); 
           //顶斜下
           g.DrawLine(pen, picbox.Width - 20, 20, (picbox.Width - 5) / 2, 20 - htmp); //底斜上
           g.DrawLine(pen, 20, picbox.Height - 20, (picbox.Width - 5) / 2, picbox.Height - 20+htmp); //底部斜上
           //底部斜下
           g.DrawLine(pen, picbox.Width - 20, picbox.Height - 20, (picbox.Width - 5) / 2, picbox.Height - 20 - htmp); 
           g.DrawLine(pen, 20, picbox.Height - 20, (picbox.Width - 5) / 2, picbox.Height - 20 - htmp); //斜下
          */


            
            Brush top = new SolidBrush(Color.FromArgb(128, 128, 255));//顶部颜色
            Brush leftdown = new SolidBrush(Color.FromArgb(150, 60, 130));//左下颜色
            Brush rightdown = new SolidBrush(Color.FromArgb(192, 66, 140));//右下颜色
            Brush ns = new SolidBrush(Color.FromArgb(160, 96, 160)); //内部颜色 上矩形
            Brush nx = new SolidBrush(Color.FromArgb(140, 64, 122)); //内部颜色 下矩形
           
            sc = 0.5;//占用比例
            
            //下面是7个矩形从上至下
            //顶
            Point[] p = new Point[4];
            p[0] = new Point(20, 20);
            p[1] = new Point((picbox.Width - 5) / 2, 20 - htmp);
            p[2] = new Point(picbox.Width - 20, 20 );
            p[3] = new Point((picbox.Width - 5) / 2, 20 + htmp);
            g.FillPolygon(top, p);

            //前左上
            p[0] = new Point(20, 20);
            p[1] = new Point((picbox.Width - 5) / 2, 20 + htmp);
            p[2] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)) + htmp);
            p[3] = new Point(20, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
            g.FillPolygon(top, p);

            //前左下
            p[0] = new Point(20, 20 + Convert.ToInt32((picbox.Height-20-20)*(1-sc)));
            p[1] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc))+htmp);
            p[2] = new Point((picbox.Width - 5) / 2, picbox.Height - 20 + htmp);
            p[3] = new Point(20, picbox.Height - 20);
            g.FillPolygon(leftdown, p);

            //前右侧上
            p[0] = new Point((picbox.Width - 5) / 2, 20 +htmp);
            p[1] = new Point(picbox.Width - 20, 20);
            p[2] = new Point(picbox.Width - 20, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
            p[3] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)) + htmp);
            g.FillPolygon(top, p);

            //右侧下
            p[0] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)) + htmp);
            p[1] = new Point(picbox.Width - 20, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
            p[2] = new Point(picbox.Width - 20, picbox.Height - 20);
            p[3] = new Point((picbox.Width - 5) / 2, picbox.Height - 20+htmp);
            g.FillPolygon(rightdown, p);

            //内侧两个矩形 上
            p[0] = new Point(20, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
            p[1] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)) - htmp);
            p[2] = new Point(picbox.Width - 20,20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
            p[3] = new Point((picbox.Width - 5) / 2, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc))+htmp);
            g.FillPolygon(ns, p);
            //内矩形 下
            p[0] = new Point(20, picbox.Height - 20);
            p[1] = new Point((picbox.Width - 5) / 2, picbox.Height - 20-htmp);
            p[2] = new Point(picbox.Width - 20, picbox.Height - 20);
            p[3] = new Point((picbox.Width - 5) / 2, picbox.Height - 20 + htmp);
            g.FillPolygon(nx, p);


            //再来3条线
            g.DrawLine(penline, (picbox.Width - 5) / 2, 20 + htmp, (picbox.Width - 5) / 2, picbox.Height - 20 + htmp);

            g.DrawLine(penline, 20, 20, (picbox.Width - 5) / 2, 20 + htmp);

            g.DrawLine(penline, (picbox.Width - 5) / 2, 20 + htmp, picbox.Width - 20, 20);

            //最后再来一个标注
            g.DrawString((sc * 100).ToString() + "%", new Font("宋体", 9), sb, picbox.Width - 20, 20 + Convert.ToInt32((picbox.Height - 20 - 20) * (1 - sc)));
           
            picbox.Image = mybitmap;
        
        }