C# 加载本地映像以用于Windows 7 Phone应用程序测试
总之,我对Windows Phone非常陌生,真的不知道该在哪里使用它。我不想将一些测试图像加载到我的Windows phone应用程序中(仅用于测试目的)。在我的机器上,我有一些JPEG,我想把它们加载到C# 加载本地映像以用于Windows 7 Phone应用程序测试,c#,image,windows-phone-7,load,C#,Image,Windows Phone 7,Load,总之,我对Windows Phone非常陌生,真的不知道该在哪里使用它。我不想将一些测试图像加载到我的Windows phone应用程序中(仅用于测试目的)。在我的机器上,我有一些JPEG,我想把它们加载到画布,画布本身包含一个图像。我知道你可以像这样从本地存储加载 private void LoadFromLocalStorage(string imageFileName, string imageFolder) { var isoFile = IsolatedStorageFile.
画布
,画布本身包含一个图像
。我知道你可以像这样从本地存储加载
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;
}
}
但是如何将图像从我的机器中存储到独立的存储中呢?对不起,我是个真正的noob:[。最简单的方法是使用某种隔离存储浏览器:
假设应用程序目录中的图像文件夹中有图像。请确保将生成操作设置为“内容”
也许你正在试图做的是过分的。你不能添加一些图像到你的项目中,并将其分配到源属性吗?那将是很好的,但我可以找到一个使用JPEG的例子。如果你能提供一个很好的例子!你能扩展你的答案吗?我现在在使用lin加载图像时遇到一些问题e
streamresourceinfosr=Application.GetResourceStream(新Uri(f,UriKind.Relative));
?它正在将sr
设置为null
,我不知道为什么,因为我有像newURI(/Icons/appbar.questionmark.rest.png),UriKind.Relative)这样的行
?
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();
}
}
}
private void Application_Launching(object sender, LaunchingEventArgs e)
{
Storage sg = new Storage();
sg.SaveFilesToIsoStore();
}