Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#在选项卡上绘制饼图_C#_Charts_Tabs_Pie Chart_Graphic - Fatal编程技术网

c#在选项卡上绘制饼图

c#在选项卡上绘制饼图,c#,charts,tabs,pie-chart,graphic,C#,Charts,Tabs,Pie Chart,Graphic,非常简单的问题,不幸的是,在C#中从未使用过绘图控件等,所以我不知道如何看待这个问题。 好的,我正在从一堆文本框中绘制一个piechart,用于输入,并在按钮事件上运行绘制。 我需要在我的一个选项卡上绘制图表,而不是从背景中绘制。我如何设置它? 这是我的密码: private void tempButton_Click(object sender, EventArgs e) { Rectangle tabArea; RectangleF tab

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

      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;

        }
    }
正如你所看到的,当我点击测试按钮时,它会绘制图表,但在我的选项卡区域后面,我需要它绘制到我的一个选项卡上(我感觉这是超级简单的1行解决方案,但谷歌不是我的朋友)。
非常感谢

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

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

    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”,以将图片框拉伸到位图尺寸。

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

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

    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”,以将图片框拉伸到位图尺寸。

我真的不知道,但我会尝试将图片框放在选项卡控件上。。。从图片框中获取图形,并在其上绘图。我真的不知道,但我会尝试在选项卡控件上放置一个图片框。。。从图片框中获取图形并在其上绘图。非常感谢您的回复,我尝试了创建位图并向其中添加图形和绘图。不确定如何将位图设置为picturebox的源?我会更新我的代码::更新::啊,我想pictureBox3.BackgroundImage=B;我希望我的解决方案能尽快发布(不,
pictureBox3.Image=B是正确的代码。否则,自动调整大小将无法工作。似乎存在问题。我已在选项卡上显示了图形(非常感谢!0,但由于某些原因,没有颜色,即使图片框的高度和宽度相同,图像也会被水平挤压。图片框和手动设置图形是否有问题?(yeh改为image)但同样的问题是,在你发布的代码中没有颜色etcLook:你没有使用位图的尺寸,而是
GetTabRect(0)
。将其更改为
B.Size
。此外,
位图的构造函数也有一些重载-有一个重载采用了高度和
像素格式
。颜色和alpha值使用像素格式(Format32bppArgb)。非常感谢您的回复,我尝试过创建位图并向其中添加图形和绘图。不确定如何将位图设置为picturebox的源?我将更新我的代码::更新::啊,我认为pictureBox3.BackgroundImage=B;应该可以。我会尽快发布我的解决方案(我希望)否,
pictureBox3.Image=B;
是正确的代码。否则,自动调整大小将不起作用。似乎有问题。我已将图形显示在选项卡上(非常感谢!0,但由于某些原因,图片框没有颜色,即使图片框的高度和宽度相同,图像也会水平挤压。图片框和手动设置图形是否有问题?(yeh改为图像)但同样的问题是,在你发布的代码中没有颜色etcLook:你没有使用位图的尺寸,而是
GetTabRect(0)
。将其更改为
B.Size
。此外,
位图的构造函数也有一些重载-有一个重载采用了高度和
像素格式
。颜色和alpha值使用像素格式(Format32bppArgb)。