如何在二进制序列化类中存储任意二进制数据?

发布于 2024-09-28 00:01:01 字数 1423 浏览 4 评论 0原文

在我的应用程序中使用 C#/.NET,我的主数据模型中有一系列表示“二进制”(而不是文本)内容的类。我有这样的继承设置:

alt text

基本上,抽象类 BinaryContent 包含存储任意二进制数据的MemoryStream。该数据是从磁盘上的文件中读取的。我计划存储的每种类型的二进制数据都将是派生类型,例如 ImageContentFontContent。这些派生类型将解释 BinaryContent.Content 中的二进制数据。例如,ImageContent 将从 MemoryStream 创建一个 BitmapImage(存储在 ImageSource 中)。 FontContent 当然会从 BinaryContent.Content 创建字体。我选择这种做事方式是因为我希望能够基本上存储内容文件(即图像)的副本,而不必一次又一次依赖文件位于磁盘上的特定位置。

我还使用二进制序列化将这些类的实例存储在“项目文件”中。我这样做基本上是为了将所有内容“打包”在一起。当我尝试反序列化 MemoryStream 时,我似乎遇到了麻烦。当我从 MemoryStream 创建图像时出现问题。当反序列化后运行以下方法时,会发生 FileFormatException

private void RefreshImageFromContent()
{
    BitmapImage image = null;
    if (Content != null &&
        Content.Length != 0L)
    {
        image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = Content;
        image.EndInit(); //throws FileFormatException
    }

    Image = image;
}

FileFormatException 消息是: “图像无法解码。图像标头可能已损坏。” 内部异常:“来自 HRESULT 的异常:0x88982F61”

我现在最好的猜测是,在序列化或反序列化期间,发生了一些事情,导致 BinaryContent.Content 中的数据被损坏。

这让我问两个问题。

  1. 有人有任何建议来解决这个问题吗?
  2. 有没有人有其他建议的方法来存储将要(反)序列化的任意二进制数据?

请随时要求澄清有关我的问题的任何内容。

谢谢。

Using C#/.NET for my application, I've got a series of classes in my main data model that represent "binary" (as opposed to text) content. I've got a inheritance setup like this:

alt text

Basically, the abstract class BinaryContent contains a MemoryStream that stores arbitrary binary data. That data is read from a file on disk. Each type of binary data I plan to store will be a derived type, such as ImageContent and FontContent. Those derived types will interpret the binary data in BinaryContent.Content. ImageContent for example, would create a BitmapImage (stored in an ImageSource) from the MemoryStream. FontContent would of course create a font from the BinaryContent.Content. I chose this way of doing things because I wanted to be able to basically store a copy of the content file (ie an image) and not have to rely on the file being in a particular location on disk time after time.

I'm also storing instances of these classes in a "project file" using binary serialization. I've done this to basically "package" everything together. I'm having trouble when I attempt to deserialize the MemoryStream, it seems. The problem happens when I create the image from the MemoryStream. When the following method runs after deserialization, a FileFormatexception occurs.

private void RefreshImageFromContent()
{
    BitmapImage image = null;
    if (Content != null &&
        Content.Length != 0L)
    {
        image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = Content;
        image.EndInit(); //throws FileFormatException
    }

    Image = image;
}

The FileFormatException message is:
"The image cannot be decoded. The image header might be corrupted."
Inner Exception: "Exception from HRESULT: 0x88982F61"

My best guess right now is that something is happening to corrupt the data in BinaryContent.Content when during serialization or deserialization.

This leads me to ask 2 questions.

  1. Does anyone have any suggestions to fix this problem?
  2. Does anyone have other suggested ways to store arbitrary binary data that is going to be (de)serialized?

Please feel free to ask for clarification on anything about my question.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-10-05 00:01:01

image.StreamSource = Content; 处的 content.Position 是什么?

流的位置可能未设置为开始(或流中的正确位置)。

What is the content.Position at image.StreamSource = Content;?

It is likely that the position of the stream is not set to the start (or the correct position in thw stream).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文