001.public partial class MainPage
002.{
003. private const string SAVEDIMG = "SavedBitmapImage.xyz";
004.
005. /// <summary>
006. /// Default constructor
007. /// </summary>
008. public MainPage()
009. {
010. InitializeComponent();
011. CanvasSource.Loaded += _MainPageLoaded;
012. }
013.
014. /// <summary>
015. /// Canvas loaded
016. /// </summary>
017. /// <param name="sender"></PARAM>
018. /// <param name="e"></PARAM>
019. void _MainPageLoaded(object sender, RoutedEventArgs e)
020. {
021. Status.Text = "Checking disk...";
022. byte[] buffer = _LoadIfExists(SAVEDIMG);
023.
024. if (buffer.Length > 0)
025. {
026. Status.Text = "Loading...";
027. BitmapImageRef.Source = _GetImage(buffer);
028. Status.Text = "Loaded.";
029. }
030. else
031. {
032. Status.Text = "Rendering...";
033. WriteableBitmap bitmap = new WriteableBitmap(CanvasSource, new TransformGroup());
034. BitmapImageRef.Source = bitmap;
035. Status.Text = "Saving...";
036. _SaveToDisk(_GetSaveBuffer(bitmap), SAVEDIMG);
037. Status.Text = "Saved.";
038. }
039. }
040.
041. /// <summary>
042. /// Load file, if it exists
043. /// </summary>
044. /// <param name="fileName">The filename</param>
045. /// <returns>The byte array for the file</returns>
046. private static byte[] _LoadIfExists(string fileName)
047. {
048. byte[] retVal;
049.
050. using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
051. {
052. if (iso.FileExists(fileName))
053. {
054. using (IsolatedStorageFileStream stream = iso.OpenFile(fileName, FileMode.Open))
055. {
056. retVal = new byte[stream.Length];
057. stream.Read(retVal, 0, retVal.Length);
058. }
059. }
060. else
061. {
062. retVal = new byte[0];
063. }
064. }
065. return retVal;
066. }
067.
068. /// <summary>
069. /// Saves to isolated storage
070. /// </summary>
071. /// <param name="buffer">The buffer</param>
072. /// <param name="fileName"></param>
073. private static void _SaveToDisk(byte[] buffer, string fileName)
074. {
075. using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
076. {
077. using (
078. IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.CreateNew,
079. iso))
080. {
081. stream.Write(buffer, 0, buffer.Length);
082. }
083. }
084. }
085.
086. /// <summary>
087. /// Gets an image from storage
088. /// </summary>
089. /// <param name="buffer"></param>
090. /// <returns>The bitmap</returns>
091. private static WriteableBitmap _GetImage(byte[] buffer)
092. {
093. int width = buffer[0]*256 + buffer[1];
094. int height = buffer[2]*256 + buffer[3];
095.
096. long matrixSize = width*height;
097.
098. WriteableBitmap retVal = new WriteableBitmap(width, height);
099.
100. int bufferPos = 4;
101.
102. for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
103. {
104. int pixel = buffer[bufferPos++];
105. pixel = pixel << 8 | buffer[bufferPos++];
106. pixel = pixel << 8 | buffer[bufferPos++];
107. pixel = pixel << 8 | buffer[bufferPos++];
108. retVal.Pixels[matrixPos] = pixel;
109. }
110.
111. return retVal;
112. }
113.
114. /// <summary>
115. /// Gets the buffer to save to disk from the writeable bitmap
116. /// </summary>
117. /// <param name="bitmap">The bitmap image</param>
118. /// <returns>The buffer of bytes</returns>
119. private static byte[] _GetSaveBuffer(WriteableBitmap bitmap)
120. {
121. long matrixSize = bitmap.PixelWidth*bitmap.PixelHeight;
122.
123. long byteSize = matrixSize*4 + 4;
124.
125. byte[] retVal = new byte[byteSize];
126.
127. long bufferPos = 0;
128.
129. retVal[bufferPos++] = (byte) ((bitmap.PixelWidth / 256) & 0xff);
130. retVal[bufferPos++] = (byte) ((bitmap.PixelWidth % 256) & 0xff);
131. retVal[bufferPos++] = (byte) ((bitmap.PixelHeight / 256) & 0xff);
132. retVal[bufferPos++] = (byte) ((bitmap.PixelHeight % 256) & 0xff);
133.
134. for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
135. {
136. retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 24) & 0xff);
137. retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 16) & 0xff);
138. retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 8) & 0xff);
139. retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos]) & 0xff);
140. }
141.
142. return retVal;
143. }
144.}