Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

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

C#
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

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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900