zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    数据结构与算法 ||设计模式
    前端页面知识点
    easyui的dialog中的输入框在关闭后如何清空输入框中的内容
    设计模式之单例模式(通俗易懂,超详细)
    分布式锁--Java1234
    spring cloud Alibaba
    easyui
    SQL查询最大或最新的一条数据
    java中的异常(exception、throw、throws、try···catch)
    git error: The following untracked working tree files would be overwritten by merge:
  • 原文地址:https://www.cnblogs.com/mokey/p/2252234.html
Copyright © 2011-2022 走看看