public static System.Drawing.Bitmap GetPngFromResources(string pngName)
{
//www.worlduc.com/blog2012.aspx?bid=760294 c#访问Resources.resx中的资源
ResourceManager rmManager = global::WpfApplication1.Properties.Resources.ResourceManager;
object obj = rmManager.GetObject(pngName);
if (obj == null)
{
MessageBox.Show("未找到图片:" + pngName);
return null;
}
System.Drawing.Bitmap b = obj as System.Drawing.Bitmap;
if (b != null)
{
return b;
}
else
{
MessageBox.Show("未找到图片:" + pngName);
return null;
}
}
// System.Drawing.Bitmap要如何轉換成WPF中可用的ImageSource
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, bitmap.RawFormat);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
推荐您阅读更多有关于“WPF4.5,”的文章