0

我正在使用 DrawToBitmap 生成 WinForms 控件的 snapshop,并为用户提供了生成不同大小的图像的选项。我调整控件大小的代码是

Private Function GetChartScaledImage(NewSize As Size) As Bitmap
    Dim bmp As New Bitmap(NewSize.Width, NewSize.Height) 'Create output bitmap
    With workingChart
        Dim oldsize As Size = .Size 'Save chart's old size
        Dim lastdock As DockStyle = .Dock 'Save chart's old docking style
        .SuspendLayout()
        .Dock = DockStyle.None 'Disable dock to allow resize
        .Size = NewSize 'Set the temporary size
        .DrawToBitmap(bmp, New Rectangle(0, 0, NewSize.Width, NewSize.Height)) 'Draw the image
        .Dock = lastdock 'Recreate the old behaviour
        .Size = oldsize 'Recreate the old size
        .ResumeLayout()
    End With
    Return bmp
End Function

大多数情况下,这可以正常工作,但存在控件的大小可以被其父容器覆盖的问题。我正在使用 TableLayoutPanels 可以防止控件被正确缩放。

我还没有遇到但可能发生的另一个问题是,可以通过临时重置.Dock属性来打乱表单的停靠顺序。

有没有其他方法可以缩放一个绘制事件的控件?

之后我可以缩放位图,但这会使图像模糊。创建具有相同属性的临时新控件也不是一种选择,因为该控件相当复杂。

4

0 回答 0