1

I'm desperately trying to set a writeable bitmap as the source of a secondary tiles image. I think I'm almost there but it refuses to work. Can anybody see what I'm missing? I would be VERY grateful!

I'm creating the bitmap using:

var bitmap = new WriteableBitmap(150, 150);
            Stream stream = bitmap.PixelBuffer.AsStream();
            stream.Seek(0, SeekOrigin.Begin);

            var pixels = new byte[stream.Length];
            for (int i = 0; i < pixels.Length; i += 4)
            {
                pixels[i] = 255;
                pixels[i + 1] = 0;
                pixels[i + 2] = 189;
                pixels[i + 3] = 9;
            }

            stream.Write(pixels, 0, pixels.Length);
            bitmap.Invalidate();

The image is saved to the computer with:

await WriteableBitmapSaveExtensions.SaveToFile(bitmap, ApplicationData.Current.LocalFolder,"image.png", CreationCollisionOption.ReplaceExisting);

The image can be found in the directory:

C:\Users\<USER>\AppData\Local\Packages\<PKGID>\LocalState 

I'm creating the secondary tile using this method:

CreateSecondaryTileFromWebImage("image.png", "tildId","shortName","displayName","arguments", MainPage.GetElementRect((FrameworkElement)sender));

public async Task CreateSecondaryTileFromWebImage(string bitmapName, string tileId, string shortName, string displayName, string arguments, Rect selection)
    {
        //Create uri
        var bitmap = new Uri(string.Format("ms-appdata:///local/{0}", bitmapName));

        //Create tile
        SecondaryTile secondaryTile = new SecondaryTile(tileId, shortName, displayName, arguments, TileOptions.ShowNameOnLogo, bitmap);

        //Confirm creation
        await secondaryTile.RequestCreateForSelectionAsync(selection, Windows.UI.Popups.Placement.Above);
    }

The tile is created and pinned to the startscreen but the image is 100% transparent.

user1567095
  • 480
  • 1
  • 5
  • 13
  • 1
    if you go to C:\Users\\AppData\Local\Packages\\LocalState you should be able to grab the precise file you wrote. See if what you think is there is there. You could even try assigning that file to a primary tile in a test VS project just to ensure that it's a valid tile image (size wise, etc.) – Jim O'Neil Apr 06 '13 at 06:49
  • Ok, so the attached code doesn't save the image correctly. However, the updated code does save the image correctly and it still fails. – user1567095 Apr 06 '13 at 13:09
  • HOWEVER. It won't display when I set it at the apps tile... It's a standard .png file that can be viewed in any image viewing problem. Any suggestions? – user1567095 Apr 06 '13 at 13:18
  • Problem solved. Question updated and answer provided. Thanks for your help, Jim. – user1567095 Apr 06 '13 at 13:29

1 Answers1

0

After correcting the code so it produced a valid .png file in the directory mentioned here: C:\Users\\AppData\Local\Packages\\LocalState

It turns out that it was a simple problem with how I was writing WriteableBitmap pixels. Although the WriteableBitmap data stream format is ARGB, the bytes need to be written backwards. In my question, I'm actually assigning every pixel to: BGRA = 255,0,189,9

Where the transparency byte is set to 9, making the image nearly 100% transparent and giving the appearance that it didn't load correctly.

Therefore, in order to get my desired color, I instead need to write:

var pixels = new byte[stream.Length];
        for (int i = 0; i < pixels.Length; i += 4)
        {
            pixels[i] = 9;
            pixels[i + 1] = 189;
            pixels[i + 2] = 0;
            pixels[i + 3] = 255;
        }
user1567095
  • 480
  • 1
  • 5
  • 13