Click here to Skip to main content
12,501,833 members (53,261 online)
Rate this:
 
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 31 Jan 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