C# 将位图转换为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Bitmap to Image
提问by Jake
So after I pull an image out of DLL and put it into an image control it is a BitmapImage. To package it back into the dll it has to be converted back to an image. How can I convert it back to image and how can i repackage it back into the dll? This is all in wpf written in c#.
因此,在我从 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, Bitmap>();
//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);
}
When I do writer.Generate() to make the new dll I get the following error: An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
当我执行 writer.Generate() 来制作新的 dll 时,我收到以下错误: mscorlib.dll 中发生了类型为“System.Runtime.Serialization.SerializationException”的未处理异常
Additional information: Type 'System.Windows.Media.Imaging.BitmapFrameDecode' in Assembly 'PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
附加信息:在程序集“PresentationCore,版本=3.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”中键入“System.Windows.Media.Imaging.BitmapFrameDecode”未标记为可序列化。
回答by Charlie
I don't know what you mean by "repackage it back into the DLL" but there is an easy way to convert a WPF image from a BitmapSource back into a System.Drawing.Image. The following method accomplishes that:
我不知道“将它重新打包回 DLL”是什么意思,但是有一种简单的方法可以将 WPF 图像从 BitmapSource 转换回 System.Drawing.Image。以下方法实现了这一点:
/// <summary>
/// Converts a WPF bitmap to a System.Drawing.Bitmap
/// </summary>
/// <param name="wpfBitmap">BitmapSource to convert</param>
/// <returns>A GDI Bitmap</returns>
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;
}