Code Bye

wpf打印bitmap

程序里创建了一个bitmap图像,该怎么样去打印它呢?
解决方案

20

来了,收分,LZ太好了,居然要打6个字

20

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace winformtest1
{
    public partial class FormPrint : System.Windows.Forms.Form
    {
        private Font printFont;
        private StreamReader streamToPrint;
        public FormPrint()
        {
            // The Windows Forms Designer requires the following call.
            InitializeComponent();
        }
        // The Click event is raised when the user clicks the Print button.
        private void printButton_Click(object sender, EventArgs e)
        {
            try
            {
                streamToPrint = new StreamReader ("G:\Photos\赵丽颖-碧瑶.bmp");
                try
                {
                    printFont = new Font("Arial", 10);
                    //打印预览
                    PrintPreviewDialog ppd = new PrintPreviewDialog();
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler
                       (this.pd_PrintPage);
                    ppd.Document = pd;
                    ppd.ShowDialog();
                    pd.Print();
                }
                finally
                {
                    streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // The PrintPage event is raised for each page to be printed.
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Image img = Image.FromFile("G:\Photos\赵丽颖-碧瑶.jpg");
            ev.Graphics.DrawImage(img, new Point(10, 10));
            ev.HasMorePages = false;
        }
    }
}

Image img = Image.FromFile(“G:\Photos\赵丽颖-碧瑶.jpg”);
ev.Graphics.DrawImage(img, new Point(10, 10));
其实就是这个,让Graphics绘图句柄画图像:DrawImage


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明wpf打印bitmap