c# - Clearing image and draw information on it -
hi want draw information on image in c#. wrote code , works:
bitmap bmp = new bitmap(@"g:\cert_template.png"); graphics g = graphics.fromimage(bmp); g.drawstring(cert_id, new font("b zar", 3,system.drawing.fontstyle.bold), brushes.black, new point(85, 95)); g.drawstring(date_cert, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(85, 135)); g.drawstring(s1 + s3, new font("b zar", 4, system.drawing.fontstyle.bold), brushes.black, new point(90, 290)); g.drawstring(s4, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(480, 360)); g.drawstring(date_exam, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(170, 515)); g.drawstring(convert.tostring(mark), new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(520, 600)); g.drawstring(lvl, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(150, 600)); g.drawstring(prvnc, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(170, 780)); g.drawstring(center, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(310, 870)); g.drawstring(inst, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(150, 870)); capturescreen(g,imgcounter);
my image same information write. i've put code in loop , draws image different information overwrites previous image. want clear image , write on again without overwriting.
edit
after send g
function show in picture box:
private void capturescreen(graphics g,int imgcounter) { /*this method captures snapshot of screen , * adds imageflowlayoutpanel */ bmp.save("snap" + imgcounter.tostring() + ".png", system.drawing.imaging.imageformat.png); //creating picturebox control , add flowlayoutpanel picturebox temppicturebox = new picturebox(); //generates thumbnail image of specified size temppicturebox.sizemode = pictureboxsizemode.stretchimage; temppicturebox.image = bmp.getthumbnailimage(600, 700, new image.getthumbnailimageabort(thumbnailcallback), intptr.zero); temppicturebox.size = new system.drawing.size(50,50); temppicturebox.click += new eventhandler(this.temppicturebox_click); imageflowlayoutpanel.controls.add(temppicturebox); } //this click event used display enlarged images private void temppicturebox_click(object sender, eventargs e) { previewpicturebox.image = ((picturebox)sender).image; } public bool thumbnailcallback() { return true; }
one possibility create temporary bitmap, based on original one, each cycle.
please not made graphics instance disposed when no longer needed (by means of using clause) , note graphics instance build on top of newbitmap
not of original one.
also make sure consume newbitmap
reference, @ end of cycle instead of bmp
reference holds original background.
bitmap bmp = new bitmap(@"g:\cert_template.png"); ( ... ) { bitmap newbitmap = new bitmap(bmp); using (graphics g = graphics.fromimage(newbitmap)) { // somehow alter cert_id, date_cert, etc... g.drawstring(cert_id, new font("b zar", 3,system.drawing.fontstyle.bold), brushes.black, new point(85, 95)); g.drawstring(date_cert, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(85, 135)); g.drawstring(s1 + s3, new font("b zar", 4, system.drawing.fontstyle.bold), brushes.black, new point(90, 290)); g.drawstring(s4, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(480, 360)); g.drawstring(date_exam, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(170, 515)); g.drawstring(convert.tostring(mark), new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(520, 600)); g.drawstring(lvl, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(150, 600)); g.drawstring(prvnc, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(170, 780)); g.drawstring(center, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(310, 870)); g.drawstring(inst, new font("b zar", 3, system.drawing.fontstyle.bold), brushes.black, new point(150, 870)); // , here newbitmap instance // (for example: save filename) } }
Comments
Post a Comment