0

我正在 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;
}}}

希望你们能帮助我。非常感谢。

4

1 回答 1

1

基本上,使用 Silverlight 来管理 Image 并不是一件容易的事。

无论运行您的应用程序的计算机的能力如何,无论如何您都会受到浏览器的限制,出于安全考虑,这将限制您的应用程序专用的 RAM 和处理器。(这取决于您的浏览器版本,但大约是 1Go 的已用 RAM)。

唯一的解决方案是优化您的内存管理(在托管语言中总是很棘手......):

  • 尽量避免你的新指令(重复使用最多的对象)

  • 一旦你不再需要一个对象,就将它的指针设置为空(让它自由地被垃圾收集器收集)

  • 在最后一个选项中,尝试在某个战略位置调用 GC.Collect() (但要非常小心,如果你调用它过于频繁,你的性能可能会急剧下降)

于 2013-02-07T08:35:46.567 回答