0
votes

I'm updating my app for windows phone 8.1 and I want to create a transparent live tile. I'm using The ToolStack PNG library for creating png images and it works fine in first look ! I am using the code below in background task

private void CreateWideTile()
    {
        int width = 691;
        int height = 336;
        string imagename = "WideBackground";

        WriteableBitmap b = new WriteableBitmap(width, height);

        var canvas = new Grid();
        canvas.Width = b.PixelWidth;
        canvas.Height = b.PixelHeight;

        var textBlock = new TextBlock();
        textBlock.Text = "example text...";
        textBlock.Margin = new Thickness(10, 55, 0, 0);
        textBlock.Width = 140;
        textBlock.Foreground = new SolidColorBrush(Colors.White);
        textBlock.FontSize = 30;

        canvas.Children.Add(textBlock);

        b.Render(canvas, null);
        b.Invalidate();

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
                store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
            var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
            b.WritePNG(stream);
            stream.Close();
        }

    }

    private void CreateMediumTile()
    {
        int width = 336;
        int height = 336;
        string imagename = "MediumBackground";

        WriteableBitmap b = new WriteableBitmap(width, height);

        var canvas = new Grid();
        canvas.Width = b.PixelWidth;
        canvas.Height = b.PixelHeight;

        var textBlock = new TextBlock();
        textBlock.Text = "example text...";
        textBlock.Margin = new Thickness(10, 55, 0, 0);
        textBlock.Width = 140;
        textBlock.Foreground = new SolidColorBrush(Colors.White);
        textBlock.FontSize = 30;

        canvas.Children.Add(textBlock);

        b.Render(canvas, null);
        b.Invalidate(); 

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
                store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
            var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
            b.WritePNG(stream);
            stream.Close();
        }

    }

    private void CreateSmallTile()
    {
        int width = 159;
        int height = 159;
        string imagename = "SmallBackground";

        WriteableBitmap b = new WriteableBitmap(width, height);

        var canvas = new Grid();
        canvas.Width = b.PixelWidth;
        canvas.Height = b.PixelHeight;

        var textBlock = new TextBlock();
        textBlock.Text = "example text...";
        textBlock.Margin = new Thickness(10, 55, 0, 0);
        textBlock.Width = 140;
        textBlock.Foreground = new SolidColorBrush(Colors.White);
        textBlock.FontSize = 30;

        canvas.Children.Add(textBlock);

        b.Render(canvas, null);
        b.Invalidate();

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
                store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
            var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
            b.WritePNG(stream);
            stream.Close();
        }

    }

as I mentioned, this code works fine but after 5 times run, the background task will be terminated and won't run anymore...I'm really confused. I've searched all the internet and I just guess the problem is because of memory leak. but I don't know how to solve this confusing problem. I also used GC.Collect(); but it didn't help at all. please please give some advice

1

1 Answers

0
votes

I also use the ToolStack to generate the transparent live tile in WP8.1, but use the method ExtendedImage.WriteToStream().