【问题标题】:Deserialize bitmap image and compare it with images in resources反序列化位图图像并将其与资源中的图像进行比较
【发布时间】:2013-12-12 18:40:33
【问题描述】:

我有一个 datagridview 使用的 BindingList。我的 datagridview 中的一列显示代表成功、失败或未知的 3 个图像之一。这些图片是从我的资源中指定的。

我能够使用内存流成功地序列化/反序列化位图。但是,一旦反序列化回位图,我无法确定它是 3 个图像中的哪一个。有没有办法做到这一点?基本上我想知道:

if(deserializedImage == resources.successImage)

我很乐意采用另一种方法:从技术上讲,我不需要序列化图像,我只需要序列化某种告诉它使用哪个资源的引用。诀窍是将其反序列化为一个位图,我可以与我的资源文件进行比较。

这是我的绑定列表:

public BindingList<Iam> DatagridList { get; set; }  

这是我的 Iam 对象的一个​​大大简化的版本(我已经删除了我的内存流代码):

public class Iam
{
   public string AssemblyPath { get; set; }
   public Bitmap UpToDate { get; set; }
}

总之,我怎样才能序列化位图,但反序列化它的方式可以将它与我的资源中的图像进行比较以确定使用了哪个?

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    我最终只存储了一个字符串来表示要显示的图像。 Bitmap 属性仅引用要使用的字符串。我从不序列化位图。

    [XmlIgnore]
    public Bitmap UpToDate
    {
        get
        {
            //Only null if file entered is invalid. This causes image to be blank
            if (UpToDateString == null) 
                return null;
    
            //Translates from the resource specified to the bitmap object being used
            var resourceObject = Resources.ResourceManager.GetObject(UpToDateString);
            return (Bitmap) resourceObject;
        }
    }
    
    //This holds the reference for which resource image to use
    [XmlElement("UpToDate")]
    public string UpToDateString { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2019-04-16
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多