Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows WPF
Hi all

I am using the below code to assign the barcode to be displayed in a canvas inside rectangle

 for (int i = 0; i < len; i++)
            {
                Rectangle rect = new Rectangle();
                rect.Height = 200;
                if (currentColor == 0)
                {
                    currentColor = 1;
                    rect.Fill = new SolidColorBrush(Colors.Black);
 
                }
                else
                {
                    currentColor = 0;
                    rect.Fill = new SolidColorBrush(Colors.White);
 
                }
                Canvas.SetLeft(rect, currentPos);
                Canvas.SetTop(rect, currentTop);
 
                if (outputString[i] == 't')
                {
                    rect.Width = thinWidth;
                    currentPos += thinWidth;
 
                }
                else if (outputString[i] == 'w')
                {
                    rect.Width = thickWidth;
                    currentPos += thickWidth;
 
                }
                mainCanvas.Children.Add(rect);
 
            }

Now i want to save that code as a image file.
Please tell me how to do that

Thank you
Posted 30-Jan-13 21:31pm
Code-Hunt7.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As you already have a Canvas instance, the problem is reduced to producing a bitmap out of it. This is done using the class System.Windows.Media.Imaging.RenderTargetBitmap:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx[^].

You can find a complete code sample, for example, here: http://denisvuyka.wordpress.com/2007/12/03/wpf-diagramming-saving-you-canvas-to-image-xps-document-or-raw-xaml/[^].

This solves your problem.

—SA
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 453
1 Sascha Lefévre 370
2 Abhinav S 170
3 Richard Deeming 160
4 Maciej Los 150
0 Sergey Alexandrovich Kryukov 7,096
1 OriginalGriff 6,395
2 Maciej Los 2,782
3 Abhinav S 2,732
4 Peter Leow 2,694


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
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