将动态BitmapImage转换为Windows Phone应用程序中的灰度BitmapImage

我想将BitmapImage转换为Grayscale BitmapImage:我从一个方法得到的因此 – 我不知道宽度和高度。 我已经尝试过查看诸如WritableBitmapEx和静态扩展方法之类的选项,但它们对我没有帮助,因为我希望返回数据类型是BitmapImage,然后我需要将它添加到List。

这在使用C#的Windows Phone应用程序中是否可行? 如果有人能对此有所了解,我将非常感激。 谢谢。

算法非常简单:

using System.Windows.Media.Imaging; using System.IO; private WriteableBitmap ConvertToGrayScale(BitmapImage source) { WriteableBitmap wb = new WriteableBitmap(source); // create the WritableBitmap using the source int[] grayPixels = new int[wb.PixelWidth * wb.PixelHeight]; // lets use the average algo for (int x = 0; x < wb.Pixels.Length; x++) { // get the pixel int pixel = wb.Pixels[x]; // get the component int red = (pixel & 0x00FF0000) >> 16; int blue = (pixel & 0x0000FF00) >> 8; int green = (pixel & 0x000000FF); // get the average int average = (byte)((red + blue + green) / 3); // assign the gray values keep the alpha unchecked { grayPixels[x] = (int)((pixel & 0xFF000000) | average << 16 | average << 8 | average); } } // copy grayPixels back to Pixels Buffer.BlockCopy(grayPixels, 0, wb.Pixels, 0, (grayPixels.Length * 4)); return wb; } private BitmapImage ConvertWBtoBI(WriteableBitmap wb) { BitmapImage bi; using (MemoryStream ms = new MemoryStream()) { wb.SaveJpeg(ms, wb.PixelWidth, wb.PixelHeight, 0, 100); bi = new BitmapImage(); bi.SetSource(ms); } return bi; } 

  

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { WriteableBitmap wb = ConvertToGrayScale((BitmapImage)this.myImage.Source); BitmapImage bi = ConvertWBtoBI(wb); myImage.Source = bi; } 

行动守则:

在此处输入图像描述

不确定这里的命名空间,但这样的事情可能有效:

 using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; FormatConvertedBitmap bitmapGreyscale = new FormatConvertedBitmap(bitmap, PixelFormats.Gray8, BitmapPalettes.Gray256, 0.0); 

您无法写入BitmapImage:您需要将其转换为WriteableBitmap。 一旦有了WriteableBitmap,就可以轻松访问缓冲区并将像素转换为GreyScale。

WriteableBitmaps和BitmapImages的工作方式非常相似,因为它们都是BitmapSources。 如果将列表创建为列表而不是列表,则可以将它们添加到同一列表中

应用程序不太可能对List的内容做任何事情,要求内容是BitmapImages而不是BitmapSources。