2015-02-10 372 views
1
Bitmap bmp1 = new Bitmap(@"c:\coneimages\Cone_Images1.gif"); 
Bitmap bmp2 = new Bitmap(@"c:\coneimages\PictureBox_Images1.gif"); 
Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1,bmp2)); 
bmp3.Save(@"c:\coneimages\merged.bmp"); 

而且如何將兩張圖像合併爲一張圖像,並將兩張圖像合併爲一張圖像,然後再將第二張圖像合成一張?

public static Bitmap MergeTwoImages(Image firstImage, Image secondImage) 
     { 
      if (firstImage == null) 
      { 
       throw new ArgumentNullException("firstImage"); 
      } 

      if (secondImage == null) 
      { 
       throw new ArgumentNullException("secondImage"); 
      } 

      int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width; 

      int outputImageHeight = firstImage.Height + secondImage.Height + 1; 

      Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

      using (Graphics graphics = Graphics.FromImage(outputImage)) 
      { 
       graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size), 
        new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel); 
       graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size), 
        new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel); 
      } 

      return outputImage; 
     } 

但是,這不是我想要的我不知道如何以及如何尋找在谷歌。 我想要的是bmp1將會像bmp2一樣。 bmp1是透明的,我希望它像bmp2上的圖層一樣。

所以在bmp3中,我會看到整個正常bmp2與bmp1就可以了。

回答

2

假設bmp1中有α,與設置爲SourceOver(默認值)的合成模式繪製bmp1繪製bmp2第一,THEN。這應該完成正確的alpha混合順序。

換句話說...

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp2,bmp1)); // Swapped arguments. 

如果bmp1不包含Alpha,你將需要使用彩色矩陣來改變透明度。

1
Bitmap m_back = new Bitmap(bmp2.Width, bmp2.Height); 
for (int y = 0; y < bmp2.Height; y++) 
{ 
    for (int x = 0; x < bmp2.Width; x++) 
    { 
      Color temp = Color.FromArgb(80, bmp2.GetPixel(x, y)); 
      m_back.SetPixel(x, y, temp); 
    } 
} 

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1, m_back)); 
+0

http://www.codeproject.com/Articles/6504/Transparency-Tutorial-with-C-Part – Ibki 2015-02-10 02:14:34

相關問題