Click here to Skip to main content
12,074,494 members (60,359 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: WPF image Canvas
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.

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 23-Apr-13 0:24am
Edited 24-Apr-13 22:38pm
v2
Comments
Matej Hlatky 23-Apr-13 13:56pm
   
What about to saving Canvas content as SVG?
MahmutKoyuncu 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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/[^]
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100