Print a picture from a picturebox

2 posts, 0 answers
  1. Roh
    Roh avatar
    1 posts
    Member since:
    May 2012

    Posted 26 Jan Link to this post

    Hi.

    I have a problem.

    How can print a picture from a picturebox?

    How can put correct object in Associateobjet to print it?

  2. Hristo
    Admin
    Hristo avatar
    1507 posts

    Posted 28 Jan Link to this post

    Hi Roh,

    The RadPrintDocument can only be used with RadControls implementing the IPrintable interface. This is also the type of the AssociatedObject property. If you want to use it with a standard picture box, you will need to inherit it and implement the interface similarly as demonstrated here: https://docs.telerik.com/devtools/winforms/telerik-presentation-framework/printing-support/how-to/create-prinatble-panel.

    Please also check my code snippet below: 
    public class PrintablePictureBox : PictureBox, IPrintable
    {
        public int BeginPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return 1;
        }
     
        public bool EndPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return true;
        }
     
        public Form GetSettingsDialog(RadPrintDocument document)
        {
            return new PrintSettingsDialog(document);
        }
     
        public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)
        {
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
            args.Graphics.DrawImage(bmp, Point.Empty);
     
            return false;
        }
     
     
        public void Print()
        {
            RadPrintDocument doc = this.CreatePrintDocument();
            doc.Print();
        }
     
        public void PrintPreview()
        {
            RadPrintDocument doc = this.CreatePrintDocument();
            RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);
             
            dialog.ShowDialog();
        }
     
        private RadPrintDocument CreatePrintDocument()
        {
            RadPrintDocument doc = new RadPrintDocument();
            doc.AssociatedObject = this;
            return doc;
        }
    }

    I hope this will help.

    Regards,
    Hristo
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top