![](/img/trans.png)
[英]Convert a dynamic BitmapImage to a grayscale BitmapImage in a Windows Phone application
[英]Windows phone 8.1 how to convert BitmapImage to grayscale and black&white?
我需要將BitmapImage轉換為灰度,第二個選項是黑白。 然后,我需要在圖像預覽上顯示它。 我創建的解決方案僅適用於Windows 8或更早版本。 謝謝。
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.