windows phone 中对图片的存储和还原显示

以下两个方法分别用来完成对图片的存储以及还原显示:

一:存储图片

        public static string ImageSave(string ISFileName, Image SLImage)
        {
            var ISAccErr = "独立存储文件系统访问失败。";
            var Rlt = "";
            var WBmp = new WriteableBitmap(SLImage.Source as BitmapSource);
            var Buf = new byte[2 * 4 + WBmp.Pixels.Length * 4];
            BitConverter.GetBytes(WBmp.PixelWidth).CopyTo(Buf, 0);
            BitConverter.GetBytes(WBmp.PixelHeight).CopyTo(Buf, 4);
            for (var I = 0; I < WBmp.Pixels.Length; I++)
            {
                BitConverter.GetBytes(WBmp.Pixels[I]).CopyTo(Buf, I * 4 + 8);
            }
            try
            {
                var FO = IsolatedStorageFile.GetUserStoreForApplication();
                var FS = FO.OpenFile(ISFileName, System.IO.FileMode.OpenOrCreate);
                FS.Write(Buf, 0, Buf.Length); FS.Close(); FS.Dispose();
            }
            catch
            {
                Rlt = ISAccErr;
            }
            return Rlt;
        }

 

二:还原显示图片

public static string ImageRestore(string ISFileName, Image SLImage)
        {
            var ISAccErr = "独立存储文件系统访问失败。";
            var ISFileErr = "独立存储文件格式错误。";
            var Rlt = ""; var Buf = null as byte[];
            try
            {
                var FO = IsolatedStorageFile.GetUserStoreForApplication();
                var FS = FO.OpenFile(ISFileName, System.IO.FileMode.OpenOrCreate);
                Buf = new byte[FS.Length]; FS.Read(Buf, 0, Buf.Length);
                FS.Close(); FS.Dispose();
            }
            catch
            {
                Rlt = ISAccErr;
            }
            if (Rlt == "")
            {
                if (!(Buf.Length >= 2 * 4))
                {
                    Rlt = ISFileErr;
                }
                else
                {
                    var PixelWidth = BitConverter.ToInt32(Buf, 0);
                    var PixelHeight = BitConverter.ToInt32(Buf, 4);
                    var WBmp = new WriteableBitmap(PixelWidth, PixelHeight);
                    if (!(Buf.Length >= (2 * 4 + WBmp.Pixels.Length * 4)))
                    {
                        Rlt = ISFileErr;
                    }
                    else
                    {
                        for (var I = 0; I < WBmp.Pixels.Length; I++)
                        {
                            WBmp.Pixels[I] = BitConverter.ToInt32(Buf, I * 4 + 8);
                        }
                        SLImage.Source = WBmp;
                    }
                }
            }
            return Rlt;
        }

另外:

在给图片赋值的时候,不能直接写成   ImgA=ImgB;

Image对象在定义的时候有一个SOURCE和STRETCH属性,在赋值的时候需要使用SOURCE属性来对Image对象赋值,例如:

ImageSource source=ImgB.source as ImageSource;

ImgA.source=source;

 

 

 

 

posted on 2011-11-17 10:28  Games  阅读(316)  评论(0编辑  收藏  举报