C# - Winforms Drawing on Bitmap -
i having such kind of problem. making small photo editor, can select image, change brightness contrast of image, draw text on image. form. . have methods.
private void drawtext(string text, int x, int y) { rectanglef rectf = new rectanglef(x, y, 90, 50); graphics g = graphics.fromimage(this.picturebox1.image); g.smoothingmode = smoothingmode.antialias; g.interpolationmode = interpolationmode.highqualitybicubic; g.pixeloffsetmode = pixeloffsetmode.highquality; g.drawstring(text.tostring(), new font("tahoma", convert.toint32(this.textboxsize.text.tostring())), brushes.black, rectf); g.flush(); } private void setbrightness(int brightness) { if (!object.referenceequals(this.picturebox1.image, null)) { bitmap temp = (bitmap)this.picturebox1.image; bitmap bmap = (bitmap)temp.clone(); if (brightness < -255) brightness = -255; if (brightness > 255) brightness = 255; color c; (int = 0; < bmap.width; i++) { (int j = 0; j < bmap.height; j++) { c = bmap.getpixel(i, j); int cr = c.r + brightness; int cg = c.g + brightness; int cb = c.b + brightness; if (cr < 0) cr = 1; if (cr > 255) cr = 255; if (cg < 0) cg = 1; if (cg > 255) cg = 255; if (cb < 0) cb = 1; if (cb > 255) cb = 255; bmap.setpixel(i, j, color.fromargb((byte)cr, (byte)cg, (byte)cb)); } } this.picturebox1.image = (bitmap)bmap.clone(); } } public void setcontrast(double contrast) { if (!object.referenceequals(this.picturebox1.image, null)) { bitmap temp = (bitmap)this.picturebox1.image; bitmap bmap = (bitmap)temp.clone(); if (contrast < -100) contrast = -100; if (contrast > 100) contrast = 100; contrast = (100.0 + contrast) / 100.0; contrast *= contrast; color c; (int = 0; < bmap.width; i++) { (int j = 0; j < bmap.height; j++) { c = bmap.getpixel(i, j); double pr = c.r / 255.0; pr -= 0.5; pr *= contrast; pr += 0.5; pr *= 255; if (pr < 0) pr = 0; if (pr > 255) pr = 255; double pg = c.g / 255.0; pg -= 0.5; pg *= contrast; pg += 0.5; pg *= 255; if (pg < 0) pg = 0; if (pg > 255) pg = 255; double pb = c.b / 255.0; pb -= 0.5; pb *= contrast; pb += 0.5; pb *= 255; if (pb < 0) pb = 0; if (pb > 255) pb = 255; bmap.setpixel(i, j, color.fromargb((byte)pr, (byte)pg, (byte)pb)); } } this.picturebox1.image = (bitmap)bmap.clone(); } }
and on button click calling methods work. problem when draw text second time, not deleting , drawing text again, need text drawn @ different location every time , not on old text. can please suggest solution ? thanks
there 2 solutions:
do not draw on bitmap; instead draw on invisible bitmap on top of it.
keep original bitmap , copy before drawing on it.
once have changed pixels of bitmap cannot undo unless keep original data.
Comments
Post a Comment