locked
How to print BitmapSource RRS feed

  • Question

  • How do I print the contents of a BitmapSource?  Preferably with a border around the image and some descriptive text at the top and bottom of the page.

    I get a blank page when I run this code:
        private void PrintBitmapSource(BitmapSource InBms)
        {
          PrintDialog pd = new PrintDialog( ) ;
          bool? ret = pd.ShowDialog( ) ;
          if (ret.Value == true)
          {
    
            Image img = new Image();
            img.Source = InBms;
            img.BeginInit();
            img.Width = InBms.Width;
            img.Height = InBms.Height;
            img.Margin = new Thickness(10);
            img.EndInit();
    
            pd.PrintVisual(img, "document image");
          }
        }
    
     
    thanks,
    Tuesday, January 26, 2010 7:04 PM

Answers

  • hi SteveRichter,
                         Please replace your method with this :

    private void PrintBitmapSource(BitmapSource inBms)
            {
                var pd = new PrintDialog();
                var ret = pd.ShowDialog();
                if (ret.Value)
                {
                    var dv = new DrawingVisual();
                    using (var dc = dv.RenderOpen())
                    {
                        dc.DrawImage(inBms, new Rect(0, 0, inBms.Width, inBms.Height));
                    }
                    pd.PrintVisual(dv, "document image");
                }
            }

    • Marked as answer by Steve Richter Tuesday, January 26, 2010 7:49 PM
    Tuesday, January 26, 2010 7:39 PM

All replies