views:

749

answers:

2

Hey guys,

I've been trying to print a dynamically loaded jpg and for some reason it's never printing to scale, not sure what I'm doing wrong so here's what I've done so far

var request:URLRequest = new URLRequest(getAbsPath("pages/" + pagePrint + "_big.jpg"));
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

var frame:Sprite = new Sprite()
function completeHandler(event:Event):void {

    var picture:Bitmap = Bitmap(loader.content);
    var bitmap:BitmapData = picture.bitmapData;
    var matrix:Matrix = new Matrix();
    matrix.scale(1, 1);

    frame.graphics.beginBitmapFill(bitmap, matrix, true);
    frame.graphics.drawRect(0, 0, bitmap.width, bitmap.height);
    frame.graphics.endFill();
    addChild(frame);
    frame.visible = false;

    printPage();    
}
function printPage ():void {
    var myPrintJob:PrintJob = new PrintJob();
    var options:PrintJobOptions = new PrintJobOptions();
    options.printAsBitmap = true;

    myPrintJob.start();

    try {
        myPrintJob.addPage(frame, null, options);
    }
    catch(e:Error) {
        trace ("Had problem adding the page to print job: " + e);
    }

    try {
        myPrintJob.send();
    }
    catch (e:Error) {
        trace ("Had problem printing: " + e);    
    }
}

For some reason that only takes one part of the image and blows it up to fill an entire page... the jpg dimentions are 1280x1656 and I would like it to print the entire jpg... Any ideas what I'm doing wrong?

A: 

I'd scale the clip appropriately before you try printing.

You can get the pixel width and height of the output page with:

myPrintJob.pageWidth
myPrintJob.pageHeight

And set the .scaleX and .scaleY of the frame based on that.

(Print handling is notoriously rubbish in every version of the Flash Player; you have to do a lot of work yourself, I'm afraid...)

IanT
A: 

I'm guessing that your image is being sent to the print spooler at default resolution of 72dpi, so it's coming out huge (at 1280x1656 that's a 17.777" x 23" image) so I'm guessing you're seeing only the top left most portion fitting on the physical printed page.

To solve this, you can either scale the image down to fit on the page (probably to about .45 scale at least) which will come out with less detail dues to it still being a fairly low 72dpi resolution. Or you can use the second approach, which is to set the DPI to be more accurate for your desired print size (probably 300dpi or so)

If you need to reset the internal DPI of the image, the only method I've seen (but not tried personally) is the ImageSnapshot class in the Flex SDK. You can use it to extract BitmapData at a specific DPI, then you could send that BitmapData to the PrintJob.

ImageSnapshot.captureBitmapData() Documentation

Also, a quicker easier thing to try... If the JPEG image file you're loading does have a higher DPI, that is probably being lost when you do your BeginBitmapFill(). Could you try adding the Bitmap as a child of 'frame' rather than drawing it? That may preserve the resolution properly for you.

JStriedl