标签:
原文:WPF 引用DLL纯图像资源包类库中的图片/// <summary> /// 读取符号(图片资源库中的文件) /// </summary> /// <param name="symbolName"></param> /// <returns></returns> public static ImageBrush GetImagebrush(string ImageName) { ImageBrush imageBrush = new ImageBrush(); System.Resources.ResourceManager rm = ImageLibrary.Properties.Resources.ResourceManager; System.Drawing.Bitmap b = (System.Drawing.Bitmap)rm.GetObject(ImageName); imageBrush.ImageSource = ToWpfBitmap(b); return imageBrush; }
public static BitmapSource ToWpfBitmap(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { //注意:转换的图片的原始格式ImageFormat设为BMP、JPG、PNG等 bitmap.Save(stream, ImageFormat.Png); stream.Position = 0; BitmapImage result = new BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return result; } }
标签:
原文地址:http://www.cnblogs.com/lonelyxmas/p/4446350.html