如何将C#4位图反序列化为Silverlight 4位图图像 [英] How to deserialise a C# 4 bitmap to a Silverlight 4 Bitmap Image

查看:82
本文介绍了如何将C#4位图反序列化为Silverlight 4位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这似乎是一个棘手的问题.

我正在使用C#4将图像上传到数据库中.
我将它们序列化,然后将它们转换为byte [],以便可以将它们加载到"Image"类型字段中.
这很棒.

如果我从byte []转换回字符串并保存图像,则可以完美保存并且没有问题.

现在要注意的是,我需要在Silverlight应用程序中显示这些图像,以使它们在另一个应用程序(恰好是Silverlight应用程序)中使用.

现在,Silverlight没有位图类,只有位图图像.

当我尝试反序列化时,出现以下错误:

Hi
This seems to be quite a tricky one.

I am uploading images into a database using C# 4.

I serialize them, then convert them to byte[] so that they can be loaded into a "Image" type field.

This works great.

If I convert back from byte[] to string and save the image, it saves perfectly and there are no issues.

Now the catch is, I need to show these images in a silverlight application, s they get used in another application (which happens to be a silverlight application).

Now Silverlight doesn''t have a bitmap class, only a Bitmap image.

When I try and deserialize I get the following error:

Type ''System.Windows.Media.ImageSource'' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.<br />



我要序列化的代码:



My Code to Serialize:

byte[] newByte = new byte[0];
string fileName = System.IO.Path.GetFileName(s);
Guid CustomPinID = new Guid(Session["Upload"].ToString());
Bitmap myImage = new Bitmap(s);
oDataService.InsertCustomPinImage(CustomPinID, oDataService.GetMyBytes(SerializeMe(myImage)), 0.00, 0.00, fileName);

public string SerializeMe(object obj)
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
        serializer.WriteObject(memoryStream, obj);
        return Encoding.UTF8.GetString(memoryStream.ToArray());
    }
}

public byte[] GetMyBytes(string text)
{
    return ASCIIEncoding.UTF8.GetBytes(text);
}



我的代码将在下面反序列化:



My Code is to Deserialize below:

BitmapImage DisplayImage = new BitmapImage();
DisplayImage = (BitmapImage)(cSerialization.DeserializeMe(cSerialization.GetMyString(oPinImage.imagePath), typeof(BitmapImage)));

public class Serialization
{

    public string GetMyString(byte[] oArray)
    {
        return Encoding.UTF8.GetString(oArray, 0, oArray.Length);
    }

    public object DeserializeMe(string xml, Type toType)
    {
        using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(toType);
            return serializer.ReadObject(memoryStream);
        }
    }
}

推荐答案

要将字节数组转换为BitmapImage对象:

To convert a byte array to a BitmapImage object:

MemoryStream stream = new MemoryStream(bytearray);
BitmapImage image = new BitmapImage();
image.SetSource(stream);



此时,您可以将对象的Source属性设置为新的BitmapImage对象.



At this point, you can set your object''s Source property to your new BitmapImage object.

myImage.Source = image;


我在我的asp应用程序的Web服务中使用了常规反序列化.
I used normal deserializing in my webservice of my asp app.


这篇关于如何将C#4位图反序列化为Silverlight 4位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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