繁体   English   中英

如何在WP7上的SQL数据库中存储图像

[英]How to store images in an SQL database on WP7

我拼命想要将图像保存到SQL数据库,然后将其加载到我的WP上。 所有在线指南都说要将图像转换为字节数组,存储它然后将其加载回图像。

到目前为止,我已经能够使用以下方法将图像保存为Byte数组:

    public static byte[] ConvertToBytes(Stream photoStream)   
    {   
        byte[] a = new Byte[photoStream.Length];   
        for (int i = 0; i < photoStream.Length; i++)   
        {   
            a[i] = (Byte)photoStream.ReadByte();   
        }   
        return (a);
    } 

这会生成一个字节数组,其大小与我正在保存的图像相似。

建议的加载图像的方法是:

    1 public static BitmapImage ConvertToImage(Byte[] inputBytes)   
    2 {   
    3     MemoryStream stream = new MemoryStream(inputBytes);   
    4     BitmapImage image = new BitmapImage();   
    5     image.SetSource(stream);   
    6     return (image);   
    7 }  

这不起作用。

我收到此错误(第5行):“未指定的错误”

有没有人知道如何解决这个问题或建议替代方法/代码?

我知道网上有信息 - 我可以向你保证,我已经搜索了很长时间并且努力工作,并且无法正常工作。

任何帮助将非常感激!

我设法解决了这个问题:

public static byte[] ConvertToBytes(String imageLocation)
    {
        StreamResourceInfo sri = Application.GetResourceStream(new Uri(imageLocation, UriKind.RelativeOrAbsolute));
        BinaryReader binary = new BinaryReader(sri.Stream);

        byte[] imgByteArray = binary.ReadBytes((int)(sri.Stream.Length));

        binary.Close();
        binary.Dispose();
        return imgByteArray;
    }

    public static WriteableBitmap ConvertToImage(Byte[] inputBytes)
    {
        MemoryStream ms = new MemoryStream(inputBytes);
        WriteableBitmap img = new WriteableBitmap(400, 400);

        img.LoadJpeg(ms);

        return (img);
    }

谢谢你们的帮助。

我用过那段代码

  byte[] Arr = Convert.FromBase64String(Im); >> i think you need that steep 
 Stream memStream = new MemoryStream(Arr);

                                    WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);

                                    image2.Source = wbimg;
                                    image2.Tag = IlbumID;

如果那nat工作

尝试

memStream.Read (Arr ,0, Arr.Length );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM