c# - Save image of whole ListView -
i have next code:
private static void snapshotpng(listview source, string destination, int zoom) { try { double actualheight = source.actualheight; double actualwidth = source.actualwidth; double renderheight = actualheight * zoom; double renderwidth = actualwidth * zoom; rendertargetbitmap rendertarget = new rendertargetbitmap((int)renderwidth, (int)renderheight, 96, 96, pixelformats.pbgra32); visualbrush sourcebrush = new visualbrush(source); drawingvisual drawingvisual = new drawingvisual(); drawingcontext drawingcontext = drawingvisual.renderopen(); using (drawingcontext) { drawingcontext.pushtransform(new scaletransform(zoom, zoom)); drawingcontext.drawrectangle(sourcebrush, null, new rect(new point(0, 0), new point(actualwidth, actualheight))); } rendertarget.render(drawingvisual); pngbitmapencoder encoder = new pngbitmapencoder(); encoder.frames.add(bitmapframe.create(rendertarget)); using (filestream stream = new filestream(destination, filemode.create, fileaccess.write)) { encoder.save(stream); } } catch (exception e) { messagebox.show(e.message); } }
it saves given source image, works fine. save visible part of control (in case, visible items of listview). how can save snapshot of whole items in listview?
i've changed first 2 lines, adding arrange , measure methods, allow control render in memory. i've assumed, control doesn't scroll horizontally , kept width was, since otherwise use minimal width required largest child. can change it.
here method.
private static void snapshotpng(listview source, string destination, int zoom) { try { double actualwidth = source.actualwidth; source.measure(new size(source.actualwidth, double.positiveinfinity)); source.arrange(new rect(0, 0, actualwidth, source.desiredsize.height)); double actualheight = source.actualheight; double renderheight = actualheight * zoom; double renderwidth = actualwidth * zoom; rendertargetbitmap rendertarget = new rendertargetbitmap((int)renderwidth, (int)renderheight, 96, 96, pixelformats.pbgra32); visualbrush sourcebrush = new visualbrush(source); drawingvisual drawingvisual = new drawingvisual(); drawingcontext drawingcontext = drawingvisual.renderopen(); using (drawingcontext) { drawingcontext.pushtransform(new scaletransform(zoom, zoom)); drawingcontext.drawrectangle(sourcebrush, null, new rect(new point(0, 0), new point(actualwidth, actualheight))); } rendertarget.render(drawingvisual); pngbitmapencoder encoder = new pngbitmapencoder(); encoder.frames.add(bitmapframe.create(rendertarget)); using (filestream stream = new filestream(destination, filemode.create, fileaccess.write)) { encoder.save(stream); } } catch (exception e) { messagebox.show(e.message); } }
Comments
Post a Comment