位图转换为图像 [英] Convert Bitmap to Image

查看:238
本文介绍了位图转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我拉的图像出来后的DLL并把它放到一个图像控件是一个BitmapImage的。打包它放回它必须被转换回图像的DLL。我怎样才能将其转换回图像,我该如何重新包装回该dll?这是所有在用C#WPF。

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#.

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);
    }

当我这样做writer.Generate(),使新的dll我得到的以下错误:
型'System.Runtime.Serialization.SerializationException'未处理的异常出现在mscorlib.dll

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

更多信息:类型'System.Windows。 Media.Imaging.BitmapFrameDecode汇编PresentationCore,版本= 3.0.0.0,文化=中性公钥= 31bf3856ad364e35'未标记为可序列化。

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.

推荐答案

我不知道你是什么意思改头换面它放回DLL但有一个简单的方法来一个WPF图像从的BitmapSource转换回一个System.Drawing.Image对象。下面的方法来完成的:

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:

/// <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;
}

这篇关于位图转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆