控件样式:
先识别本机打印机:
//显示打印设备
List<KeyValuePair<string, string>> PrintS = new List<KeyValuePair<string, string>>();
int index = 0;
foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
{
PrintS.Add(new KeyValuePair<string, string>(index.ToString(), sPrint));
index++;
}
ucCombox1.Source = PrintS;
ucCombox1.SelectedIndex = 0;
打印预览部分写在了set里:
private Bitmap ownBitmap;
public Bitmap bitmap
{
get
{
return ownBitmap;
}
set
{
ownBitmap = value;
pictureBox1.Image = value;
}
}
显示完毕,点击按钮打印:
先在构造函数里注册一个打印之前的方法,处理图像:
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
docToPrint是定义的一个打印文档:
private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
printPage事件:
private void docToPrint_PrintPage(object o, System.Drawing.Printing.PrintPageEventArgs e)
{
Rectangle rectangle = new Rectangle(0, 0, ownBitmap.Width, ownBitmap.Height);
e.Graphics.DrawImage(ownBitmap, rectangle);
}
打印的时候直接调:
docToPrint.Print();
对打印设备的识别:
引用:
using System.Management
获取:
string path = @"win32_printer.DeviceId='" + ucCombox1.SelectedText + "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
获取属性:
printer["WorkOffline"].ToString()
遍历所有的属性:
foreach (var property in temp.Properties)
{
sw.WriteLine(property.Name + ":" + property.Value);
}