如何将SL的image保存到SL的独立存储文件系统

/// <summary>
/// 将SL的image保存到独立存储文件系统
/// TFSoft - 2011/06/07
/// </summary>
/// <param name="ISFileName">独立存储文件名</param>
/// <param name="SLImage">需要保存的图片源(Silverlihgt image 组件引用)</param>
/// <returns>无错返回空串,出错返回出错信息</returns>
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;
}

/// <summary>
/// 将保存在独立存储文件系统中的数据恢复到SL的image中
/// TFSoft - 2011/06/07
/// </summary>
/// <param name="ISFileName">独立存储文件名</param>
/// <param name="SLImage">恢复到图片源(Silverlihgt image 组件引用)</param>
/// <returns>无错返回空串,出错返回出错信息</returns>
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;
}


1)如何将SL的image保存到SL的独立存储文件系统;

2)如何从SL的独立存储区保存的图片数据恢复到SL的image中。

如果这两个,能做到,你的要求一定能达到。无非是加一个ValueConvertor。

 

你可能感兴趣的:(image)