FlowLayoutPanel导出

时间:2013-03-25 10:30:03

标签: winforms c#-4.0 flowlayoutpanel

我想有办法将FlowLayoutPanel的图形内容导出到文件中(不介意什么格式,bmp可能是最简单的)。我也希望它滚动内容,以便导出的文件包含Panel的全部内容。

有没有办法做到这一点?我正在使用C#WinForms和Framework 4。

2 个答案:

答案 0 :(得分:1)

尝试查看xml serialization

您可以序列化面板并保存xml。并加载xml并将其反序列化回面板

同时检查this

要保存为图像,您只需执行以下操作:

Bitmap image = new Bitmap(flowLayoutPanel1.Width, flowLayoutPanel1.Height);
flowLayoutPanel1.DrawToBitmap(image, new Rectangle(0, 0, flowLayoutPanel1.Width, flowLayoutPanel1.Height));
image.Save("SAVE PATH");

答案 1 :(得分:0)

技巧是临时设置flowLayoutPanel使其适合所有控件,即使它对于可见屏幕而言太大,然后使用flowLayoutPanel.clientRectangle区域而不是.Width和.Height来执行DrawToBitmap。

在我的示例中,Outside_Splitter停靠在具有两个面板的表单上,而fraAction是一个组框,并且是面板上的最后一个控件,可以垂直滚动。

Public Sub Print_Panel()


    Dim newHeight As Integer
    Dim pos As Point, oheight As Integer, owidth As Integer, xDock As DockStyle
    With Outside_Splitter ' This contains the two panel ...
        pos.X = .Left ' Store original position and size
        pos.Y = .Top
        oheight = .Height
        owidth = .Width
        xDock = .Dock ' get original dock set
        newHeight = FraAction.Top + FraAction.Height + 30 ' calculate new height based on position and size of the last control
        .Dock = DockStyle.None ' undock it
        .Height = newHeight ' set new height 
        .Refresh()
        .SetBounds(pos.X, pos.Y, owidth, newHeight) ' Set position and size, temporarily
        .Refresh()
    End With

    'Create Bitmap based on panel.ClientRectangle   
    Dim myBmp As New Bitmap(Painel_Detalhe_NC.ClientRectangle.Width, Painel_Detalhe_NC.ClientRectangle.Height) 

    'Paint the bitmap
    Painel_Detalhe_NC.DrawToBitmap(myBmp, Painel_Detalhe_NC.ClientRectangle) 

    'Create pdf
    Dim _pdf As New C1.C1Pdf.C1PdfDocument

    _pdf.Clear()
    _pdf.Landscape = False
    _pdf.PaperKind = PaperKind.A4


    Dim rec As New RectangleF ' Set 5% margin around the page 
    rec = _pdf.PageRectangle
    rec.X = 0.05 * rec.Width
    rec.Y = 0.05 * rec.Height
    rec.Width = 0.9 * _pdf.PageRectangle.Width
    rec.Height = 0.9 * _pdf.PageRectangle.Height


    _pdf.DrawImage(myBmp, rec) ' paint/resize bitmap to that size on the pdf

    'Save it and show it 
    _pdf.Save(My.Computer.FileSystem.SpecialDirectories.Temp & "\temp.pdf")
    Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\temp.pdf")

    myBmp.Dispose() ' Clear it

    With Outside_Splitter ' put it back to where it was
        .Left = pos.X
        .Top = pos.Y
        .Dock = xDock ' Back to filling the form
        .Refresh()

    End With
    End  Sub