WinCe 5.0如何使用透明图像?
How to use transparent image in WinCe 5.0?
找到了对背景图像使用透明度的示例,但此选项不适合我。因为假设背景会随着某些动作而改变。
不确定您在做什么,因为细节不多,但希望这对您有所帮助。我们使用具有透明度的图标 (.ico) 文件,如下所示。这些只是将背景更改为单一颜色。如果您需要更复杂的行为,那么这可能不合适。
向您的项目添加一些图标(具有透明背景)。将 Build Action
设置为 Embedded Resource
。在下面的示例中,我们使用名为 ico1.ico
.
的图标
定义一个结构来保存您的图标。根据您需要的背景颜色数量,您需要的每个 icon/colour 组合都会有一个实例。如果在设计时数字非常大或未知,则您需要即时创建图标。
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
}
根据请求为 return 缓存图标编写辅助函数:
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);
找到了对背景图像使用透明度的示例,但此选项不适合我。因为假设背景会随着某些动作而改变。
不确定您在做什么,因为细节不多,但希望这对您有所帮助。我们使用具有透明度的图标 (.ico) 文件,如下所示。这些只是将背景更改为单一颜色。如果您需要更复杂的行为,那么这可能不合适。
向您的项目添加一些图标(具有透明背景)。将
Build Action
设置为Embedded Resource
。在下面的示例中,我们使用名为ico1.ico
. 的图标
定义一个结构来保存您的图标。根据您需要的背景颜色数量,您需要的每个 icon/colour 组合都会有一个实例。如果在设计时数字非常大或未知,则您需要即时创建图标。
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 }
根据请求为 return 缓存图标编写辅助函数:
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);