C#图像处理之霓虹效果实现方法
程序员文章站
2022-10-25 19:59:03
本文实例讲述了c#图像处理之霓虹效果实现方法。分享给大家供大家参考。具体如下:
//定义霓虹处理函数
public bitmap pnihong(bitmap...
本文实例讲述了c#图像处理之霓虹效果实现方法。分享给大家供大家参考。具体如下:
//定义霓虹处理函数 public bitmap pnihong(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, r2; int g1, g2, g0; int b1, b2, b0; double vr, vg, vb; //左上 p = pin - stride - 3; //右 p = pin + 3; r1 = p[2]; g1 = p[1]; b1 = p[0]; //正下 p = pin + stride; r2 = p[2]; g2 = p[1]; b2 = p[0]; //中心点 p = pin; r0 = p[2]; g0 = p[1]; b0 = p[0]; //使用模板 vr = math.sqrt((r0 - r1) * (r0 - r1) + (r0 - r2) * (r0 - r2)); vg = math.sqrt((g0 - g1) * (g0 - g1) + (g0 - g2) * (g0 - g2)); vb = math.sqrt((b0 - b1) * (b0 - b1) + (b0 - b2) * (b0 - b2)); 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#程序设计有所帮助。
上一篇: 三款花茶能淡斑、美肤 解决你的烦恼
下一篇: 四个检查全身经络畅通方法