Stampare una form o un controllo

image image

 

Nel Framework 2.0 fu introdotto il metodo DrawToBitmap in ogni controllo visuale standard. Questo metodo permette la renderizzazione del controllo in un'immagine bitmap.

Il metodo è utile per effettuare rapidamente degli scereenshot e per stampare (a bassa risoluzione) il controllo desiderato.

 

    ' VB.
    Private Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveImage.Click
        Try
            GetBitmap.Save("C:\DotNetWork_DrawToBitmap.bmp")
            MessageBox.Show("L'immagine è stata salvata correttamente.", "DrawToBitmap", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Function GetBitmap() As Bitmap
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        Dim bmp As New Bitmap(rect.Size.Width, rect.Size.Width)
        Me.DrawToBitmap(bmp, rect)
        Return bmp
    End Function

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        ' Crea una nuova form.
        Dim f As New Form
        f.Size = New Size(600, 500)
        f.Text = "Anteprima di stampa"
        f.StartPosition = FormStartPosition.CenterScreen

        ' Crea un PrintDocument.
        Dim pd As New Printing.PrintDocument
        AddHandler pd.PrintPage, AddressOf PrintPage

        ' Crea un PrintPreviewControl.
        Dim prew As New PrintPreviewControl
        prew.Dock = DockStyle.Fill
        prew.Document = pd
        prew.AutoZoom = False
        f.Controls.Add(prew)

        f.ShowDialog()

        ' Stampa l'immagine.
        pd.Print()
    End Sub

    Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        e.Graphics.DrawImage(GetBitmap, 0, 0)
    End Sub

 

// C#. 
private void btnSaveImage_Click(object sender, System.EventArgs e) 
{ 
    try { 
        GetBitmap.Save("C:\\DotNetWork_DrawToBitmap.bmp"); 
        MessageBox.Show("L'immagine è stata salvata correttamente.", "DrawToBitmap", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 
    catch (Exception ex) { 
        MessageBox.Show(ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 

public Bitmap GetBitmap() 
{ 
    Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); 
    Bitmap bmp = new Bitmap(rect.Size.Width, rect.Size.Width); 
    this.DrawToBitmap(bmp, rect); 
    return bmp; 
} 

private void btnPrint_Click(object sender, System.EventArgs e) 
{ 
    // Crea una nuova form. 
    Form f = new Form(); 
    f.Size = new Size(600, 500); 
    f.Text = "Anteprima di stampa"; 
    f.StartPosition = FormStartPosition.CenterScreen; 
    
    // Crea un PrintDocument. 
    Printing.PrintDocument pd = new Printing.PrintDocument(); 
    pd.PrintPage += PrintPage; 
    
    // Crea un PrintPreviewControl. 
    PrintPreviewControl prew = new PrintPreviewControl(); 
    prew.Dock = DockStyle.Fill; 
    prew.Document = pd; 
    prew.AutoZoom = false; 
    f.Controls.Add(prew); 
    
    f.ShowDialog(); 
    
    // Stampa l'immagine. 
    pd.Print(); 
} 

public void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
{ 
    e.Graphics.DrawImage(GetBitmap, 0, 0); 
} 

 

Potrebbe essere un metodo molto utile... se non fosse vincolato alla risoluzione dello schermo.

 

Tag Cloud:

Print | posted on lunedì 15 settembre 2008 11.28

Feedback

# re: Stampare una form o un controllo

Left by Mattew at 12/07/2011 9.51
Gravatar veramente ottimo articolo,c'é un errore dove viene creato il bitmap,infatti l'immagine risulterà quadrata
[Bitmap bmp = new Bitmap(rect.Size.Width, rect.Size.Width); ].In ogni caso basterà sostituire i primo width con l'height,per il resto veramente utile!

Your comment:





 
Please add 5 and 7 and type the answer here:

Copyright © Andrea Zingoni

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski