我想在Windows Phone 8.1中调整图像的对比度 我找到了Windows Phone 7的示例,但它在Windows Phone 8.1中不起作用。
这是我的代码:
public static WriteableBitmap ChangeContrast(WriteableBitmap Bitmap,
int[] orgPixels,
double contrastValue)
{
var result = new WriteableBitmap(Bitmap.PixelWidth, Bitmap.PixelHeight);
var contrastFactor = (50f + contrastValue) / 50f;
contrastFactor *= contrastFactor;
var contrastFactorIndex = (int)(contrastFactor * 32768);
var inputPixels = Bitmap.PixelBuffer;
int[] tempPixels = new int[Bitmap.PixelBuffer.Length];
orgPixels.CopyTo(tempPixels, 0);
if (0 != Bitmap.PixelBuffer.Length)
{
for (int index = 0; index < Bitmap.PixelBuffer.Length; index++)
{
// Extract color components
var color = tempPixels[index];
var alpha = (byte)(color >> 24);
var red = (byte)(color >> 16);
var green = (byte)(color >> 8);
var blue = (byte)(color);
int ri = red - 128;
int gi = green - 128;
int bi = blue - 128;
// Multiply contrast factor
ri = (ri * contrastFactorIndex) >> 15;
gi = (gi * contrastFactorIndex) >> 15;
bi = (bi * contrastFactorIndex) >> 15;
// Transform back to range [0, 255]
ri = ri + 128;
gi = gi + 128;
bi = bi + 128;
// Clamp to byte boundaries
red = (byte)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
green = (byte)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
blue = (byte)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
result.SetValue[index] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
//附加Buffer.BlockCopy(tempPixels,0,inputPixels,0,tempPixels.Length);
return result;
}
&#39; SetValue&#39;是&#39;像素&#39;在原始样本中,但在WP8.1上此对象中不存在像素。
等同的是什么?
答案 0 :(得分:1)
您想要修改int [] tempPixels数组。完成后,您需要将tempPixel数组复制回result.PixelBuffer。您可以使用Buffer.BlockCopy
将块复制到另一个块。
完整示例:如何调整亮度:Adjust Brightness WriteableBitmap 8.1
完整示例:灰度图像:Grayscale an Image WriteableBitmap 8.0
请务必在System.IO
和System.Runtime.InteropServices.WindowsRuntime
中添加上述解决方案。
我还在OneDrive中包含了完整的8.1项目:Adjust Brightness Project 8.1