带有边距的背景图片

3
我正在制作音频光谱图,它基本上是一张位图。为了显示坐标轴(图形),我在使用ZedGraph,它基本上只是用来显示坐标轴的辅助控件。然后我在上面显示位图。
当窗口大小为默认大小时,这样做效果很好 - 但是,当我将其最大化时,比例会丢失(如下图所示)。使用锚定来解决这个问题很难(或者不可能?)。
现在我想知道其他选项是否可以保持正确的定位。是否可以为控件(在我的情况下是ZedGraphControl)设置BackgroundImage并设置其边距?如果不能,那么我该怎么做?
默认窗口大小(一切正常): enter image description here 最大化的窗口(位图未填充ZedGraphControl): enter image description here
1个回答

1
我认为有两种解决方案。第一种是将波形位图绘制到控件上(因此使用全部可用空间,但会略微拉伸位图)。第二种是在您的控件周围构建一些面板并相应地调整其大小(使图形始终以正确的纵横比显示,但会浪费屏幕空间且更加复杂)。
1 我不知道zedgraph如何工作,但我提出以下建议。根据您的描述,它是一个用户控件。我会监听其onPaint方法。您将获得一个Graphics对象,可以自由使用它来在控件上绘制任何东西(包括位图)。参考控件的大小,您可以轻松地以相应的纵横比绘制位图。
2 创建一个容器来容纳您的图形控件,并添加四个面板,分别为顶部、底部、左侧和右侧。就像这张图片上的那样:

现在,您可以根据需要调整这些内容的纵横比。为此,您必须监听两个事件,即ResizeEnd事件(每当用户完成调整大小控件时调用)和一个事件来监听表单是否最大化(example)。 需要执行的代码如下:
        private void AdjustPanels(object sender, EventArgs e)
        {
        double desiredAspectRatio = 1;

        // I am using the form itself as a reference to the size and aspect ration of the application.
        // you can, of course, use any other control instead (e.g. a panel where you stuff all the other panels
        int width = this.Width;
        int height = this.Height;
        double formAspectRatio = (double)width / (double)height;

        int marginLeft=0, marginRight=0, marginTop=0, marginBottom=0;

        if (desiredAspectRatio > formAspectRatio)
        {
            // high aspect ratios mean a wider picture -> the picture we want is wider than what it currently is
            // so we will need a margin on top and bottom
            marginLeft = 0; marginRight = 0;
            marginTop = (int)((height - desiredAspectRatio * width) / 2);
            marginBottom = (int)((height - desiredAspectRatio * width) / 2);
        }
        else
        {
            marginTop = 0; marginBottom = 0;
            marginLeft = (int)((width - desiredAspectRatio*height)/2);
            marginRight = (int)((width - desiredAspectRatio * height) / 2);
        }

        pnlTop.Height = marginTop;
        pnlBottom.Height = marginBottom;
        pnlLeft.Width = marginLeft;
        pnlRight.Width = marginRight;
    }

当然,您需要用波形图像的宽高比替换“desiredAspectRatio”的值。 如果您需要进一步帮助,请向我发送私人消息并提供您的电子邮件地址,我将向您发送完整的Visual Studio解决方案。


首先,非常感谢您的帮助!最近我的工作量非常大,我没有时间解决我提出的问题。我会尽快查看您的解决方案,然后接受答案。 - c0dehunter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接