Karl
Karl

Reputation: 75

C# Save all of scrollable Panel as Image

I'm trying to save all of a panel to a JPG, I already asked this question but the answer wasn;t what I was looking for. The problem is that the panel has a scrollbar (not enough space for all elements) but it doesn't print what is at the bottom of the panel, only visible elements.

public void DrawControl(Control control,Bitmap bitmap)
{
    control.DrawToBitmap(bitmap,control.Bounds);
    foreach (Control childControl in control.Controls)
    {
        DrawControl(childControl,bitmap);
    }
}

public void SaveBitmap()
{
    Bitmap bmp = new Bitmap(this.panel1.Width, this.panel.Height);

    this.panel.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel.Width, this.panel.Height));
    foreach (Control control in panel1.Controls)
    {
        DrawControl(control, bmp);
    }

    bmp.Save("d:\\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Upvotes: 3

Views: 4005

Answers (3)

Richard Lee Corlew
Richard Lee Corlew

Reputation: 11

I realize that this is an old thread but I found a much simpler way to save the contents of a single panel as an image. This will work for a single panel, currently one of my reports prints out to 1560 x 6043 using this approach.

private void button2_Click(object sender, EventArgs e)
{

    panel1.AutoSize = true;
    panel1.Refresh();

    using (Bitmap bmp = new Bitmap(panel1.Width, panel1.Height))
    {

       panel1.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
       string fileName = "Full_Domain_Report_" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss") + ".png";       
       bmp.Save(@"C:\temp\" + fileName, ImageFormat.Png); // make sure path exists!

    }

    panel1.AutoSize = false;
    panel1.Refresh();
}

Upvotes: 0

Ch'nycos
Ch'nycos

Reputation: 300

You could scroll the panel and Get a Bitmap of each scrolled content, then merge the bitmap parts ?

    public void SaveBitmap(System.Windows.Forms.Panel CtrlToSave, string fileName)
    {
        Point oldPosition = new Point(this.HorizontalScroll.Value, this.VerticalScroll.Value);

        CtrlToSave.PerformLayout();

        ComposedImage ci = new ComposedImage(new Size(CtrlToSave.DisplayRectangle.Width, CtrlToSave.DisplayRectangle.Height));

        int visibleWidth = CtrlToSave.Width - (CtrlToSave.VerticalScroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0);
        int visibleHeightBuffer = CtrlToSave.Height - (CtrlToSave.HorizontalScroll.Visible ? SystemInformation.HorizontalScrollBarHeight : 0);

        //int Iteration = 0;

        for (int x = CtrlToSave.DisplayRectangle.Width-visibleWidth; x >= 0 ; x -= visibleWidth)
        {               

            int visibleHeight = visibleHeightBuffer;

            for (int y = CtrlToSave.DisplayRectangle.Height-visibleHeight; y >= 0 ; y -= visibleHeight)
            {
                CtrlToSave.HorizontalScroll.Value = x;
                CtrlToSave.VerticalScroll.Value = y;

                CtrlToSave.PerformLayout();

                Bitmap bmp = new Bitmap(visibleWidth, visibleHeight);

                CtrlToSave.DrawToBitmap(bmp, new Rectangle(0, 0, visibleWidth, visibleHeight));
                ci.images.Add(new ImagePart(new Point(x,y),bmp));

                ///Show image parts
                //using (Graphics grD = Graphics.FromImage(bmp))
                //{
                //    Iteration++;
                //    grD.DrawRectangle(Pens.Blue,new Rectangle(0,0,bmp.Width-1,bmp.Height-1));
                    //grD.DrawString("x:"+x+",y:"+y+",W:"+visibleWidth+",H:"+visibleHeight + " I:" + Iteration,new Font("Segoe UI",9f),Brushes.Red,new Point(2,2));                        
                //}

                if (y - visibleHeight < (CtrlToSave.DisplayRectangle.Height % visibleHeight))
                    visibleHeight = CtrlToSave.DisplayRectangle.Height % visibleHeight;

                if (visibleHeight == 0)
                    break;
            }

            if (x - visibleWidth < (CtrlToSave.DisplayRectangle.Width % visibleWidth))
                visibleWidth = CtrlToSave.DisplayRectangle.Width % visibleWidth;                
            if (visibleWidth == 0)
                break;
        }

        Bitmap img = ci.composeImage();
        img.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);

        CtrlToSave.HorizontalScroll.Value = oldPosition.X;
        CtrlToSave.VerticalScroll.Value = oldPosition.Y;
    }



public class ComposedImage
{
    public Size dimensions;
    public List<ImagePart> images;

    public ComposedImage(Size dimensions)
    {
        this.dimensions = dimensions;
        this.images = new List<ImagePart>();
    }

    public ComposedImage(Size dimensions, List<ImagePart> images)
    {
        this.dimensions = dimensions;
        this.images = images;
    }

    public Bitmap composeImage()
    {
        if (dimensions == null || images == null)
            return null;

        Bitmap fullbmp = new Bitmap(dimensions.Width, dimensions.Height);
        using (Graphics grD = Graphics.FromImage(fullbmp))
        {
            foreach (ImagePart bmp in images)
            {
                grD.DrawImage(bmp.image, bmp.location.X, bmp.location.Y);
            }
        }
        return fullbmp;
    }
}

public class ImagePart
{
    public Point location;
    public Bitmap image;

    public ImagePart(Point location, Bitmap image)
    {
        this.location = location;
        this.image = image;
    }
}

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941455

The bitmap is too small of course, make it as large as panel.DisplayRectangle. Don't draw the panel if you don't want to get the scrollbars in the image.

Upvotes: 1

Related Questions