1

我有一个绘制一些形状的现有 OnPaint 方法:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        g.CompositingQuality = CompositingQuality.HighQuality;

        Brush greenBrush = new SolidBrush(Color.FromArgb(73,172,113));
        Brush blueBrush = new SolidBrush(Color.FromArgb(197,232,221));
        Brush whiteBrush = new SolidBrush(Color.White);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TextRenderingHint = TextRenderingHint.AntiAlias; 

        Rectangle newRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
            ClientRectangle.Width, ClientRectangle.Height);

        // Green background
        g.FillRectangle(greenBrush, newRect);

        // Blue ellipse
        int rectX = System.Convert.ToInt32(newRect.Width * .244) * -1;
        int rectY = System.Convert.ToInt32(newRect.Height * .025);
        int rectWidth = System.Convert.ToInt32(newRect.Width * 1.416);
        int rectHeight = System.Convert.ToInt32(newRect.Height * 1.685);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // Green ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .283) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .035);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.26);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.35);
        g.FillEllipse(greenBrush, rectX, rectY, rectWidth, rectHeight);

        // Blue ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .009) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .175);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.094);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.755);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // White ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .146);
        rectY = System.Convert.ToInt32(newRect.Height * .225);
        rectWidth = System.Convert.ToInt32(newRect.Width * .928);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.545);
        g.FillEllipse(whiteBrush, rectX, rectY, rectWidth, rectHeight);

}

我已将其转换为 - 在 WPF 中:

<Grid>
    <Viewbox HorizontalAlignment="Left" VerticalAlignment="Top"  >
        <Canvas Width="1680" Height="1050">
            <Rectangle    Fill="#FF49AC71" Width="1780" Height="1050" VerticalAlignment="Stretch" />
        <Ellipse  Fill="#FFC5E8DD"  Width="2379" Height="1770" Canvas.Left="-410" Canvas.Top="25"/>
            <Ellipse   Fill=  "#FF49AC71"  Width="2116" Height="1417" Canvas.Left="-475" Canvas.Top="37"/>
            <Ellipse    Fill="#FFC5E8DD"  Width="1837" Height="1842" Canvas.Left="-15" Canvas.Top="183"/>
            <Ellipse   Fill="White"  Width="1559" Height="1622" Canvas.Left="245" Canvas.Top="236"/>
        </Canvas>
    </Viewbox>

调整大小的行为是不一样的。有人可以告诉我如何解决这个问题,或者是一个更好的 WPF 示例!?

4

1 回答 1

3

您想将 ViewBox 的Stretch属性设置为 Fill。

<Viewbox Stretch="Fill">

默认值为 Uniform,它将保留纵横比。如果您想了解有关如何使用 Stretch 的各种值的更多详细信息,请参阅此博客条目。

于 2010-06-14T12:34:35.497 回答