c# - How to convert a view into a PNG? -
i want create png view. using code:
//i instance user control ucformtoprintview miviewtoprint = new ucformtoprintview(); miviewtoprint.datacontext = ((ucformtoprintview)ccform).datacontext; //i use rendertargetbitmap render user control system.windows.media.imaging.rendertargetbitmap rtb = new system.windows.media.imaging.rendertargetbitmap(794, 1122, 72, 72, system.windows.media.pixelformats.pbgra32); rtb.render(miviewtoprint); //i use encoder create png system.windows.media.imaging.pngbitmapencoder encoder = new system.windows.media.imaging.pngbitmapencoder(); encoder.frames.add(system.windows.media.imaging.bitmapframe.create(rtb)); //i use dialog select path save png file microsoft.win32.savefiledialog savefiledialog = new microsoft.win32.savefiledialog(); savefiledialog.filterindex = 1; if (savefiledialog.showdialog() == true) { using (system.io.stream stream = savefiledialog.openfile()) { encoder.save(stream); stream.close(); system.diagnostics.process.start(savefiledialog.filename); } }
the result empty png.
how can create png file user control?
thank much.
the usercontrol has laid out @ least once visible. can achieve calling measure
, arrange
methods.
var miviewtoprint = new ucformtoprintview(); miviewtoprint.datacontext = ((ucformtoprintview)ccform).datacontext; // layout, i.e. measure , arrange miviewtoprint.measure(new size(double.positiveinfinity, double.positiveinfinity)); miviewtoprint.arrange(new rect(miviewtoprint.desiredsize)); ... if (savefiledialog.showdialog() == true) { using (var stream = savefiledialog.openfile()) { encoder.save(stream); } system.diagnostics.process.start(savefiledialog.filename); }
Comments
Post a Comment