0

Is it possible to add transparency to a WriteableBitmap in Windows Phone 8.1 using C# / WinRT programmatically?

I've referenced the WriteableBitmapEx library in my project for image resizing and blitting, which is quite useful, and it could also possibly help with adding transparency or alpha channels/layers, but the documentation is not very thorough.

It might be better to have a method without using the WriteableBitmapEx library to achieve this, but whatever works... I've been searching this for a while and there's not much information on a basic solution.

The idea is that a user can select an image or graphic, and pin it to their start screen, and the app would make the background color transparent...probably white or black, or even by using the first X/Y coordinate color of the image's background.

theMaxx
  • 3,756
  • 2
  • 27
  • 33

2 Answers2

0

Have you tried using the transparent colour with your WriteableBitmapEx? For example to clear the entire bitmap use:

writeableBitmap = new WriteableBitmap(200, 200, 96, 96, PixelFormats.Bgra32, null);
writeableBitmap.Clear(Colors.Transparent);
YetMoreStuff
  • 930
  • 1
  • 9
  • 16
  • That makes the entire image transparent. I want to make only one specific color transparent...the background color. – theMaxx Sep 15 '16 at 12:01
0

If you want to edit specific pixels yourself - you need to access the PixelBuffer property. I'm sure WriteableBitmapEx does it all the time. So does my toolkit here. I can't recall the pixel format right now, but I think it's like ARGB or P-ARGB? I'm likely confused here, but basically your pixel buffer is an array that has data for all the row of pixels with each pixel defined by 4 bytes, so a 2x2 bitmap will have something like ARGBARGBARGBARGB bytes where each letter is one of four pixel component values (0-255 with 0 for no color and 255 for all RGB being white). R is Red, G is Green, B is Blue and A is Alpha - opacity or transparency. In P- or Premultiplied- pixel formats - all of RGB values are additionally multiplied by the Alpha channel value for performance reasons (IIRC).

You'll need to loop through all your pixels and set the ARGB values according to your rules to add transparency where needed, possibly un-pre-multiplying and pre-multiplying RGB as needed.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100