2011-08-05 33 views

回答

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; 

這並不是說如果疊加圖像不具有一些透明的,其尺寸爲等於或更大比基本圖像,它將完全重疊其他圖像,所以你的覆蓋圖像必須有一些不透明度。

+0

@ Strider007你嘗試了這一點?它解決了你的問題嗎? –

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; 
      } 
      
  • 相關問題