Reputation: 3
I'm creating an application in Silverlight that saves images in isolated storage. I managed to save images in isolated storage but I'm having trouble by loading and displaying the image.
Here is the code:
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;
}}}
Hope you guys can help me. Thanks a lot.
Upvotes: 0
Views: 235
Reputation: 3043
Basically, use Silverlight to manage Image is not an easy task.
Whatever the power of the computer running your application, you are anyway limited by the browser, which by security will limit the RAM and processor dedicated to your application. (it will depend of the version of your browser, but it is roughly around 1Go of used RAM).
The only solution is to otpimize your memory management (always tricky in a managed language...):
Try to avoid your new instruction (reuse a maximum of object)
As soon as you do not need an object anymore, set its pointer to null (to keep it free to be collect by the garbage collector)
In a last option, try to call GC.Collect() in some strategic place (but be very carefull with that, your performance could dramatically decreased if you call it too often)
Upvotes: 1