Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi people,
My problem is that. If width is less than 6000 not problem, everything is okay, but if greater, then appears my problem. Image is distorted. Some shapes are not shown, white shape turn into black etc. (like this[^])

I tried to separate into subimages, but it didn't work.

Thanks in advance.

C#
public void ExportToPng(Canvas canvas, string path, Size size)
{
    if (path == null) return;
    canvas.Measure(size);
    var rect = new Rect(size);
    canvas.Arrange(rect);
    Render(canvas, size, path);
}


private void Render(Visual visual, Size size, string path)
{    
    double dpi = 128;
    double scale = dpi / 96;
    RenderTargetBitmap renderBitmap =
        new RenderTargetBitmap(
        (int)(size.Width * scale),
        (int)(size.Height * scale),
        dpi,
        dpi,
        PixelFormats.Pbgra32);
    renderBitmap.Render(visual);
    BitmapEncoder encoder = new JpegBitmapEncoder();
    using (FileStream outStream = new FileStream(path, FileMode.Create))
    {
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        encoder.Save(outStream);
    }
}
Posted
Updated 24-Apr-13 21:38pm
v2
Comments
Matej Hlatky 23-Apr-13 13:56pm    
What about to saving Canvas content as SVG?
Mahmut Koyuncu 24-Apr-13 4:41am    
Thanks for your response.Does WPF support SVG? I couldn't find anything about saving canvas or visual as SVG?
Matej Hlatky 24-Apr-13 4:54am    
No, it was just my idea as a workaround.
You need to code this by yourself - iterate over all Canvas children, get their absolute position, size and other visual properties and print appropriate SVG element (text, rect, circle, ...) into output stream.
Good luck!

1 solution

There are lots of different ways of doing this, you might try this:
http://stackoverflow.com/questions/5851168/save-canvas-to-bitmap[^]

The easiest solution would ofcourse be to save the imaqge in some vector format, but you would have to build the function by had, or so it seems:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/10ceacdb-1d42-4248-8413-9de6e9034774/[^]

You could use the drawing visual calass to print it though:
http://stackoverflow.com/questions/10160740/how-to-render-a-visualelement-to-a-vector-based-image[^]

Edit:
This might also be worth a go:
http://xamltoys.codeplex.com/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900