1

非常简单的问题,不幸的是从未在 C# 中使用过绘图控件等,所以我不知道如何看待这个。好的,我从一堆文本框中绘制饼图用于输入并在按钮事件上运行绘制。我需要在我的一个选项卡上而不是从背景上绘制图表。我该如何设置?这是我的代码:

      private void tempButton_Click(object sender, EventArgs e)
    {
        Rectangle tabArea;
        RectangleF tabTextArea;

        Bitmap B = new Bitmap(500, 500);

        tabArea = tabControl1.GetTabRect(0);

        tabTextArea = (RectangleF)tabControl1.GetTabRect(0);

        using (Graphics g = Graphics.FromImage(B))
        {
            int i1 = int.Parse(textBox1.Text);
            int i2 = int.Parse(textBox2.Text);
            int i3 = int.Parse(textBox3.Text);
            int i4 = int.Parse(textBox4.Text);

            float total = i1 + i2 + i3 + i4;
            float deg1 = (i1 / total) * 360;
            float deg2 = (i2 / total) * 360;
            float deg3 = (i3 / total) * 360;
            float deg4 = (i4 / total) * 360;

            Font font = new Font("Arial", 10.0f);
            SolidBrush brush = new SolidBrush(Color.Red);
            Pen p = new Pen(Color.Black, 2);
            p.Width = 0.5f;

            tabArea = new Rectangle(textBox1.Location.X + textBox1.Size.Width + 250, 150, 500, 500);

            Brush b1 = new SolidBrush(Color.Gold);
            Brush b2 = new SolidBrush(Color.Silver);
            Brush b3 = new SolidBrush(Color.DarkOrange);
            Brush b4 = new SolidBrush(Color.Black);

            g.DrawRectangle(p, tabArea);

            g.DrawPie(p, tabTextArea, 0, deg1);
            g.FillPie(b1, tabArea, 0, deg1);
            g.DrawPie(p, tabTextArea, deg1, deg2);
            g.FillPie(b2, tabArea, deg1, deg2);
            g.DrawPie(p, tabTextArea, deg2 + deg1, deg3);
            g.FillPie(b3, tabArea, deg2 + deg1, deg3);
            g.DrawPie(p, tabTextArea, deg3 + deg2 + deg1, deg4);
            g.FillPie(b4, tabArea, deg3 + deg2 + deg1, deg4);

            //set picturebox3 as data source??
            pictureBox3.Image = B;

        }
    }

正如您所看到的,当我单击测试按钮时,它会绘制图表,但在我的选项卡区域后面,我需要将它绘制到我的一个选项卡上(我觉得这是超级简单的 1line 解决方案,但谷歌不是我的朋友 atm) . 提前谢谢了!

4

1 回答 1

1

最简单的解决方案是创建所需尺寸的Graphics位图,为此位图创建,进行绘图,然后将此位图设置为您放置在选项卡上的自动调整大小的图片框的图像源。这是最干净的方法。

更新
我在评论中指出您的绘图代码没有经过深思熟虑。如下更改第一行:

    Rectangle tabArea;
    RectangleF tabTextArea;

    Bitmap B = new Bitmap(500, 500, PixelFormat.Format32bppArgb);

    tabArea = new Rectangle(0, 0, B.Width, B.Height);
    tabTextArea = new RectangleF(0, 0, B.Width, B.Height);

另外:tabArea根据控制位置确定不是一个好主意。最后:将“SizeMode”属性设置为“AutoSize”以使图片框拉伸到位图尺寸。

于 2011-11-28T10:18:08.877 回答