Does Mono Support System.Drawing and System.Drawing.Printing

Does Mono support System.Drawing and System.Drawing.Printing?

From the Mono docs, I think yes:

Managed.Windows.Forms (aka
System.Windows.Forms): A complete and
cross platform, System.Drawing based
Winforms implementation.

It also useful if you run the Mono Migration Analyzer first.

How to print plain text using Monodevelop and C#?

The code on this page excellently explains how this can be done:
http://www.mail-archive.com/gtk-sharp-list@lists.ximian.com/msg03762.html

How To Print Image with GTKSharp

With a little manipulation of Dave Black's answer, I found a way to print any image type. Supposedly, you should be able to load any image type into a Pixbuf and use the Gdk CairoHelper to paint the Pixbuf on the CairoContext. I had issues with loading from file to Pixbuf when the type was not a PNG, so I used System.Drawing.Image to load it into a byte array first.

Also, make sure this occurs on the main thread of the application. If your code is occurring on a different thread, call

        Gtk.Application.Invoke(delegate {

to invoke on the main thread.

        var imageBit = default(byte[]);
var image = System.Drawing.Image.FromFile(fileName);
using (var memoryStream = new MemoryStream()) {
image.Save(memoryStream, ImageFormat.Png);
imageBit = memoryStream.ToArray();
}

var print = new PrintOperation();
print.BeginPrint += (obj, a) => { print.NPages = 1; };
print.DrawPage += (obj, a) => {
using (PrintContext context = a.Context) {
using (var pixBuf = new Gdk.Pixbuf(imageBit, image.Width, image.Height)) {
Cairo.Context cr = context.CairoContext;

cr.MoveTo(0, 0);
Gdk.CairoHelper.SetSourcePixbuf(cr, pixBuf, image.Width, image.Height);
cr.Paint();

((IDisposable) cr).Dispose();
}
}
};
print.EndPrint += (obj, a) => { };

print.Run(PrintOperationAction.Print, null);


Related Topics



Leave a reply



Submit