C# 背景图片带边框

C# 背景图片带边框,c#,bitmap,background,margin,zedgraph,C#,Bitmap,Background,Margin,Zedgraph,我正在制作一个基本上是位图的音频频谱图。要显示轴(图形),我使用ZedGraph,它基本上只是显示轴,如前所述。一种辅助控件。然后我在上面显示位图。 当窗口是默认大小时,这很好——但是,当我最大化它时,我会松开比例(见下图)。使用锚定很难(不可能?)做到这一点 现在我想知道其他的选择,这将保持适当的定位。是否可以为控件(在我的例子中是ZedGraphControl)设置背景图像并设置其边距?如果不是,我最好的选择是什么 默认窗口大小(一切正常): 最大化窗口(位图未填充ZedGraph控件):

我正在制作一个基本上是位图的音频频谱图。要显示轴(图形),我使用ZedGraph,它基本上只是显示轴,如前所述。一种辅助控件。然后我在上面显示位图。 当窗口是默认大小时,这很好——但是,当我最大化它时,我会松开比例(见下图)。使用锚定很难(不可能?)做到这一点

现在我想知道其他的选择,这将保持适当的定位。是否可以为控件(在我的例子中是ZedGraphControl)设置背景图像并设置其边距?如果不是,我最好的选择是什么

默认窗口大小(一切正常):

最大化窗口(位图未填充ZedGraph控件):
我认为有两种解决方案。第一种方法是自己将波形位图绘制到控件上(这样可以使用全部可用空间,但会稍微拉伸位图)。第二种方法是围绕控件构建一些面板,并相应地调整它们的大小(使图形始终以正确的纵横比显示,但浪费屏幕空间,而且更复杂)

1我不知道zedgraph是如何工作的,但我提出了以下建议。 从您正在编写的内容来看,它是一个用户控件。我要做的是听它的onPaint方法。 您可以自由使用图形对象在控件上绘制任何内容(包括位图)。参照控件的大小,可以轻松绘制具有相应纵横比的位图

2 创建一个容器来容纳图形控件,并添加四个面板,一个用于顶部、底部、左侧和右侧。如图所示:

现在,您可以根据所需的纵横比调整这些尺寸。为此,您必须侦听两个事件,ResizeEnd事件(每当用户完成控件大小调整时调用)和一个要侦听窗体是否最大化的事件()。 需要执行的代码如下所示:

        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;
    }
当然,您必须将“DesiredSpectration”的值替换为波形图像的纵横比。
如果您需要更多帮助,只需向我发送带有您电子邮件地址的私人消息,我将向您发送完整的Visual Studio解决方案。

首先,感谢您的帮助!这些天我的工作量非常大,我找不到时间来解决我写的问题。我将尽快审查您的解决方案,然后接受答案。