将位图转换为图像

因此,在我从DLL中取出图像并将其放入图像控件后,它就是一个BitmapImage。 要将其打包回dll,必须将其转换回图像。 如何将其转换回图像,如何将其重新打包回dll? 这都是用c#编写的wpf。

private void compileDLL_Click(object sender, RoutedEventArgs e) { string sourcePath = Directory.GetCurrentDirectory() + "\\PCAngelResources.dll"; //destination path string dllname = textBox1.Text + "_PCAngelResources.dll"; string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string destFile = System.IO.Path.Combine(targetPath, dllname); System.IO.File.Copy(sourcePath, destFile, true); //lstImages = new Dictionary(); //string filename = "PCAngelResources.dll"; Assembly pcangdll = Assembly.LoadFile(sourcePath); System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture; ResourceManager rm = new ResourceManager("PCAngelResources.DynResources", pcangdll); rs = rm.GetResourceSet(culture, true, true); ResourceWriter writer = new ResourceWriter(destFile); foreach (DictionaryEntry resource in rs) { resources.Add((string)resource.Key); if (resource.Key.Equals("Branding") || resource.Key.Equals("Advertising")) { if (resource.Key.Equals("Branding")) { writer.AddResource("Branding", image5.Source); //System.Object obj = rm.GetObject((string)resource.Key); //lstImages.Add((string)resource.Key, (Bitmap)obj); } else if (resource.Key.Equals("Advertising")) { writer.AddResource("Advertising", image6.Source); } } } writer.Generate(); System.Windows.MessageBox.Show("Done", "Process Finished", MessageBoxButton.OK, MessageBoxImage.Asterisk, MessageBoxResult.OK); } 

当我执行writer.Generate()来创建新的DLL时,我收到以下错误:mscorlib.dll中发生了未处理的类型’System.Runtime.Serialization.SerializationException’exception

附加信息:在程序集’PresentationCore中输入’System.Windows.Media.Imaging.BitmapFrameDecode’,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35’未标记为可序列化。

我不知道你的意思是“将它重新打包回DLL”,但是有一种简单的方法可以将WPF图像从BitmapSource转换回System.Drawing.Image。 以下方法实现了:

 ///  /// Converts a WPF bitmap to a System.Drawing.Bitmap ///  /// BitmapSource to convert /// A GDI Bitmap public static System.Drawing.Bitmap GdiBitmapFromWpfBitmap(BitmapSource wpfBitmap) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(wpfBitmap)); MemoryStream imageStream = new MemoryStream(); encoder.Save(imageStream); System.Drawing.Bitmap gdiBitmap = new System.Drawing.Bitmap(imageStream); imageStream.Close(); imageStream.Dispose(); return gdiBitmap; }