Question

I have tried, literally for hours, and I have not been able to budge this problem.

I have a UserControl, that is 800x369, and it contains, simply, a path that forms a worldmap. I put this on a landscape page, then I render it into a WriteableBitmap. I then run a conversion to turn the 1d Pixels array into a 2d array of integers. Then, to check the conversion, I wire up the custom control's click command to use the Point.X and Point.Y relative to the custom control in the newly created array. My logic is thus:

wb = new WriteableBitmap(worldMap, new TranslateTransform());
wb.Invalidate();
intTest = wb.Pixels.To2DArray(wb.PixelWidth);

My conversion logic is as such:

public static int[,] To2DArray(this int[] arr,int rowLength)
{
    int[,] output = new int[rowLength, arr.Length / rowLength];
    if (arr.Length % rowLength != 0) throw new IndexOutOfRangeException();
    for (int i = 0; i < arr.Length; i++)
    {
        output[i % rowLength, i / rowLength] = arr[i];
    }
return output;
}

Now, when I do the checking, I get completely and utterly strange results: apparently all pixels are either at values of -1 or 0, and these values are completely independent of the original colours.

Just for posterity: here's my checking code:

private void Check(object sender, MouseButtonEventArgs e)
{
    Point click = e.GetPosition(worldMap);
    ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString());
}

The result show absolutely no correlation to the path that the WriteableBitmap has rendered into it. The path has a fill of solid white.

What the heck is going on? I've tried for hours with no luck. Please, this is the major problem stopping me from submitting my first WP7 app. Any guidance?

Was it helpful?

Solution

You code works correctly, Silverlight uses pre-multiplied ARGB, so for transparent each of the RGB values will get multiplied by 0 and the final result will be 0. For white you get four 255 bytes which is -1 because of how integers are represented. You can get the bytes from an int using the BitConverter class but I'd recommend WriteableBitmapEx which has Get/SetPixel methods and handles the the pre-multiplication for you.

If you were just looking for hex values:

ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString("X8"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top