SWT中打印图片、图像

本文转载自: http://www.blogjava.net/Javawind/articles/129899.html
  // Send questions, comments, bug reports, etc. to the authors:

// Rob Warner (rwarner@interspatial.com)
// Robert Harris (rbrt_harris@yahoo.com)

import  org.eclipse.swt. * ;
import  org.eclipse.swt.graphics. * ;
import  org.eclipse.swt.printing. * ;
import  org.eclipse.swt.widgets. * ;

/** */ /**
 * This class demonstrates printing images
 
*/

public   class  ImagePrinterExample  {
  
/** *//**
   * The application entry point
   * 
@param args the command line arguments
   
*/

  
public static void main(String[] args) {
    Display display 
= new Display();
    Shell shell 
= new Shell(display, SWT.NONE);

    
try {
      
// Prompt the user for an image file
      FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
      String fileName 
= fileChooser.open();

      
if (fileName == nullreturn; }

      
// Load the image
      ImageLoader loader = new ImageLoader();
      ImageData[] imageData 
= loader.load(fileName);

      
if (imageData.length > 0{
        
// Show the Choose Printer dialog
        PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
        PrinterData printerData 
= dialog.open();

        
if (printerData != null{
          
// Create the printer object
          Printer printer = new Printer(printerData);
  
          
// Calculate the scale factor between the screen resolution and printer
          
// resolution in order to correctly size the image for the printer
          Point screenDPI = display.getDPI();
          Point printerDPI 
= printer.getDPI();
          
int scaleFactor = printerDPI.x / screenDPI.x;
  
          
// Determine the bounds of the entire area of the printer
          Rectangle trim = printer.computeTrim(0000);

          
// Start the print job
          if (printer.startJob(fileName)) {
            
if (printer.startPage()) {
              GC gc 
= new GC(printer);
              Image printerImage 
= new Image(printer, imageData[0]);
              
              
// Draw the image
              gc.drawImage(printerImage, 00, imageData[0].width,
                imageData[
0].height, -trim.x, -trim.y, 
                scaleFactor 
* imageData[0].width, 
                scaleFactor 
* imageData[0].height);
  
              
// Clean up
              printerImage.dispose();
              gc.dispose();
              printer.endPage();
            }

          }

          
// End the job and dispose the printer
          printer.endJob();
          printer.dispose();
        }

      }

    }
 catch (Exception e) {
      MessageBox messageBox 
= new MessageBox(shell, SWT.ICON_ERROR);
      messageBox.setMessage(
"Error printing test image");
      messageBox.open();
    }

  }

}

 
<script type="text/javascript">//</script> 和打印文字不同。因为系统中的dpi(dot per inch)和打印机的dpi不同,所以要进行转换。 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值