如何打印图像的GTKSharp [英] How To Print Image with GTKSharp

查看:212
本文介绍了如何打印图像的GTKSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植的C#.NET应用程序的.NET单在OSX执行。应用程序的一部分包括打印图像。很容易在.NET和归结为主要

  e.Graphics.DrawImage(IMG,新的Rectangle(X,Y,printSize .WIDTH,printSize.Height)); 

在的PrintDocument的PrintPage事件。单没有完全实现System.Drawing.Printing,因此它似乎是最好的方式去这样做是使用GtkSharp。我发现了一些例子在线至于如何使用Gtk.PrintOperation打印文本。 PrintOperation有一个事件一个DrawPage这应该是类似的PrintPage,但我只能找到如何使用PangoSharp打印在此事件中的文本。



足够解释什么我知道,我认为这个问题是相当简单的。我希望有人能够帮助为GtkSharp印刷是不是有据可查的。如果有更好的方式来做到这一点以外GtkSharp,我所有的耳朵。



非常感谢。



修改



所以,我已经成功地得到的东西打印,然而,它始终是一个空白页。下面是代码:

  VAR打印=新PrintOperation(); 
print.BeginPrint + =(OBJ,参数)=> {print.NPages = 1; };
print.DrawPage + =(OBJ,参数)=> {
PrintContext背景= args.Context;
Cairo.Context CR = context.CairoContext;

变种imageSurface =新Cairo.ImageSurface(printImage.FileName);
cr.MaskSurface(imageSurface,0,0);
};
print.EndPrint + =(OBJ,参数)=> {};

print.Run(PrintOperationAction.Print,NULL);


解决方案

通过戴夫黑色的回答有点操纵,我发现一种方法来打印任何图像类型。据说,你应该能够对任何类型的图像加载到一个PIXBUF并使用GDK CairoHelper绘制PIXBUF上CairoContext。我曾与加载问题,从文件到PIXBUF当类型不是一个PNG,所以我用System.Drawing.Image对象将其加载到一个字节数组第一。



另外,确保发生这种情况的应用程序的主线程上。如果你的代码是在不同的线程中发生,请致电

  Gtk.Application.Invoke(委托{

来调用主线程上。

  VAR imageBit =默认值(字节[]); 
VAR图像= System.Drawing.Image.FromFile(文件名);
使用(VAR的MemoryStream =新的MemoryStream()){
image.Save(MemoryStream的,ImageFormat.Png);
imageBit = memoryStream.ToArray();
}

VAR打印=新PrintOperation();
打印。 BeginPrint + =(OBJ,A)=> {print.NPages = 1;};
print.DrawPage + =(OBJ,A)=> {
使用(PrintContext背景= a.Context ){使用
(VAR PIXBUF =新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);


I'm porting an C# .NET application to Mono .NET for executing in OSX. Part of the application involves printing an image. Fairly easy in .NET and boils down to primarily

e.Graphics.DrawImage(img, new Rectangle(x, y, printSize.Width, printSize.Height));

in the PrintDocument's PrintPage event. Mono does not have a full implementation of System.Drawing.Printing so it seems that the best way to go about doing this is using GtkSharp. I've found some examples online as to how to print text using a Gtk.PrintOperation. PrintOperation has an event DrawPage which should be similar to PrintPage, however I could only find how to use PangoSharp to print text in this event.

Enough explaining what I do know, I think the question is fairly straight forward. I hope somebody can be of help as GtkSharp's printing is not well documented. If there is a better way to do this outside GtkSharp, I am all ears.

Many thanks.

EDIT

So I've managed to get something printing, however, it is always a blank page. Here is the code:

var print = new PrintOperation();
print.BeginPrint += (obj, args) => { print.NPages = 1; };
print.DrawPage += (obj, args) => {
    PrintContext context = args.Context;
    Cairo.Context cr = context.CairoContext;

    var imageSurface = new Cairo.ImageSurface(printImage.FileName);
    cr.MaskSurface(imageSurface, 0, 0);
};
print.EndPrint += (obj, args) => { };

print.Run(PrintOperationAction.Print, null);

解决方案

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);

这篇关于如何打印图像的GTKSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆