WPF 是个用户控件, 被 WinForm 调用. 而 WinForm 是在一个 DLL 类库中被调用. 试了很多方法, 都无法将 Resource 中的图读进程序. 用下面的方法总算实现了.
/ 根据图片的名称, 从资源中找到它
- public static System.Drawing.Bitmap GetPngFromResources(string pngName)
- {
- //http://www.worlduc.com/blog2012.aspx?bid=760294 c# 访问 Resources.resx 中的资源
- ResourceManager rmManager = global::AddMenuToRevit2013.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
- // http://www.dotblogs.com.tw/bauann/archive/2013/04/18/101793.aspx
- 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;
- }
来源: http://www.bubuko.com/infodetail-2727503.html