kooyee ‘s blog

          開源軟件, 眾人努力的結晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          公告

          一起暢游計算機的世界

          搜索

          •  

          積分與排名

          • 積分 - 163344
          • 排名 - 363

          最新評論

          SWT中打印圖片,圖像

          Posted on 2007-07-12 19:10 kooyee 閱讀(1523) 評論(0)  編輯  收藏 所屬分類: GUI骨衣
          //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();
              }

            }

          }


          和打印文字不同。因為系統中的dpi(dot per inch)和打印機的dpi不同,所以要進行轉換。
          主站蜘蛛池模板: 离岛区| 阜新市| 海城市| 宁海县| 迁西县| 靖西县| 武邑县| 福清市| 河池市| 上林县| 邮箱| 武城县| 沁阳市| 苗栗县| 时尚| 沁源县| 南投县| 拜城县| 长武县| 紫云| 龙川县| 永定县| 井冈山市| 遂昌县| 新疆| 巫山县| 永丰县| 成安县| 阿坝县| 洮南市| 西畴县| 黄冈市| 汉阴县| 衡山县| 双辽市| 博白县| 岚皋县| 青龙| 浦东新区| 惠来县| 军事|