分離されたストレージに画像を保存するアプリケーションをSilverlightで作成しています。隔離されたストレージに画像を保存できましたが、画像を読み込んで表示するのに問題があります。
コードは次のとおりです。
public partial class MainPage : UserControl
{
private const string ImageName = "google1.png";
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap bitmap = new WriteableBitmap(saveImage, new TransformGroup());
loadedImage.Source = bitmap;
imageToStore(saveBuffer(bitmap), ImageName);
MessageBox.Show("saved");
}
public void imageToStore(byte[] buffer, string filename)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream s = new IsolatedStorageFileStream(filename, FileMode.Create, iso);
Int64 freeSpace = iso.AvailableFreeSpace;
Int64 needSpace = 20971520; // 20 MB in bytes
if (freeSpace < needSpace)
{
if (!iso.IncreaseQuotaTo(iso.Quota + needSpace))
{ MessageBox.Show("User rejected increase spacerequest");
}
else { MessageBox.Show("Space Increased");
}
}
using (StreamWriter writer = new StreamWriter(s))
{
writer.Write(buffer);
}
}
}
private static byte[] saveBuffer(WriteableBitmap bitmap)
{
long matrixSize = bitmap.PixelWidth * bitmap.PixelHeight;
long byteSize = matrixSize * 4 + 4;
byte[] retVal = new byte[byteSize];
long bufferPos = 0;
retVal[bufferPos++] = (byte)((bitmap.PixelWidth / 256) & 0xff);
retVal[bufferPos++] = (byte)((bitmap.PixelWidth % 256) & 0xff);
retVal[bufferPos++] = (byte)((bitmap.PixelHeight / 256) & 0xff);
retVal[bufferPos++] = (byte)((bitmap.PixelHeight % 256) & 0xff);
return retVal;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
byte[] buffer = _LoadIfExists(ImageName);
loadedImage.Source = _GetImage(buffer);
MessageBox.Show("loaded");
}
private static byte[] _LoadIfExists(string fileName)
{
byte[] retVal;
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(fileName))
{
using (IsolatedStorageFileStream stream = iso.OpenFile(fileName, FileMode.Open))
{
retVal = new byte[stream.Length];
stream.Read(retVal, 0, retVal.Length);
stream.Close();
}
}
else
{
retVal = new byte[0];
}
}
return retVal;
}
private static WriteableBitmap _GetImage(byte[] buffer)
{
int width = buffer[0] * 256 + buffer[1];
int height = buffer[2] * 256 + buffer[3];
long matrixSize = width * height;
//this is the section where Exception of type 'System.OutOfMemoryException' was thrown.
WriteableBitmap retVal = new WriteableBitmap(width, height);
int bufferPos = 4;
for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
{
int pixel = buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
retVal.Pixels[matrixPos] = pixel;
}
return retVal;
}}}
皆さんが私を助けてくれることを願っています。どうもありがとう。