-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImages.cs
89 lines (79 loc) · 3.45 KB
/
Images.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Windows.Storage;
namespace StoreMediaLib
{
/// <summary>
/// Provides methods to save and load images in isolated storage.
/// </summary>
public static class Images
{
/// <summary>
/// Saves image stream in the specified folder.
/// </summary>
/// <param name="imageToBeSaved">Represents stream with image.</param>
/// <param name="folderName">Desireble folder name.</param>
/// <param name="fileName">Name of the file to create.</param>
public static void SaveImage(Stream imageToBeSaved, string folderName, string fileName)
{
using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
{
if(!local.DirectoryExists(folderName))
local.CreateDirectory(folderName);
SaveImage(imageToBeSaved, folderName + "\\" + fileName);
}
}
/// <summary>
/// Saves stream with image to the specified path.
/// </summary>
/// <param name="imageToBeSaved">Represents stream with image.</param>
/// <param name="fileName">The relative path of the file to create.</param>
private static void SaveImage(Stream imageToBeSaved, string fileName)
{
using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoFileStream = local.CreateFile(fileName + ".jpg"))
{
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = imageToBeSaved.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
isoFileStream.Write(readBuffer, 0, bytesRead);
}
}
}
}
/// <summary>
/// Loads image by relative path.
/// </summary>
/// <param name="fileName">Relative path to the file in isolated storage.</param>
/// <returns>Image as a Bitmap object.</returns>
public static BitmapImage LoadImage(string fileName)
{
BitmapImage bmpImage = new BitmapImage();
using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
{
if(local.FileExists(fileName))
using (IsolatedStorageFileStream file = local.OpenFile(fileName, FileMode.Open))
{
bmpImage.SetSource(file);
}
}
return bmpImage;
}
/// <summary>
/// Loads all images in directory by relative path.
/// </summary>
/// <param name="folderName">Relative path to the folder</param>
/// <returns>Collection of Bitmap object.</returns>
public static List<BitmapImage> LoadImagesFromFolder(string folderName)
{
List<BitmapImage> retreivedImages = new List<BitmapImage>();
foreach (var file in Directory.GetFiles(ApplicationData.Current.LocalFolder.Path + "\\" + folderName))
retreivedImages.Add(LoadImage(file));
return retreivedImages;
}
}
}