Print BMP image : Print PrintDocument « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / CSharp Tutorial » GUI Windows Forms » Print PrintDocument 
23.51.2.Print BMP image
Print BMP image
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

public class PrintDialogPrint : Form
{
    public PrintDialogPrint()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        this.cmdPrint.Location = new System.Drawing.Point(109122);
        this.cmdPrint.Size = new System.Drawing.Size(7523);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(272260);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Simple Print";
        this.ResumeLayout(false);
    }

    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDocument doc = new PrintDocument();
        doc.PrintPage += this.Doc_PrintPage;

        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;

        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }

    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Font font = new Font("Arial"30);
        
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;

        float lineHeight = font.GetHeight(e.Graphics);

        for (int i = 0; i < 5; i++)
        {
            e.Graphics.DrawString("This is line " + i.ToString(),font, Brushes.Black, x, y);
            y += lineHeight;
        }
        y += lineHeight;

        e.Graphics.DrawImage(Image.FromFile("c:\\YourFile.bmp"), x, y);
        
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintDialogPrint());
    }
    private System.Windows.Forms.Button cmdPrint;
    
}
23.51.Print PrintDocument
23.51.1.Basic Printing
23.51.2.Print BMP imagePrint BMP image
23.51.3.Multi Page Print
23.51.4.Subclass PrintDocument
23.51.5.Print a paragraph
23.51.6.The print preview application.
23.51.7.Print With Margins
23.51.8.Print PageSettings Metrics
23.51.9.PrintDocument: DocumentName, PrintPage, Print
23.51.10.The print process application.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.