简体   繁体   English

Windows Phone 8.1如何将BitmapImage转换为灰度和黑白?

[英]Windows phone 8.1 how to convert BitmapImage to grayscale and black&white?

i need to turn BitmapImage to grayscale and second option is to black&white. 我需要将BitmapImage转换为灰度,第二个选项是黑白。 Then i need to show it on image preview. 然后,我需要在图像预览上显示它。 Solutions i founded is only for windows 8 or older. 我创建的解决方案仅适用于Windows 8或更早版本。 Thanks. 谢谢。

     private async void ConvertToGrayScale()
            {
                BitmapDecoder decoder = null;
                BitmapImage bitmapImage = new BitmapImage(new Uri(imageurl, UriKind.RelativeOrAbsolute));
               RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);

                using (IRandomAccessStreamWithContentType streamWithContent = await rasr.OpenReadAsync())
                {
                    decoder = await BitmapDecoder.CreateAsync(streamWithContent);

                    // Get the first frame
                    BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0);


                    // Get the pixels
                    PixelDataProvider dataProvider =
                        await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
                                                            BitmapAlphaMode.Premultiplied,
                                                            new BitmapTransform(),
                                                            ExifOrientationMode.RespectExifOrientation,
                                                            ColorManagementMode.ColorManageToSRgb);

                    byte[] pixels = dataProvider.DetachPixelData();

                    // Create WriteableBitmap and set the pixels
                    WriteableBitmap srcBitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth,
                                                                 (int)bitmapFrame.PixelHeight);

                    using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream())
                    {
                        await pixelStream.WriteAsync(pixels, 0, pixels.Length);
                    }


                    byte[] srcPixels = new byte[4 * srcBitmap.PixelWidth * srcBitmap.PixelHeight];

                    using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream())
                    {
                        await pixelStream.ReadAsync(srcPixels, 0, srcPixels.Length);
                    }

                    // Create a destination bitmap and pixels array
                    WriteableBitmap dstBitmap =
                            new WriteableBitmap(srcBitmap.PixelWidth, srcBitmap.PixelHeight);
                    byte[] dstPixels = new byte[4 * dstBitmap.PixelWidth * dstBitmap.PixelHeight];


                    for (int i = 0; i < srcPixels.Length; i += 4)
                    {
                        double b = (double)srcPixels[i] / 255.0;
                        double g = (double)srcPixels[i + 1] / 255.0;
                        double r = (double)srcPixels[i + 2] / 255.0;

                        byte a = srcPixels[i + 3];

                        double e = (0.21 * r + 0.71 * g + 0.07 * b) * 255;
                        byte f = Convert.ToByte(e);

                        dstPixels[i] = f;
                        dstPixels[i + 1] = f;
                        dstPixels[i + 2] = f;
                        dstPixels[i + 3] = a;

                    }

                    // Move the pixels into the destination bitmap
                    using (Stream pixelStream = dstBitmap.PixelBuffer.AsStream())
                    {
                        await pixelStream.WriteAsync(dstPixels, 0, dstPixels.Length);
                    }
                    dstBitmap.Invalidate();

                    // Display the new bitmap
                   image.Source = dstBitmap;
                }
            }
private async void ConvertToBlackAndWhite()
        {
            BitmapDecoder decoder = null;
            BitmapImage bitmapImage = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
            RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);

            using (IRandomAccessStreamWithContentType streamWithContent = await rasr.OpenReadAsync())
            {
                decoder = await BitmapDecoder.CreateAsync(streamWithContent);

                // Get the first frame
                BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0);


                // Get the pixels
                PixelDataProvider dataProvider =
                    await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
                                                        BitmapAlphaMode.Premultiplied,
                                                        new BitmapTransform(),
                                                        ExifOrientationMode.RespectExifOrientation,
                                                        ColorManagementMode.ColorManageToSRgb);

                byte[] pixels = dataProvider.DetachPixelData();

                // Create WriteableBitmap and set the pixels
                WriteableBitmap srcBitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth,
                                                             (int)bitmapFrame.PixelHeight);

                using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream())
                {
                    await pixelStream.WriteAsync(pixels, 0, pixels.Length);
                }

                byte[] srcPixels = new byte[4 * srcBitmap.PixelWidth * srcBitmap.PixelHeight];

                using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream())
                {
                    await pixelStream.ReadAsync(srcPixels, 0, srcPixels.Length);
                }

                 WriteableBitmap dstBitmap =
                        new WriteableBitmap(srcBitmap.PixelWidth, srcBitmap.PixelHeight);
                byte[] dstPixels = new byte[4 * dstBitmap.PixelWidth * dstBitmap.PixelHeight];


                for (int i = 0; i < srcPixels.Length; i += 4)
                {
                    double b = (double)srcPixels[i] / 255.0;
                    double g = (double)srcPixels[i + 1] / 255.0;
                    double r = (double)srcPixels[i + 2] / 255.0;

                    byte a = srcPixels[i + 3];
                    int ans = (int)(srcPixels[i] + srcPixels[i+1] + srcPixels[i+2]) / 3;
                    double e;
                    if (ans <200)
                    {
                        e = 0;//black;
                        byte f = Convert.ToByte(e);

                        dstPixels[i] = f;
                        dstPixels[i + 1] = f;
                        dstPixels[i + 2] = f;
                        dstPixels[i + 3] = a;
                    }
                    else
                    {
                        e = 255;//white
                        byte f = Convert.ToByte(e);

                        dstPixels[i] = f;
                        dstPixels[i + 1] = f;
                        dstPixels[i + 2] = f;
                        dstPixels[i + 3] =a;
                    }

                }
                using (Stream pixelStream = dstBitmap.PixelBuffer.AsStream())
                {
                    await pixelStream.WriteAsync(dstPixels, 0, dstPixels.Length);
                }
                dstBitmap.Invalidate();

                // Display the new bitmap
                image.Source = dstBitmap;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM