今天在做图片处理的时候没找到有多少关于wp8的图片处理,wp8.1的话因为API和window的一样了,处理按window来简单好多,而且也到处都是,所以就找了些资料,自己加了两个处理效果
1.灰度处理:
也就是将彩色变黑白效果,他的原理是:对每个像素点的RGB进行平均处理。下面我是使用了加权平均去算的
/// <summary> /// 灰度处理 /// </summary> private void GrayScale() { WriteableBitmap wb = new WriteableBitmap(imageSource, null); //imageSource这个是image图片 WriteableBitmap wb_gray = new WriteableBitmap(wb.PixelWidth, wb.PixelHeight); int[] ImageData = wb.Pixels; for (int i = 0; i < wb.PixelHeight; i++) { for (int j = 0; j < wb.PixelWidth; j++) { int curColor = ImageData[i * wb.PixelWidth + j]; byte RedValue = (byte)(curColor >> 16 & 0xFF); //获取整形值 byte GreenValue = (byte)(curColor >> 8 & 0xFF); byte BlueValue = (byte)(curColor & 0xFF); byte GrayValue = (byte)(RedValue * 0.7 + GreenValue * 0.2 + BlueValue * 0.1); byte[] GrayValueArr = new byte[4]; GrayValueArr[3] = 0xFF; //0x00 GrayValueArr[2] = GrayValue; GrayValueArr[1] = GrayValue; GrayValueArr[0] = GrayValue; //依次通过该数组,获取作为整数值的单个像素值,这些整数值计算为自左乘的 ARGB32。 int GrayPixel = BitConverter.ToInt32(GrayValueArr, 0); unchecked { wb_gray.Pixels[i * wb.PixelWidth + j] = GrayPixel; //OK->成功啦!!! } } } wb_gray.Invalidate(); imageC.Source = wb_gray; //imageC这个是image图片 }
2.反色处理
原理:对每个像素点的RGB进行取反处理(就是用255去减)。
private void inverse() { WriteableBitmap wb = new WriteableBitmap(imageSource, null); WriteableBitmap wb_gray = new WriteableBitmap(wb.PixelWidth, wb.PixelHeight); int[] ImageData = wb.Pixels; for (int i = 0; i < wb.PixelHeight; i++) { for (int j = 0; j < wb.PixelWidth; j++) { int curColor = ImageData[i * wb.PixelWidth + j]; int RedValue = curColor >> 16 & 0xFF; int GreenValue = curColor >> 8 & 0xFF; int BlueValue = curColor & 0xFF; byte[] GrayValueArr = new byte[4]; GrayValueArr[3] = 0xFF; //0x00 GrayValueArr[2] = (byte)(255-RedValue); GrayValueArr[1] = (byte)(255-GreenValue); GrayValueArr[0] = (byte)( 255- BlueValue); //依次通过该数组,获取作为整数值的单个像素值,这些整数值计算为自左乘的 ARGB32。 int GrayPixel = BitConverter.ToInt32(GrayValueArr, 0); unchecked { wb_gray.Pixels[i * wb.PixelWidth + j] = GrayPixel; } } } wb_gray.Invalidate(); imageC.Source = wb_gray; }
3.柔化处理
原理:当前像素点与周围像素点的颜色差距较大时取其平均值(这个也是看来的,个人感觉是取当前像素点颜色与周围的几个像素点颜色进行平均)
private void Soften() { WriteableBitmap wb = new WriteableBitmap(imageSource, null); WriteableBitmap wb_gray = new WriteableBitmap(wb.PixelWidth, wb.PixelHeight); int[] ImageData = wb.Pixels; //高斯模板,高斯一个很牛逼的人,大学数学书里到处都有他,人称“数学王子”,没错高斯定理就是这家伙的 int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 }; for (int i = 0; i < wb.PixelHeight; i++) { for (int j = 0; j < wb.PixelWidth; j++) { int curColor = ImageData[i * wb.PixelWidth + j]; int r = 0; int g = 0; int b = 0; int Index = 0; for (int col = -1; col <= 1; col++) for (int row = -1; row <= 1; row++) { int x = (i + col) < 0 || (i + col) > i ? i : i + col; int y = (j + row) < 0 || (j + row) > j ? j : j + row; curColor = ImageData[x * wb.PixelWidth + y]; r += (curColor >> 16 & 0xFF) *Gauss[Index]; g += (curColor >> 8 & 0xFF) * Gauss[Index]; b += (curColor & 0xFF) * Gauss[Index]; Index++; } byte[] GrayValueArr = new byte[4]; GrayValueArr[3] = 0xFF; //0x00 GrayValueArr[2] = (byte)(r/16); GrayValueArr[1] = (byte)(g/16); GrayValueArr[0] = (byte)(b/16); //依次通过该数组,获取作为整数值的单个像素值,这些整数值计算为自左乘的 ARGB32。 int GrayPixel = BitConverter.ToInt32(GrayValueArr, 0); unchecked { wb_gray.Pixels[i * wb.PixelWidth + j] = GrayPixel; } } } wb_gray.Invalidate(); imageC.Source = wb_gray; }
原本还想做其他效果的,比如锐化效果,雾化效果,这里先说说原理(都是看来的呀),
锐化效果:突出显示颜色值大(即形成形体边缘)的像素点(就是把像素点的RGB值都加大)
雾化效果:在图像中引入一定的随机值, 打乱图像中的像素值(随机改变每个像素点的大小)
上面说的锐化效果还比较好实现方法都差不多,但是雾化效果可以看看ImageTools,里面提供了ImageTools.Filtering(还没看怎么用,主要看点EditableImage这个应该可以实现),其他的什么变绿变蓝的效果把RGB值中对应的颜色改改就出来了