2011-09-09 104 views
1

我有兩個圖像分層堆疊ontop,並希望能夠清除頂部圖像的一部分。通常情況下,如果我想清楚的圖像的部分,我只想畫它的背景顏色做C#圖形繪製區域上方的透明矩形

g.FillRectangle(Brushes.White,x,y,width,height); 

,但如果我這樣做,頂部圖像的底部圖像的區域得到由白色矩形覆蓋。我試着做

g.FillRectangle(Brushes.Transparent,x,y,width,height); 

但這似乎並沒有清除它的所有以前的內容的區域。有什麼辦法可以使該區域的像素透明嗎?

回答

2

這是不可能的。

GDI +和Graphics類不支持分層繪圖;一旦覆蓋之前的圖像,這些像素不見了

您應該通過調用帶有兩個矩形的DrawImage過載來重新繪製您想要出現的上一幅圖像的部分。

如果下面的圖像包含透明部分,您應該首先通過調用FillRectangle將該區域清除爲白色(或任何原始背景),以便透明度正確疊加。

2

另一種選擇是不直接繪製圖像。用途:

System.Windows.Forms.PictureBox 

和它的屬性

Region 

改變圖像的可見度/ transaparency。區域不必是矩形。它可以從任何一行中定義。

PS:Brushes.Transparent並不真正意味着透明,而是父容器的BackColor。

+1

你'PS'上的控件繪製時是唯一的真。位圖支持完整的alpha混合透明度。然而,「透明」意味着「無所事事」;它並不意味着「擦除像素」。 – SLaks

3
//using System.Drawing.Drawing2D; 

g.FillRectangle(Brushes.White,x,y,width,height); 
g.CompositingMode = CompositingMode.SourceCopy;  
g.FillRectangle(Brushes.Transparent,x,y,width,height); 
+1

這不適合我。我沒有注意到任何區別。 –

+0

這不起作用,CompositingMode.SourceCopy隻影響RGB通道。 –

0
float[][] ptsArray ={ 
      new float[] {1, 0, 0, 0, 0}, 
      new float[] {0, 1, 0, 0, 0}, 
      new float[] {0, 0, 1, 0, 0}, 
      new float[] {0, 0, 0, 0.5f, 0}, 
      new float[] {0, 0, 0, 0, 1}}; 
      ColorMatrix clrMatrix = new ColorMatrix(ptsArray); 
      ImageAttributes imgAttributes = new ImageAttributes(); 
      imgAttributes.SetColorMatrix(clrMatrix, 
      ColorMatrixFlag.Default, 
      ColorAdjustType.Bitmap); 
      _ImageThumb.Height, imgAttributes); 
      e.Graphics.DrawImage(_ImageThumb,new Rectangle(0, 0, _ImageThumb.Width,_ImageThumb.Height),0, 0, _ImageThumb.Width, _ImageThumb.Height,GraphicsUnit.Pixel, imgAttributes); 

//使用固定夾&區域繪製