0

I'd like to have a way to export the graphical contents of a FlowLayoutPanel to a file (don't mind what format, bmp is probably the easiest). I'd also like it to scroll the contents so that the exported file contains the entire contents of the Panel.

Is there any way to do this? I'm using C# WinForms and Framework 4.

vbtrek
  • 21
  • 4

2 Answers2

1

Try looking into xml serialization

You could serialize the panel and save the xml. and load the xml and deserialize it back into the panel

Also check this out.

To save as an image you just do this:

Bitmap image = new Bitmap(flowLayoutPanel1.Width, flowLayoutPanel1.Height);
flowLayoutPanel1.DrawToBitmap(image, new Rectangle(0, 0, flowLayoutPanel1.Width, flowLayoutPanel1.Height));
image.Save("SAVE PATH");
Community
  • 1
  • 1
string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • Thanks for your answer, that would work perfectly. I think I asked the question wrongly, I should have said I want to export the graphical presentation not the data (sorry!). I'll edit the op. – vbtrek Apr 04 '13 at 11:10
  • so you want to know how to print the panel into a image? – string.Empty Apr 04 '13 at 18:29
  • Yes that would be perfect print or save the panel – vbtrek Apr 05 '13 at 07:27
  • That works, but my flowlayout panel has a vertical scrollbar because there is more than fits on the screen. The saved image only saves what is shown on the screen not the entire contents of the flowlayout. – vbtrek Apr 05 '13 at 15:28
  • you would have to create a dummy form and add a clone of the flowlayoutpanel and take the image from the clone. – string.Empty Apr 05 '13 at 15:31
0

The trick is to temporaly set the flowLayoutPanel as large as to fit all the controls within, even if it gets too large for the visible screen, then do the DrawToBitmap using the flowLayoutPanel.clientRectangle area, not the .Width and .Height.

In my example, Outside_Splitter is docked to the form, with two panels, and fraAction is a groupbox and is the last control on the panel, that is vertically scrolled.

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