Can we get a FileStream for a Printer?
If I understand it right, a printer is also a file fro开发者_如何学Pythonm the operating system's view. Can I get a .NET C# FileStream type for a printer? It seems wild to do this. Thanks in advance.
A printer is modeled in Win32 as a set of bitmaps. This is wrapped by the PrintDocument
, which basically sets up a callback that lets you draw each page individually and sends them one by one to the printing spooler which then sends the data (in .ps format usually) to the printer.
This is how modern printers operate, they draw pages, not text. Only the old school matrix printers drew text character by character, in a stream-like fashion. They used to use the "PRN:" special file. This has lost all meaning in a modern environment however.
The good news is, it's really easy to work with the PrintDocument
class, you get a Graphics
object for each page that you can use to print text anywhere on the page, as well as draw graphics.
You can simulate a stream-like printer if you so wish by caching all the data then just calling DrawText
for every page.
Building a Windows Forms
application, you can think of a printer as a Graphics Device.
The System.Drawing.Printing
namespace provides print-related services for Windows Forms applications.
If you want to print from a Windows Presentation Foundation (WPF)
application, see the System.Printing
namespace. It provides classes that enable you to automate the management of print servers, print queues, and print jobs.
Assuming that you want to send the raw data to printer, please see How to send raw data to a printer by using Visual C# .NET
Since .Net 3.0 you can, by using the PrintSystemJobInfo.JobStream
When you create a printjob in the spooler, e.g.
var job = LocalPrinter.GetDefaultQueue().AddJob();
You can write to the
job.JobStream
Closing that stream queues it for printing. Interestingly, you can use that stream to write control codes and text to a generic/text-only printer (e.g. LPT1 parallel port impact printer)
精彩评论