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
Detect Windows Version in .Net
How to Determine the Owner of a Process in C#
C# Reflection: How to Get Class Reference from String
How to Use Nuget Packages in My Azure Functions
Globally Catch Exceptions in a Wpf Application
Associating Enums with Strings in C#
How to Set Processor Affinity to a Thread or a Task in .Net
Return File in ASP.NET Core Web API
Difference Between Invariantculture and Ordinal String Comparison
Does Mono Support System.Drawing and System.Drawing.Printing
Easiest Way to Parse JSON Response
Change Wpf Controls from a Non-Main Thread Using Dispatcher.Invoke
Creating a Generic<T> Type Instance with a Variable Containing the Type
Combobox: Adding Text and Value to an Item (No Binding Source)