找到了将透明度与背景图像配合使用的示例,但此选项不适合我。由于假设背景会因某些操作而改变。
不确定细节,所以您不确定自己在做什么,但是希望这会有所帮助。我们使用具有透明度的图标(.ico)文件,如下所示。这些只是将背景更改为单一颜色。如果您需要更复杂的行为,那么这可能不合适。
将一些图标(具有透明背景)添加到您的项目。将设置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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句