我使用以下代码在monotouch中打印图像(png文件):
void Print ()
{
UIImage image = myImgView.Image;
NSMutableData data = new NSMutableData();
UIGraphics.BeginPDFContext(data, new RectangleF(new PointF(0,0), image.Size), null);
image.Draw(new RectangleF(0, 0, image.Size.Width, image.Size.Height));
UIGraphics.EndPDFContent();
var printInfo = UIPrintInfo.PrintInfo;
printInfo.JobName = "Test: PDF Print";
var printer = UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintingItem = image; //url;
printer.ShowsPageRange = true;
printer.Present (true, (handler, completed, err) => {
if (!completed && err != null){
Console.WriteLine ("error");
}
} );
}
图像打印出来,但会占据整个页面。如何将图像限制为实际尺寸? (约为200px x 200像素)
编辑:
将我的代码更改为此,但结果仍然相同:
void Print ()
{
UIImage image = myImgView.Image;
NSData data = new NSData();
// var imageRect = new RectangleF (){
// Height = image.Size.Height,
// Width = image.Size.Width,
// Size = image.Size
//
// } ;
var printInfo = UIPrintInfo.PrintInfo;
printInfo.JobName = "Test";
var printer = UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintingItem = data;
printer.PrintFormatter = new UIPrintFormatter(){
MaximumContentWidth = image.Size.Width
} ;
printer.ShowsPageRange = true;
printer.Present (true, (handler, completed, err) => {
if (!completed && err != null) {
Console.WriteLine ("error");
}
} );
}
答案 0 :(得分:0)
我最终使用了自定义UIPrintPageRenderer类,如https://github.com/xamarin/monotouch-samples/blob/master/RecipesAndPrinting/RecipePrintPageRenderer.cs
所示