using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PrintImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//打印字符串用
SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
Font DrawFont = new Font("Arial", 22);
PrintDocument pd = new PrintDocument();
pd.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件
//pd.PrinterSettings.PrinterName = "HP LaserJet Professional M1213nf MFP"; //打印机选择,使用默认打印机不需要设置
pd.Print();
}
private void PicturePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打开文件
FileStream fs = File.OpenRead("test.jpg"); //OpenRead
//转换为BYTE
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] imageByte = new Byte[filelength]; //建立一个字节数组
fs.Read(imageByte, 0, filelength); //按字节流读取
//转换为 IMAGE
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
fs.Close();
e.Graphics.DrawImage(image, 0, 0); //img大小
//e.Graphics.DrawString(TicCode, DrawFont, brush, 600, 600); //绘制字符串
e.HasMorePages = false;
}
}
}
C#打印图片文件
最新推荐文章于 2024-08-08 11:24:16 发布