c# - WPF Toolkit Chart: How to save them as Image -


i looking save chart image merge in pdf report. wpf toolkit chart , can save image them if displayed on form. but, since working on background service, don't have visual element show up, , hence have no clue how save image, image saving code :

renderbitmap = new rendertargetbitmap(400, 400, 96, 96, pixelformats.pbgra32); drawingvisual isolatedvisual = new drawingvisual(); drawing.drawrectangle(new visualbrush(mychart), null, new rect(new point(), bounds.size)); renderbitmap.render(isolatedvisual); 

gives black image only. here mychart chart control , if add mychart window shows chart fine. so, know chart control working, doesn't render when not on window.

edit: do

 mychart.measure(size);  mychart.arrange(new rect(size));  mychart.updatelayout(); 

but still getting blank image , control not rendering on image.

i had same problem rendertargetbitmap producing black image.

i use method:

public static void saveasimage(     frameworkelement element,      string filepath,      int width,      int height) {     element.width = width;     element.height = height;     element.measure(new size(width, height));     element.arrange(new rect(0, 0, width, height));     element.updatelayout();      var target = new rendertargetbitmap(         width, height,         96, 96, system.windows.media.pixelformats.pbgra32);      target.render(element);      var encoder = new pngbitmapencoder();     var outputframe = bitmapframe.create(target);     encoder.frames.add(outputframe);      using (var file = file.openwrite(filepath))     {         encoder.save(file);     } } 

my problem frameworkeelement window object, had not been .show()'ed before. there no warnings or exceptions. black image.

if call .show() works expected.

my solution change:

savetoimage(view); 

into:

savetoimage((frameworkelement)view.content); 

this solved problem in case.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -