使用WriteableBitmapEx渲染TextBlock时出现OutOfMemoryException

时间:2014-03-06 09:11:46

标签: c# windows-phone-8

我再次与OutOfMemoryException打架。

我有一段代码片段,可以在Windows Phone后台代理任务中使用WriteableBitmapEx来呈现一些图像(内存使用限制方法为10M)。

以下工作正常:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    var wbBG = BitmapFactory.New(0, 0);
    var bmp = BitmapFactory.New(0, 0);
    for (int i = 0; i < 30; i++)
    {
        using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            wbBG = BitmapFactory.New(0, 0).FromContent("Assets/image" + i + ".jpg");                          
            wbBG.Invalidate();

            for (int j = 0; j < 6; j++)
            {
                bmp = BitmapFactory.New(0, 0).FromContent("Assets/" + j + ".png");
                bmp = bmp.Resize(60, 60, WriteableBitmapExtensions.Interpolation.Bilinear);

                wbBG.Blit(new Rect(j * 65, 0, 60, 60), bmp, new Rect(0, 0, 60, 60));
                wbBG.Invalidate();
            }
            string filenameBG = "/Shared/" + i + ".jpg";
            using (var stream = iso.CreateFile(filenameBG))
            {
                wbBG.SaveJpeg(stream, 480, 800, 0, 85);
                stream.Close();
            }
            wbBG = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
    NotifyComplete();
});

但是,如果我在循环中添加或更改以使用TextBlock,则会在OutOfMemoryException的第二个循环中失败:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    var wbBG = BitmapFactory.New(0, 0);
    TextBlock tb;
    for (int i = 0; i < 30; i++)
    {
        using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            wbBG = BitmapFactory.New(0, 0).FromContent("Assets/image" + i + ".jpg"); 
                            //The above line would thrown OutOfMemoryException in the 2nd loop                       
            wbBG.Invalidate();

            for (int j = 0; j < 6; j++)
            {
                tb = new TextBlock(){
                    Text = j.ToString(),
                    //FontSize = 13,
                    //Height = 20,
                    //Width = 240,
                    //FontWeight = System.Windows.FontWeights.Bold,
                    //HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                    //Foreground = new SolidColorBrush(Colors.White)
                };
                wbBG.Render(tb, new TranslateTransform() { X = j*65, Y = 350 });
                wbBG.Invalidate();
                tb = null;
            }
            string filenameBG = "/Shared/" + i + ".jpg";
            using (var stream = iso.CreateFile(filenameBG))
            {
                wbBG.SaveJpeg(stream, 480, 800, 0, 85);
                stream.Close();
            }
            wbBG = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
    NotifyComplete();
});

知道为什么TextBlock会导致更多内存使用?

此外,我没有看到有更好的方法来渲染图像上的文字。

TextBlock也不是IDisposable

嗯,这是我的意见,可能是错的,我会感激任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道这个库但是你忘了在它们不再使用时释放资源所以试试这个:

        for (var i = 0; i < 30; i++)
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var wbBG = BitmapFactory.New(0, 0).FromContent("Assets/image" + i + ".jpg"))
                {
                    wbBG.Invalidate();

                    for (int j = 0; j < 6; j++)
                    {
                        var tb = new TextBlock()
                        {
                            Text = j.ToString(),
                            //FontSize = 13,
                            //Height = 20,
                            //Width = 240,
                            //FontWeight = System.Windows.FontWeights.Bold,
                            //HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                            //Foreground = new SolidColorBrush(Colors.White)
                        };
                        wbBG.Render(tb, new TranslateTransform() { X = j * 65, Y = 350 });
                        wbBG.Invalidate();
                    }
                    using (var stream = iso.CreateFile("/Shared/" + i + ".jpg"))
                    {
                        wbBG.SaveJpeg(stream, 480, 800, 0, 85);
                    }
                }
            }
        }