0

嗨,我试图在 Windows 手机中使用WriteableBitmapEx,但代码不起作用......我在这做错了什么?

        double height = image1.ActualHeight;
        double width = image1.ActualWidth;
        BitmapImage img = new BitmapImage(new Uri("Tulips.png", UriKind.RelativeOrAbsolute));
        BitmapImage newImg = image1.Source as BitmapImage;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {

                int grayScale = (int)((image1.writeableBmp.GetPixel(j, i).R * 0.3) + (image1.writeableBmp.GetPixel(j, i).G * 0.59) + (image1.GetPixel(j, i).B * 0.11));
                Color nc = Color.FromArgb (grayScale, grayScale, grayScale);
                newImg.SetPixel(j, i, nc);

            }
        }
4

1 回答 1

2

您正在尝试修改BitmapImage( newImg)。您必须创建一个WriteableBitmap

var newImg = new WriteableBitmap(image1.Source);

之后能够修改位图图像。

然后(如果您引用WriteableBitmapEx)您应该能够grayScale直接从以下获取表达式中的像素值newImg

byte grayScale = Convert.ToByte((newImg.GetPixel(j, i).R * 0.3) + 
    (newImg.GetPixel(j, i).G * 0.59) + (newImg.GetPixel(j, i).B * 0.11));

然后还有一个Color.FromArgb看起来应该更像这样的声明:

Color nc = Color.FromArgb (255, grayScale, grayScale, grayScale);
于 2012-06-25T14:06:14.413 回答