在窗體應用程序,我有一個Drawing.Bitmap和DrawingImage作爲輸入。我需要覆蓋它們並將輸出放在Controls.Image中。我怎樣才能做到這一點?有沒有方法來覆蓋或合併Drawing.Bitmap和DrawingImage
0
A
回答
4
不要緊,如果您使用Image
對象或Bitmap
對象,Drawing.Image
是抽象類和Drawing.Bitmap
繼承它。到 在圖像上繪製圖像,從基本圖像中獲取圖形對象,然後使用Graphics.DrawImage接受Image
類型的參數。
所以,你在這裏兩個圖像,一個應印有「疊加」比其他圖像:
System.Drawing.Image primaryImage = Image.FromFile(@"Your file path");//or resource..
using (Graphics graphics = Graphics.FromImage(primaryImage))//get the underlying graphics object from the image.
{
using (Bitmap overlayImage = new Bitmap(primaryImage.Width, primaryImage.Hieght,
System.Drawing.Imaging.PixelFormat.Format32bppArgb)//or your overlay image from file or resource...
{
graphics.DrawImage(overlayImage, new Point(0, 0));//this will draw the overlay image over the base image at (0, 0) coordination.
}
}
Control.Image = primaryImage;
這並不是說如果疊加圖像不具有一些透明的,其尺寸爲等於或更大比基本圖像,它將完全重疊其他圖像,所以你的覆蓋圖像必須有一些不透明度。
2
我意識到它已經有一段時間了,但這裏的答案不是相當於爲我工作。稍微調整一下,雖然讓他們工作得很好。對於它的價值,這裏是我的最終版本。
SCENARIO:
- 背景圖像是RGB 24
- 覆蓋圖像是ARGB 32與已經正確設置alpha通道。從內存流
問題創建
- 創建從內存流疊加圖像假定我的意思是:
Format32bppRgb
- 但我們需要的是
Format32bppArgb
因爲透明度已經到位..
SOLUTION:
pictureBox1.Image = MergeImages(backgroundImage, overlayImage);
using System.Drawing; using System.Drawing.Imaging; // ... private Image MergeImages(Image backgroundImage, Image overlayImage) { Image theResult = backgroundImage; if (null != overlayImage) { Image theOverlay = overlayImage; if (PixelFormat.Format32bppArgb != overlayImage.PixelFormat) { theOverlay = new Bitmap(overlayImage.Width, overlayImage.Height, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(theOverlay)) { graphics.DrawImage(overlayImage, new Rectangle(0, 0, theOverlay.Width, theOverlay.Height), new Rectangle(0, 0, overlayImage.Width, overlayImage.Height), GraphicsUnit.Pixel); } ((Bitmap)theOverlay).MakeTransparent(); } using (Graphics graphics = Graphics.FromImage(theResult)) { graphics.DrawImage(theOverlay, new Rectangle(0, 0, theResult.Width, theResult.Height), new Rectangle(0, 0, theOverlay.Width, theOverlay.Height), GraphicsUnit.Pixel); } } return theResult; }
相關問題
- 1. 沒有合適的方法來覆蓋
- 2. 沒有合適的方法來覆蓋更新(GameTime gameTime)
- 3. 有沒有辦法來覆蓋ConfigurationManager.AppSettings?
- 4. React.Component.defaultProps對象被覆蓋,沒有合併?
- 5. expressjs支持方法刪除,並把沒有方法覆蓋
- 6. 有沒有更好的方法來覆蓋文件內容?
- 7. Checkstyle檢查ParameterNumber沒有覆蓋方法
- 8. 有沒有簡單的方法來覆蓋列表對象的方法__getitem__?
- 9. 「OnDisconnected():沒有找到合適的方法覆蓋」 - SignalR
- 10. Java私有方法覆蓋
- 11. 覆蓋私有方法
- 12. WindowsFormsApplication1.Form1.Dispose(布爾)':沒有找到合適的方法來覆蓋
- 13. 沒有找到合適的方法來覆蓋部分類錯誤
- 14. 方法覆蓋和功能覆蓋/超載有什麼區別
- 15. 有沒有辦法用反射「覆蓋」一個方法?
- 16. 有沒有辦法覆蓋openjpa中的javax.persistence.EntityManager.persist()方法
- 17. 是否有任何方法來覆蓋CONSUMER_KEY和CONSUMER_SECRET
- 18. asp.net MVC多租戶:有沒有辦法來覆蓋/影響User.Identity.IsAuthenticated?
- 19. 有沒有辦法來覆蓋編譯器::模板中定義()
- 20. 有沒有辦法來防止覆蓋的PHP設置?
- 21. 有沒有辦法來覆蓋[DataType(DataType.Password)]的錯誤消息
- 22. 覆蓋imageview沒有推佈局下來
- 23. 關於方法在StringBuffer和StringBuilder中沒有被覆蓋?
- 24. 刪除超類方法並刪除所有覆蓋方法
- 25. 有沒有簡單的方法來覆蓋樣式表內聯樣式?
- 26. KnockoutJS - 有沒有簡單的方法來覆蓋單個對象的observables?
- 27. 有沒有好的方法來覆蓋iOS應用程序中的DNS?
- 28. 有沒有一種方法來覆蓋android框架膨脹資源的getString()?
- 29. 無法在beta5中覆蓋OnConfiguring - 找不到合適的方法來覆蓋
- 30. 有沒有辦法通過替換部分或全部類型參數來覆蓋虛擬泛型方法?
@ Strider007你嘗試了這一點?它解決了你的問題嗎? –