C# 文件直接打印功能

部署运行你感兴趣的模型镜像
using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace Common
{
    /// <summary>
    /// 文件打印方法
    /// </summary>
    public class PrintService
    {
        public PrintService()
        {
            //将事件处理函数添加到PrintDocument的PrintPage中
            this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
        }

        // Declare the PrintDocument object.
        private string streamType;
        private Stream streamToPrint;
        private PrintDocument docToPrint = new PrintDocument();//创建一个PrintDocument的实例

        // This method will set properties on the PrintDialog object and
        // then display the dialog.
        public void StartPrint(Stream streamToPrint, string streamType)
        {
            this.streamToPrint = streamToPrint;
            this.streamType = streamType;
            PrintDialog printDialog = new PrintDialog();//创建一个PrintDialog的实例
            printDialog.AllowSomePages = true;
            printDialog.ShowHelp = true;
            // Set the Document property to the PrintDocument for
            // which the PrintPage Event has been handled. To display the
            // dialog, either this property or the PrinterSettings property
            // must be set
            printDialog.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
            DialogResult result = printDialog.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();//开始打印
            }
        }

        // The PrintDialog will print the document
        // by handling the document’s PrintPage event.
        private void docToPrint_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
        {
            // Insert code to render the page here.
            // This code will be called when the control is drawn.
            // The following code will render a simple
            // message on the printed document
            switch (this.streamType)
            {
                case "txt":
                    string text = null;
                    System.Drawing.Font printFont = new System.Drawing.Font
                    ("Arial", 35, System.Drawing.FontStyle.Regular);
                    // Draw the content.
                    System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
                    text = streamReader.ReadToEnd();
                    e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
                    break;
                case "image":
                    System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
                    int x = e.MarginBounds.X;
                    int y = e.MarginBounds.Y;
                    int width = image.Width;
                    int height = image.Height;
                    if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
                    {
                        width = e.MarginBounds.Width;
                        height = image.Height * e.MarginBounds.Width / image.Width;
                    }
                    else
                    {
                        height = e.MarginBounds.Height;
                        width = image.Width * e.MarginBounds.Height / image.Height;
                    }
                    System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
                    e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
                    break;
                default:
                    break;
            }
        }
    }
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值