2011-02-27 46 views
4

我正在開發一個使用Windows 7(和Vista)任務欄功能的程序。現在我有一個自定義位圖,將顯示在任務欄縮略圖。位圖以編程方式創建併成功顯示。我遇到的唯一問題是,想要在該圖像中使用透明,該圖像在縮略圖中也應該顯示爲透明。但是沒有任何成功,導致顏色的標準淺灰色顏色。如何讓我的任務欄縮略圖上的區域在Windows 7中顯示爲透明?

我見過的證據表明,方案順利拿到透明在他們的形象:


現在是我的問題:怎麼辦我的縮略圖圖像變得透明瞭嗎?


我會與Graphics類中填充的圖像,所以任何事情是允許的。
我應該提及的是我使用Windows® API Code Pack,它使用GetHbitmap到 將圖像設置爲縮略圖。

編輯:
只是爲了使其完整,這是我使用ATM代碼:

Bitmap bmp = new Bitmap(197, 119); 

Graphics g = Graphics.FromImage(bmp); 
g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, bmp.Width, bmp.Height)); // Transparent is actually light-gray; 
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
g.DrawString("Information:", fontHeader, brush, new PointF(5, 5)); 

bmp.MakeTransparent(Color.Red); 
return bmp; 
+1

嘗試在不透明的黑色中繪製您想要顯示爲透明的區域,如[Rolling Stones說的要做的事](http://en.wikipedia.org/wiki/Paint_It,_Black)。我不確定這是否會起作用,因爲您實際上可能希望黑色出現在縮略圖中,但這是DWM用於透明區域的顏色。 'bmp.MakeTransparent'幾乎肯定是這樣做的錯誤方法。 – 2011-02-27 11:03:42

回答

0

System.Drawing.Bitmap支持Alpha水平。所以,最簡單的方法是

Graphics g = Graphics.FromImage(bmp); 
g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, bmp.Width, bmp.Height)); // Transparent is actually light-gray; 
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
g.DrawString("Information:", fontHeader, brush, new PointF(5, 5)); 

但是你也可以通過

new SolidBrush(Color.FromArgb(150, 255, 255, 255)); 
2

什麼像素格式是位圖替換Brushes.Transparent有部分透明?如果它沒有Alpha通道,則無法將透明度信息存儲在圖像中。

這裏是如何創建一個位圖的alpha通道,並使其透明默認:

Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb); 
using(Graphics graphics = Graphics.FromImage(image)) 
{ 
    graphics.Clear(Color.Transparent); 
    // Draw your stuff 
} 

然後,您可以畫出你想要的任何東西,包括使用alpha通道半透明的東西。

還要注意的是,如果你想在現有的不透明的東西畫透明度(說做一個洞),你需要改變合成模式:

graphics.CompositingMode = CompositingMode.SourceCopy; 

這會讓你使用任何顏色覆蓋一個在圖像中而不是與之混合。

相關問題