Click here to Skip to main content
Rate this: bad
good
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 at 23-Apr-13 13:56pm
   
What about to saving Canvas content as SVG?
MahmutKoyuncu at 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 at 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
0 OriginalGriff 277
1 Maciej Los 220
2 DamithSL 190
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
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