Skip to main content Skip to footer

Render Image of UI Control in C1PrintDocument

First step is to capture an image of the control on the form with optional zoom setting.

// Method to convert control into an image  
static Image GetControlImage(Control ctl, float zoom)  
{  
    // get image  
    Rectangle rc = ctl.ClientRectangle;  
    Bitmap bmp = new Bitmap(rc.Width, rc.Height);  
    ctl.DrawToBitmap(bmp, rc);  

    // apply zoom  
    if (zoom != 1)  
    {  
        Size newSize = Size.Round(new SizeF(bmp.Width * zoom, bmp.Height * zoom));  
        bmp = new Bitmap(bmp, newSize);  
    }  

    // return image  
    return bmp;  
}

Second step is to draw the image into a C1PrintDocument component.

//PrintDocument for C1PrintPreviewControl  
C1PrintDocument doc = new C1PrintDocument();  
doc.DefaultUnit = UnitTypeEnum.Inch;  
doc.PageLayout.PageSettings.TopMargin = 0.5;  
doc.PageLayout.PageSettings.LeftMargin = 0.5;  
doc.PageLayout.PageSettings.RightMargin = 0.5;  
doc.PageLayout.PageSettings.BottomMargin = 0.5;   

//Renders C1FlexGrid control image to document
RenderImage ri = new RenderImage();  
ri.Image = GetControlImage(c1FlexGrid1, 1);  
ri.Width = doc.PageLayout.PageSettings.Width.Value-1;  
doc.Body.Children.Add(ri);