Reputation: 23833
All, I am very new to Windows Phone and really don't know where to be gin with this one. I wan't to load some test images into my Windows phone application (for testing purposes only). On my machine I have some JPEGs that I would like to loade into a Canvas
which itself contains an Image
. I know you can load from Local Storage like this
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;
}
}
but how to get the image from my machine into isolated storage? Sorry, I am a real noob :[.
Upvotes: 1
Views: 1795
Reputation: 860
Suppose you have images inside a images folder in your application directory. Make sure you set Build Action to "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();
}
}
}
Add the following codes in App startup
private void Application_Launching(object sender, LaunchingEventArgs e)
{
Storage sg = new Storage();
sg.SaveFilesToIsoStore();
}
Upvotes: 1
Reputation: 4849
The easiest way is to use some sort of Isolated storage browser:
Upvotes: 2