drawingcontext - WPF Clear Region on a Drawing Context? -
so producing transparent png using drawingcontext , drawingvisual.
inside drawingcontext, drew rectange.
i "cut out" circle within of rectangle. how do this? did not find functions in drawing context clear region.
you can seek using combinedgeometry combine 2 geometries (each time). has geometrycombinemode
allowing specify logic combination. in case need geometrycombinemode.xor
. intersection of rect , ellipse (cirlce) cut out. here simple code demonstrating it:
drawingvisual dv = new drawingvisual(); using (var dc = dv.renderopen()) { var rect = new rect(0, 0, 300, 200); var cb = new combinedgeometry(geometrycombinemode.xor, new rectanglegeometry(rect), new ellipsegeometry(new point(150, 100), 50, 50)); dc.drawgeometry(brushes.blue, null, cb); }
i hope know how render drawingvisual
. can utilize rendertargetbitmap
capture kind of bitmapsource , have many ways show bitmap.
here screenshot:
the black part means color transparent.
in case want cutting out complex image (such drawn text or image). can turn combinedgeometry
kind of opacitymask
(type of brush
). can turn drawingbrush
, brush can used opacitymask
can passed drawingcontext.pushopacitymask
method:
drawingvisual dv = new drawingvisual(); using (var dc = dv.renderopen()) { var rect = new rect(0, 0, 300, 200); var cb = new combinedgeometry(geometrycombinemode.xor, new rectanglegeometry(rect), new ellipsegeometry(new point(150, 100), 50, 50)); var mask = new drawingbrush(new geometrydrawing(brushes.blue, null, cb)); dc.pushopacitymask(mask); dc.drawimage(someimage, rect); dc.drawtext(new formattedtext("windows presentation foundation", system.globalization.cultureinfo.currentculture, system.windows.flowdirection.lefttoright, new typeface("lucida bright"), 30, brushes.red){ maxtextwidth = rect.width, maxtextheight = rect.height, textalignment = textalignment.center }, new point()); }
note rect
should have size of whole drawing. positioning hole , other drawn stuff exact want.
finally drawingvisual
has useful property called clip
geometry
. can prepare combinedgeometry
, assign drawingvisual.clip
property.
suppose have drawingvisual
(with drawn stuff including text, images, ...). next code punch hole through it:
//prepare geometry, can considered puncher. var rect = new rect(0, 0, 300, 200); var cb = new combinedgeometry(geometrycombinemode.xor, new rectanglegeometry(rect), new ellipsegeometry(new point(150, 100), 50, 50)); //punch drawingvisual yourdrawingvisual.clip = cb;
wpf drawingcontext
No comments:
Post a Comment