Click here to Skip to main content
11,636,598 members (71,998 online)
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 20: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 jyo.net 295
1 Sergey Alexandrovich Kryukov 275
2 OriginalGriff 233
3 Suvendu Shekhar Giri 225
4 stibee 215
0 OriginalGriff 8,941
1 Sergey Alexandrovich Kryukov 8,459
2 Mika Wendelius 6,849
3 F-ES Sitecore 2,364
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web02 | 2.8.150728.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