This project is read-only.

Color issues with semitransparent data

Aug 27, 2009 at 11:40 PM

I found an issue with encoding images that contains semi-transparency. The colors are off compared to the output from WriteableBitmap. If I add two images to the Silverlight Control, one with the data from WriteableBitmap, and the other with data from the PngEncoder, this is what I get:

These are basically just semitransparent yellow and orange ellipses on a transparent background. Also note the issues with the outline around the red circles (they are red ellipses with a white outline).

As far as I can tell this is not specific to the PNG encoder. I was seeing the same thing with the JpgEncoder (although I really need to preserve the semi-transparency).

Aug 28, 2009 at 9:35 AM
do you have a sample project, which I can use for testing?
Aug 28, 2009 at 6:08 PM
Edited Aug 28, 2009 at 6:10 PM

Yes. Get it here. I'm using the latest source (Changeset 27034).

Sep 3, 2009 at 9:14 PM
Thanks for this excellent sample. I could fix it, but I dont have team
explorer yet and cannot update the source at the moment (just have a
very slow mobile internet connection...).

But nevertheless here is the code:

public static ImageTools.Image ToImage(this WriteableBitmap bitmap)
Guard.NotNull(bitmap, "bitmap");


ImageTools.Image image = new
ImageTools.Image(bitmap.PixelWidth, bitmap.PixelHeight);
for (int y = 0; y < bitmap.PixelHeight; ++y)
for (int x = 0; x < bitmap.PixelWidth; ++x)
int pixel = bitmap.Pixels[bitmap.PixelWidth * y
+ x];

byte a = (byte)((pixel >> 24) & 0xFF);

float aFactor = a / 255f;

byte r = (byte)(((pixel >> 16) & 0xFF) / aFactor);
byte g = (byte)(((pixel >> 8) & 0xFF) / aFactor);
byte b = (byte)((pixel & 0xFF) /

image.SetPixel(x, y, r, g, b, a);
catch (System.Security.SecurityException e)
throw new ArgumentException("Bitmap cannot accessed", e);

return image;

I can also send you the precompiled Utils dll if you want.
Sep 4, 2009 at 2:14 AM

Thanks! Works like a charm !