views:

1306

answers:

2

I have a BitmapImage instance in Silverlight, and I am trying to find a way to read the color information of each pixel in the image. How can I do this? I see that there is a CopyPixels() method on this class that writes pixel information into the array that you pass it, but I don't know how to read color information out of that array.

How can I do this?

A: 

This is not possible with the current Bitmap API in the currently released Silverlight 3 beta.

The Silverlight BitmapImage file does not have a CopyPixels method. Please see the MSDN documentation here.

markti
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage_methods.aspxIt is on MSDN, but yes it doesn't seem to be available in Silverlight.
Rafe
The link you provided is for .NET 3.5, not Silverlight.You can find the MSDN documentation for the Silverlight BitmapImage class here: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(VS.96).aspx
markti
+1  A: 

Look for the WriteableBitmap class in Silverlight 3. This class has a member Pixels which returns the bitmap's pixel data in an array of int's.

An example with a transform. bi is an BitmapImage object.

Image img = new Image();
img.source = bi;

img.Measure(new Size(100, 100));
img.Arrange(new Rect(0, 0, 100, 100));

ScaleTransform scaleTrans = new ScaleTransform();
double scale = (double)500 / (double)Math.Max(bi.PixelHeight, bi.PixelWidth);
scaleTrans.CenterX = 0;
scaleTrans.CenterY = 0;
scaleTrans.ScaleX = scale;
scaleTrans.ScaleY = scale;

WriteableBitmap writeableBitmap = new WriteableBitmap(500, 500);
writeableBitmap.Render(img, scaleTrans);

int[] pixelData = writeableBitmap.Pixels;
Chris