http://msdn.microsoft.com/zh-tw/library/ms172507(VS.80).aspx

.NET Compact Framework 支援透明度,但只有一種透明度色彩。SetColorKey 方法必須針對低色彩和高色彩範圍指定相同的色彩。

此範例會以紅色和黑色的設計來建立矩形的 Bitmap,並示範兩種設定透明度的技巧:

根據影像的像素來使用 SetColorKey 方法。此範例會使用影像左上方的像素來設定透明度。因為此像素是黑色的,所有原先為黑色的像素將會變成透明。

以明確的色彩設定來使用 SetColorKey 方法。此範例會將色彩設為紅色,因此所有原先為紅色的像素將會變成透明。

若要示範,先將兩種透明度技巧標記為註解再執行應用程式,以查看影像沒有設定透明度時出現的樣子。然後針對其中一種技巧,取消註解程式碼。

// The .NET Compact Framework supports transparency,

// but with only one transparency color. 
// The SetColorKey method must have the same color  
// specified for the low color and high color range. 
 // Note that you must uncomment lines of code 
// as indicated for the desired transparency effect.  
protected override void OnPaint(PaintEventArgs e) {     
// Create a red and black bitmap to demonstrate transparency.     
Bitmap bmp = new Bitmap(75,75);     
Graphics g = Graphics.FromImage(bmp);      
g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);     
g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);     
g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);     
g.Dispose();      ImageAttributes attr = new ImageAttributes();      
// Set the transparency color key based on the upper-left pixel      
// of the image.     
// Uncomment the following line to make all black pixels transparent:     
// attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));     
 // Set the transparency color key based on a specified value.     
// Uncomment the following line to make all red pixels transparent:     
// attr.SetColorKey(Color.Red, Color.Red);      
// Draw the image using the image attributes.     
Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);     
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,GraphicsUnit.Pixel, attr); 
}

arrow
arrow
    全站熱搜

    雀悦 發表在 痞客邦 留言(0) 人氣()