反序列化WindowsForms ImageList

时间:2018-10-04 10:47:40

标签: c# winforms deserialization imagelist

我有一些包含各种System.Windows.Forms.ImageList的resx文件。

有没有办法通过resx从图像中提取图像-我不想从我的项目代码中获得对图像列表的引用。

到目前为止,我已经设法反序列化base64字符串。

我得到了。像这样:

WSystem.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089&System.Windows.Forms.ImageListStreamer
Data    <MSFtIL
BM66(@ [...]

所以我想这是一个base64类型的System.Windows.Forms.ImageListStreamer编码对象。但是该类是密封的,我无法设法从该resx中获取图像。

由于 VisualStudio 设法从中取出图像,因此必须采取任何方法。

这是一个示例resx。 (Form1.resx)-您基本上可以通过添加新的Windows表单然后添加仅包含一张图像的图像列表来创建自己的图像。

[...]
<data name="imageList1.ImageStream" mimetype="application/x-microsoft.net.object.binary.base64">
    <value>
    AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
    LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
    ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAA8
    CAAAAk1TRnQBSQFMAwEBAAEIAQABCAEAARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMA
    AUADAAEQAwABAQEAAQgGAAEEGAABgAIAAYADAAKAAQABgAMAAYABAAGAAQACgAIAA8ABAAHAAdwBwAEA
    AfABygGmAQABMwUAATMBAAEzAQABMwEAAjMCAAMWAQADHAEAAyIBAAMpAQADVQEAA00BAANCAQADOQEA
    AYABfAH/AQACUAH/AQABkwEAAdYBAAH/AewBzAEAAcYB1gHvAQAB1gLnAQABkAGpAa0CAAH/ATMDAAFm
    AwABmQMAAcwCAAEzAwACMwIAATMBZgIAATMBmQIAATMBz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lMBdQEaARsBGgH/OAAB9QEbAf8BAAHz
    AXUBUwEbAf85AAH1AZoBUwGaAfUBdQIaAf82AAH0AVMBdQH0AQABmgFTAfMBUwH2NgAB/wEbAf8BAAHz
    AVMB8wEbAVMB/zkAAf8BUwGaAQABGgF1OgABmgF1Af8BAAKaOgAB/wH1AgABUwEaPQAB/wGaAfUxAAFC
    AU0BPgcAAT4DAAEoAwABQAMAARADAAEBAQABAQUAAYAXAAP/AQAB4AEDBgAB4AEDBgAB4AEDBgAB4AED
    BgAB4AEDBgAB4AEDBgAB5AEDBgAB+AEHBgAB+AGDBgAB/gcAAfwBIAYAAfwBQAYAAf8BiQYAAf8BiQYA
    Af8BmQYAAf8B8QYACw==
    </value>
 </data>
 [...]

我也可以在resx中找到它:

mimetype: application/x-microsoft.net.object.binary.base64
value   : The object must be serialized with 
        : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        : and then encoded with base64 encoding.

编辑1

现在,我已经设法获取ImageListStreamer对象。遗憾的是,其ImageList属性仍为null

//File containing only the value of the imagelist
string source = @"C:\Users\fd\source\repos\Trials\WindowsFormsApp1\imagelistsource.txt";

BinaryFormatter formatter = new BinaryFormatter();

string base64 = File.ReadAllText(source);
byte[] bytes = Convert.FromBase64String(base64);

using (Stream stream = new MemoryStream(bytes))
{
   ImageListStreamer streamer = (ImageListStreamer)formatter.Deserialize(stream);
 }

1 个答案:

答案 0 :(得分:1)

//File containing only the value of the imagelist
string source = @"C:\Users\fd\source\repos\Trials\WindowsFormsApp1\imagelistsource.txt";

BinaryFormatter formatter = new BinaryFormatter();

string base64 = File.ReadAllText(source);
byte[] bytes = Convert.FromBase64String(base64);

using (Stream stream = new MemoryStream(bytes))
{
    ImageListStreamer streamer = (ImageListStreamer)formatter.Deserialize(stream);

    //streamer.ImageList is actually null BUT that does not matter at all.

    ImageList list = new ImageList();
    list.ImageStream = streamer;

    //list is now filled with all the images !
   foreach (Bitmap image in list.Images)
   {
       //Got my Bitmap YAY !             
   }

}