locked
Print controls presents in Picture box RRS feed

  • Question

  • Hello Friends 

    I am developing a window application where i have to print controls which is present in Picturebox in c#

    please help me how can i print the controls available in c#

    Thanks in advance 

    Monday, February 15, 2016 9:58 AM

Answers

  • I'm a bit confused by the question.

    A PictureBox is intended to show pictures rather than as a container for controls.

    So I don't really follow why you'd have controls inside a picturebox.

    .

    By " window application", do you mean windows forms? Or UWA / WPF?

    You should post in the forum specific to your technology so that it's clear which you're using and you get support from people who specialise in that technology.

    Windows forms would be over here:

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    Guessing it's winforms.

    All controls have a DrawToBitMap method so you can do:

    Graphics g = Graphics.FromImage(pictureBox1.Image);
    
    using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g))
    {
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        bmp.Save(@"C:\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }

    or maybe you just mean print a control and you can use the approach here:

    http://stackoverflow.com/questions/10418215/printing-a-control

    down vote
    
    accepted
     
    private static void PrintControl(Control control)
    {
        var bitmap = new Bitmap(control.Width, control.Height);
    
        control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
    
        var pd = new PrintDocument();
    
        pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
        pd.Print();
    }
     


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by Kristin Xie Tuesday, February 16, 2016 6:28 AM
    • Marked as answer by Kristin Xie Monday, February 22, 2016 5:24 AM
    Monday, February 15, 2016 10:15 AM

All replies

  • I'm a bit confused by the question.

    A PictureBox is intended to show pictures rather than as a container for controls.

    So I don't really follow why you'd have controls inside a picturebox.

    .

    By " window application", do you mean windows forms? Or UWA / WPF?

    You should post in the forum specific to your technology so that it's clear which you're using and you get support from people who specialise in that technology.

    Windows forms would be over here:

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    Guessing it's winforms.

    All controls have a DrawToBitMap method so you can do:

    Graphics g = Graphics.FromImage(pictureBox1.Image);
    
    using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g))
    {
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        bmp.Save(@"C:\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }

    or maybe you just mean print a control and you can use the approach here:

    http://stackoverflow.com/questions/10418215/printing-a-control

    down vote
    
    accepted
     
    private static void PrintControl(Control control)
    {
        var bitmap = new Bitmap(control.Width, control.Height);
    
        control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
    
        var pd = new PrintDocument();
    
        pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
        pd.Print();
    }
     


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by Kristin Xie Tuesday, February 16, 2016 6:28 AM
    • Marked as answer by Kristin Xie Monday, February 22, 2016 5:24 AM
    Monday, February 15, 2016 10:15 AM
  • Hi Mjay,

    I agree with Andy.

    >> where i have to print controls which is present in Picturebox in c#

    What do you mean by "print controls which is present in Picturebox"? I assume it's winform project. Does the controls are from tool box? Or any others?

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 16, 2016 6:28 AM