c#chartarea背像尺寸调整

问题描述

我试图在Windows窗体应用程序中放置图表区域的背景图像。 我成功放置了背面图像,但我希望图表面积与背面图像的大小完全相同。 我尝试使用内部绘图位置来调整图表区域的大小,我想我设法正确地调整了图表区域的大小以使其等于图像大小,但是我仍然遇到问题:背面图像略有偏移(请参见附件{{3 }}。我希望我的背像像(1)。 有人可以帮助我正确调整图表区域和图像的尺寸吗?请注意,该图像将用于绘制图表点,图像的宽度对应于x = [0.0005-0.2],高度对应于y = [0.0005-0.05]。 这是我的代码

        ChartArea ca = Chart1.ChartAreas[0];
        ElementPosition cap = ca.Position;
        ElementPosition ipp = ca.InnerPlotPosition;
        // chartarea pixel size:
        Size CaSize = new Size((int)(cap.Width * Chart1.ClientSize.Width / 100f),(int)(cap.Height * Chart1.ClientSize.Height / 100f));

        // InnerPlotArea pixel size:
        Size IppSize = new Size((int)(ipp.Width * CaSize.Width / 100f),(int)(ipp.Height * CaSize.Height / 100f));
        //PrevIoUs lines are used to get the size of the inner plot in pixels to adjust my image size.
        Chart1.ChartAreas[0].BackImage = "MyImage.png";
        //Chart1.Series["Series1"].Points.AddXY(0.1,0.02);
        Chart1.Series["Series1"].Color = Color.Red;
        Chart1.ChartAreas[0].AxisX.MajorGrid.linewidth = 0;
        Chart1.ChartAreas[0].AxisY.MajorGrid.linewidth = 0;
        Chart1.ChartAreas[0].AxisX.Maximum = 0.2;
        Chart1.ChartAreas[0].AxisX.Minimum = 0.00005;
        //Chart1.ChartAreas[0].AxisX.Title = "h/gT^2";
        Chart1.ChartAreas[0].AxisY.Minimum = 0.00005;
        Chart1e.ChartAreas[0].AxisY.Maximum = 0.05;
        //Chart1.ChartAreas[0].AxisY.Title = "H/gT^2";

预先感谢

Mohamad Hmedi。

解决方法

通过添加BackImageWrapMode来解决:

Chart1.ChartAreas[0].BackImageWrapMode = ChartImageWrapMode.Scaled;

通过使用自动缩放模式,背景图像将被拉伸以适合图表区域的边界。