簡體   English   中英

在WinCe 5.0中如何使用透明圖像?

[英]How to use transparent image in WinCe 5.0?

找到了將透明度與背景圖像結合使用的示例,但此選項不適合我。 由於假定背景會因某些操作而改變。

不確定細節,您不確定自己在做什么,但是希望這會有所幫助。 我們使用具有透明度的圖標(.ico)文件,如下所示。 這些只是將背景更改為單色。 如果您需要更復雜的行為,那么這可能不合適。

  • 將一些圖標(具有透明背景)添加到您的項目。 將“ Build Action設置為Build Action Embedded Resource 在下面的示例中,我們使用一個名為ico1.ico的圖標。

  • 定義一個結構來保存您的圖標。 根據您需要的背景顏色數量,您需要的每種圖標/顏色組合都會有一個實例。 如果數量很大或在設計時未知,則需要即時創建圖標。

     public struct CacheGraphics { public Bitmap ico1White, ico1Blue; } public static CacheGraphics cacheGraphics;` 
  • 緩存圖標:

     cacheGraphics.ico1White = new Bitmap(GetIconImage("ico1", Color.White)); cacheGraphics.ico1Blue = new Bitmap(GetIconImage("ico1", Color.Blue));` 
  • 編寫一個輔助函數來修改背景顏色:

     private static Bitmap GetIconImage(string szIcon, Color clrBackground) { // Convert an embedded icon into an image // Load icon string szImage = ("YOUR-PROJECT.Resources.Icons." + szIcon + ".ico"); Assembly _assembly = Assembly.GetExecutingAssembly(); Stream file = _assembly.GetManifestResourceStream(szImage); Icon icoTmp = new Icon(file); // Create new image Bitmap bmpNewIcon = new Bitmap(icoTmp.Width, icoTmp.Height, PixelFormat.Format32bppRgb); // Create a graphics context and set the background colour Graphics g = Graphics.FromImage(bmpNewIcon); g.Clear(clrBackground); // Draw current icon onto the bitmap g.DrawIcon(icoTmp, 0, 0); // Clean up... g.Dispose(); // Return the new image return bmpNewIcon; } 
  • 為每個圖標定義一個簡單的別名:

     // Alias which goes at the top of any file using icons: using icons = YOUR-PROJECT.CCommon.AppIcons; public enum AppIcons { ICO1_WHITE, ICO1_BLUE } 
  • 編寫一個輔助函數,以根據要求返回緩存的圖標:

     public static Image GetCachedIcon(AppIcons eIcon) { // Return a cached icon image. These icons are cached at application startup. Image imgIcon = null; switch (eIcon) { // System Settings > Advanced case AppIcons.ICO1_WHITE: imgIcon = (Image)cacheGraphics.ico1White; break; case AppIcons.ICO1_BLUE: imgIcon = (Image)cacheGraphics.ico1Blue; break; } return imgIcon; } 
  • 需要時使用圖標:

     picturebox1.Image = CCommon.GetCachedIcon(icons.ICO1_WHITE); picturebox2.Image = CCommon.GetCachedIcon(icons.ICO1_BLUE); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM