WriteableBitmapEx GetPixel() returns wrong value

2019-07-04 03:12发布

问题:

I need to combine two color values from two WriteableBitmap objects and compute something with them. Therefore I run a ForEach loop on the first object and parse its color value and the color value of the second object into a method.

writeableBitmap.ForEach((x, y, color) => 
    MergePixelColor(color, mergedWriteableBitmap.GetPixel(x, y)));

The first value I get directly from the delegate, but to access the second color value I use the GetPixel method from the WriteableBitmap extension.

This should actually work just like that, but it seems that the GetPixel method returns wrong data (the colors are somehow incorrectly "yellow-ish" or "red-ish").

I looked up and found the following article:

http://forums.silverlight.net/t/250392.aspx/1?WriteableBitmap+GetPixel+

There it is mentioned that there might be a problem with the image format. My Problem is though that I do not have direct access to the point where the images are generated. I extract them from a webservice and I dont know if that part can be adapted (at least not from me).

My question is now, if there is any other way or workaround to fix this issue? Do you have any ideas?

回答1:

This solution might just be a workaround, but I couldnt come up with something better in my given time. I just loop the mergedWriteableBitmap beforehand and save its color values into a dictionary:

IDictionary<int, Color> mergedWriteableBitmapMapping = new Dictionary<int, Color>();
mergedWriteableBitmap.ForEach((x, y, color) =>
{
    int index = GetIndex(x, y, mergedWriteableBitmap.PixelWidth);
    mergedWriteableBitmapMapping.Add(index, color);
    return color;
});

Afterwards I use this dictionary values to parse the correct color values into the method:

writeableBitmap.ForEach((x, y, color) => 
    MergePixelColor(color, mergedWriteableBitmapMapping[GetIndex(x, y, mergedWriteableBitmap.PixelWidth)]));