[英]Loading a Local Image for use in Windows 7 Phone App Testing
总而言之,我对Windows Phone非常陌生,并且真的不知道在哪里杜松子酒。 我不想将一些测试图像加载到我的Windows手机应用程序中(仅用于测试目的)。 在我的机器上,我有一些JPEG,我想把它装入Canvas
,它本身包含一个Image
。 我知道你可以像这样从本地存储加载
private void LoadFromLocalStorage(string imageFileName, string imageFolder)
{
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoFile.DirectoryExists(imageFolder))
{
isoFile.CreateDirectory(imageFolder);
}
string filePath = Path.Combine(imageFolder, imageFileName);
using (var imageStream = isoFile.OpenFile(
filePath, FileMode.Open, FileAccess.Read))
{
var imageSource = PictureDecoder.DecodeJpeg(imageStream);
image.Source = imageSource;
}
}
但如何将我的机器中的图像转换为独立存储? 对不起,我是一个真正的菜鸟:[。
最简单的方法是使用某种隔离存储浏览器:
假设您的应用程序目录中的images文件夹中有图像。 确保将Build Action设置为“Content”
public class Storage
{
public void SaveFilesToIsoStore()
{
//These files must match what is included in the application package,
//or BinaryStream.Dispose below will throw an exception.
string[] files = {
"images/img1.jpg", "images/img2.png"
};
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (files.Length > 0)
{
foreach (string f in files)
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
SaveToIsoStore(f, data);
}
}
}
}
private void SaveToIsoStore(string fileName, byte[] data)
{
string strBaseDir = string.Empty;
string delimStr = "/";
char[] delimiter = delimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
//Get the IsoStore.
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
//Re-create the directory structure.
for (int i = 0; i < dirsPath.Length - 1; i++)
{
strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
//Remove the existing file.
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
//Write the file.
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
{
bw.Write(data);
bw.Close();
}
}
}
在App启动中添加以下代码
private void Application_Launching(object sender, LaunchingEventArgs e)
{
Storage sg = new Storage();
sg.SaveFilesToIsoStore();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.