如何在Windows Mobile下使用.NET Compact Framework畫透明圖片

來源:互聯網
上載者:User

Iphone之所以那麼流行一部分歸功於他的炫麗的介面,其實那些介面的實現主要由兩大功能組成:畫透明圖片和畫漸層效果。這次主要講述windows mobile下畫透明圖片。

所謂透明圖片其實就是把圖片背景設定成透明,如果直接畫出圖片會顯示一個規則正方形的圖,其中圖片包括不規則圖形和該映像下的背景,所以如果只是想畫出不規則圖形,需要把背景去掉。下面為去掉背景的實現。

/// <summary>
/// Draws the image with transparency
/// </summary>
/// <param name="gx">Destination graphics</param>
/// <param name="image">The image to draw</param>
/// <param name="destRect">Desctination rectangle</param>
public static void DrawImageTransparent(this Graphics gx, Image image, Rectangle destRect)
{
ImageAttributes imageAttr = new ImageAttributes();
Color transpColor = GetTransparentColor(image);
imageAttr.SetColorKey(transpColor, transpColor);
gx.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
imageAttr.Dispose();
}

private static Color GetTransparentColor(Image image)
{
return ((Bitmap)image).GetPixel(image.Width - 1, image.Height - 1);
}

先調用Bitmap.GetPixel()函數取出圖片的背景資訊,然後設定ImageAttributes 的屬性,在畫圖的時候把圖片屬性傳遞到Graphics.DrawImage()函數進行繪畫。

如下:

 

有些圖片做的不好,有點鋸齒。

 下面是沒有去掉背景的圖片,作為比較。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.