3 回答
TA贡献2003条经验 获得超2个赞
这里的代码为资源词典中的任何位图资源(而不是Windows.Forms时代经常使用的Resources.resx)设置透明背景。我在InitializeComponent()-methode之前调用此方法。上面的melvas在文章中提到了方法'ConvertBitmap(Bitmap source)'和BitmapFromSource(BitmapSource bitmapsource)。
private void SetBitmapResourcesTransparent()
{
Image img;
BitmapSource bmpSource;
System.Drawing.Bitmap bmp;
foreach (ResourceDictionary resdict in Application.Current.Resources.MergedDictionaries)
{
foreach (DictionaryEntry dictEntry in resdict)
{
// search for bitmap resource
if ((img = dictEntry.Value as Image) is Image
&& (bmpSource = img.Source as BitmapSource) is BitmapSource
&& (bmp = BitmapFromSource(bmpSource)) != null)
{
// make bitmap transparent and assign it back to ressource
bmp.MakeTransparent(System.Drawing.Color.Magenta);
bmpSource = ConvertBitmap(bmp);
img.Source = bmpSource;
}
}
}
}
- 3 回答
- 0 关注
- 1596 浏览
添加回答
举报