學習筆記:將Image(SWT)存到一個OutputStream

來源:互聯網
上載者:User

     想把一個自己產生的org.eclipse.swt.graphics.Image存到一個資料流或檔案中,但是沒有似乎Image沒有寫到檔案中的API。
     查到AWT的ImageIO有個Write()函數,可惜是AWT的。
     發現org.eclipse.swt.graphics.ImageLoader提供save功能,既可以把它存到資料流中,也可以存到檔案中。但是它的Load()函數只提供FileName和InputStream這兩種參數。
     鬱悶。

     ImageLoader的data[] 竟然是public,那就意味著把源Image的ImageData賦值給它,它的Save()照樣可用了。

                ImageData imageData = sourceImage.getImageData( );
                ImageLoader imageLoader = new ImageLoader();
                imageLoader.data = new ImageData[1];
                imageLoader.data[0] = imageData;
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                imageLoader.save( outputStream, SWT.IMAGE_JPEG );

     這樣,就把sourceImage以JPEG格式存到資料流outputStream中了。

     從SWT Newsgroup上得知,這種方法有一定局限。如果一個Composite在畫面以外,那麼在畫面以外的部分是不會繪出來。如果這個Composite在畫面以內,但是它是不可見的(Visual 為false),畫到Image中的內容也不是希望中的Composite。

參考資料:
1.Create Image from Composite for printing 及其相關回帖
http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg26082.html

2.SWT Snippets
http://www.eclipse.org/swt/snippets/  中的capture a widget image with a GC

3.addison.wesley.professional.swt.the.standard.widget.toolkit.volume.1
中關於SWT的章節

(完)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.