DrawToBitmap:暂时缩放控件

时间:2014-12-07 10:13:21

标签: vb.net winforms controls drawtobitmap

我正在使用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属性来改变表单的停靠顺序。

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

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

0 个答案:

没有答案