C#图像处理之浮雕效果实现方法
程序员文章站
2022-10-25 19:09:54
本文实例讲述了c#图像处理之浮雕效果实现方法。分享给大家供大家参考。具体如下:
//定义浮雕处理函数
public bitmap pfudiao(bitmap...
本文实例讲述了c#图像处理之浮雕效果实现方法。分享给大家供大家参考。具体如下:
//定义浮雕处理函数 public bitmap pfudiao(bitmap a) { try { int w = a.width; int h = a.height; bitmap dstbitmap = new bitmap(w, h, system.drawing.imaging.pixelformat.format24bpprgb); system.drawing.imaging.bitmapdata srcdata = a.lockbits(new rectangle (0, 0, w, h), system.drawing.imaging.imagelockmode.readonly, system.drawing.imaging.pixelformat.format24bpprgb); system.drawing.imaging.bitmapdata dstdata = dstbitmap.lockbits(new rectangle (0, 0, w, h), system.drawing.imaging.imagelockmode.writeonly, system.drawing.imaging.pixelformat.format24bpprgb); unsafe { byte* pin = (byte*)srcdata.scan0.topointer(); byte* pout = (byte*)dstdata.scan0.topointer(); byte* p; int stride = srcdata.stride; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { //边缘八个点像素不变 if (x == 0 || x == w - 1 || y == 0 || y == h - 1) { pout[0] = pin[0]; pout[1] = pin[1]; pout[2] = pin[2]; } else { int r0, r1; int g1, g0; int b1, b0; double vr, vg, vb; //右 p = pin - 3; r1 = p[2]; g1 = p[1]; b1 = p[0]; //中心点 p = pin; r0 = p[2]; g0 = p[1]; b0 = p[0]; //使用模板 vr = math.abs(r0 - r1+128); vg = math.abs((g0 - g1 + 128)); vb = math.abs((b0 - b1 + 128)); if (vr > 0) { vr = math.min(255, vr); } else { vr = math.max(0, vr); } if (vg > 0) { vg = math.min(255, vg); } else { vg = math.max(0, vg); } if (vb > 0) { vb = math.min(255, vb); } else { vb = math.max(0, vb); } pout[0] = (byte)vb; pout[1] = (byte)vg; pout[2] = (byte)vr; } pin += 3; pout += 3; } pin += srcdata.stride - w * 3; pout += srcdata.stride - w * 3; } } a.unlockbits(srcdata); dstbitmap.unlockbits(dstdata); return dstbitmap; } catch (exception e) { messagebox.show(e.message.tostring()); return null; } }
原图:
效果图:
希望本文所述对大家的c#程序设计有所帮助。
下一篇: hadoop 安装