2

well i have a picture and i want to print it in exactly actual size on paper and implemented below code but i don't know why this code zooms the picture but i want picture to keep its size , could you please help me ?

Thanks a lot ...

    final Image img = new ImageIcon("C:\\check.jpg").getImage();
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(new Printable()
    {
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex != 0) {
                return NO_SUCH_PAGE;
            }
            graphics.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null),   null);
            return PAGE_EXISTS;
        }
    });
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception prt) {
            System.err.println(prt.getMessage());
        }
    }
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
omid askari
  • 41
  • 1
  • 5
  • 1
    How do you know what the 'actual size' is? – z7sg Ѫ Mar 17 '11 at 11:54
  • actual size is the size when take a properties of an image windows tells you in summary tab , width and height pal ... – omid askari Mar 17 '11 at 17:03
  • You have to somehow get the metadata. It's not normally preserved when you read an image. And image metadata is not a simple thing to crack, once you have it. – Hot Licks Oct 04 '13 at 00:05

1 Answers1

1

That is probably because your image does not have the correct resolution. Google “dots per inch” or “image resolution,” then take a look at the javadoc for PageFormat which obviously uses a resolution of 72 dpi. Now you should have enough information to scale your image correctly.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • well you know i looked all of them but it doesn't look like it's about java code , when i drawImage in a jpanel it has exactly the size of original image size but when i implement that code in paint function and print it with XPS printer in windows image makes larger , why ? is this about xps printer ? – omid askari Mar 17 '11 at 17:00
  • @omidaskari because your screen is probably 96dpi, try scaling it by 1.3x and see if that fixes it. You'll probably need some code to get screen dpi for this, as screens can be different. – Serdalis Oct 03 '13 at 23:36