rendertargetbitmap - RenderToBitmap trouble in WPF -
actually, have print view viewport3d
, obviously, use rendertargetbitmap
. problem if resolution of rendered image got high, triangles of scene don't appear on final image.
for example, viewport
can 1024*768 , resolution use rendertargetbitmap
3 times viewport
's resolution.
i solved problem in way... in fact, triangles don't appear when use big scale. if lower size of rendertargetbitmap
, contain everything.
actually, have more or less 1024*768 @ 96dpi. if want impress @ 300dpi, need huge image avoid last solution.
some code :
public static rendertargetbitmap captureecran(viewport3d p_viewport, int p_scale) { rendertargetbitmap l_bmp; p_scale = p_scale > 5 ? 5 : p_scale; l_bmp = new rendertargetbitmap(p_scale * convert.toint32(p_viewport.actualwidth), p_scale * convert.toint32(p_viewport.actualheight), p_scale * 96.0, p_scale * 96.0, pixelformats.pbgra32); drawingvisual vis = new drawingvisual(); drawingcontext dc = vis.renderopen(); dc.drawrectangle(system.windows.media.brushes.white, null, new rect(0, 0, p_scale * p_viewport.actualwidth, p_scale * p_viewport.actualheight)); dc.close(); l_bmp.render(vis); p_viewport.updatelayout(); l_bmp.render(p_viewport); return l_bmp; } public static void saveimage(rendertargetbitmap rendertargetbitmap, string m_impression) { system.windows.forms.folderbrowserdialog l_fbd = new system.windows.forms.folderbrowserdialog(); l_fbd.showdialog(); string l_path = l_fbd.selectedpath + "\\" + currentuser() + "__" + currentdate() + "__." + m_impression.tolower(); filestream stream = new filestream(l_path, filemode.create); bitmapencoder l_encoder = null; switch(m_impression){ case "png": pngbitmapencoder l_png = new pngbitmapencoder(); l_encoder = l_png; break; case "jpeg": jpegbitmapencoder l_jpeg = new jpegbitmapencoder(); l_jpeg.qualitylevel = 30; l_encoder = l_jpeg; break; } l_encoder.frames.add(bitmapframe.create(rendertargetbitmap)); l_encoder.save(stream); stream.close(); }
my call :
saveimage(captureecran(m_viewportcourant.viewport3d,5), m_impression);
where m_impression .png or .jpg
it works using visualbrush containing viewport , drawn drawingcontext.
drawingvisual vis = new drawingvisual(); drawingcontext dc = vis.renderopen(); visualbrush sourcebrush = new visualbrush(p_viewport); dc.drawrectangle(system.windows.media.brushes.white, null, new rect(0, 0, p_viewport.actualwidth * p_scale, p_viewport.actualheight * p_scale)); dc.drawrectangle(sourcebrush, null, new rect(new system.windows.point(0, 0), new vector(p_viewport.actualwidth, p_viewport.actualheight))); dc.close(); l_bmp.render(vis);
Comments
Post a Comment