forms - Blur an image on MouseClick in C# -
so have form picturebox on it, idea blur image around cursor when click it. ideas?
it worked on pc when had blur whole image, , added mouse location stuff , doesn't seem anything.
public partial class form1 : form { bitmap newbitmap; image file; int bluramount = 5 ; public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { dialogresult dr = openfiledialog1.showdialog(); if (dr == dialogresult.ok) { file = image.fromfile(openfiledialog1.filename); newbitmap = new bitmap(openfiledialog1.filename); picturebox1.image=file; } } private void form1_mouseclick(object sender, mouseeventargs e) { (int x = e.location.x-25; x < e.location.x+25; x++) { (int y =e.location.y-25; y < e.location.y+25; y++) { try { color prevx = newbitmap.getpixel(x - bluramount, y); color nextx = newbitmap.getpixel(x + bluramount, y); color prevy = newbitmap.getpixel(x, y - bluramount); color nexty = newbitmap.getpixel(x, y + bluramount); int avgr = (int)((prevx.r + nextx.r + prevy.r + nexty.r) / 4); int avgg = (int)((prevx.g + nextx.g + prevy.g + nexty.g) / 4); int avgb = (int)((prevx.b + nextx.b + prevy.b + nexty.b) / 4); newbitmap.setpixel(x, y, color.fromargb(avgr, avgg, avgb)); } catch (exception) {} } } picturebox1.image = newbitmap; } }
i set click event picturebox , works fine
public partial class form1 : form { bitmap newbitmap; image file; int bluramount = 5; public form1() { initializecomponent(); // occurs when picturebox1 control clicked. this.picturebox1.click += new system.eventhandler(this.picturebox1_click); } private void button1_click(object sender, eventargs e) { dialogresult dr = openfiledialog1.showdialog(); if (dr == dialogresult.ok) { file = image.fromfile(openfiledialog1.filename); newbitmap = new bitmap(openfiledialog1.filename); picturebox1.image = file; } } private void form1_mouseclick(object sender, mouseeventargs e) { } private void picturebox1_click(object sender, eventargs e) { mouseeventargs me = (mouseeventargs)e; point coordinates = me.location; (int x = me.location.x - 25; x < me.location.x + 25; x++) { (int y = me.location.y - 25; y < me.location.y + 25; y++) { try { color prevx = newbitmap.getpixel(x - bluramount, y); color nextx = newbitmap.getpixel(x + bluramount, y); color prevy = newbitmap.getpixel(x, y - bluramount); color nexty = newbitmap.getpixel(x, y + bluramount); int avgr = (int)((prevx.r + nextx.r + prevy.r + nexty.r) / 4); int avgg = (int)((prevx.g + nextx.g + prevy.g + nexty.g) / 4); int avgb = (int)((prevx.b + nextx.b + prevy.b + nexty.b) / 4); newbitmap.setpixel(x, y, color.fromargb(avgr, avgg, avgb)); } catch (exception) { } } } picturebox1.image = newbitmap; } }
Comments
Post a Comment