DayPilot Knowledge Base

AJAX Calendar/Scheduling Controls
DayPilot Pro (AJAX Calendar Control)
» DayPilot ASP.NET Calendar
DayPilot Pro (AJAX Monthly Calendar Control)
» DayPilot ASP.NET Monthly Calendar
DayPilot Pro (AJAX Scheduler Control)
» DayPilot ASP.NET Scheduler
DayPilot » Knowledge Base » How to add a header to the exported calendar image (PNG)

How to add a header to the exported calendar image (PNG)

Last revision: May 17, 2012

monthly calendar image export

The calendar (CalendarMonth, and Scheduler) can be easily exported as an image (PNG, BMP, JPG, and other image formats):

    protected void ButtonExport_Click(object sender, EventArgs e)
    {
        DayPilotMonth1.DataSource = getData(DayPilotMonth1.VisibleStart, DayPilotMonth1.VisibleEnd, (string)DayPilotMonth1.ClientState["filter"]);
        DayPilotMonth1.DataBind();
        DayPilotMonth1.EventTextLeftIndent = 30;
        DayPilotMonth1.EventTextAlignment = TextAlignment.Left;

        Response.Clear();
        Response.ContentType = "image/png";
        Response.AddHeader("Content-Disposition", "attachment;filename=print.png");
        DayPilotMonth1.Export(ImageFormat.Png).WriteTo(Response.OutputStream);
        Response.End();
    }

If you want to add a header to the exported image (e.g. with the date or filter details) you have to draw the exported bitmap to a new one and add the header:

protected void ButtonExportHeader_Click(object sender, EventArgs e)
    {
        DayPilotMonth1.DataSource = getData(DayPilotMonth1.VisibleStart, DayPilotMonth1.VisibleEnd, (string)DayPilotMonth1.ClientState["filter"]);
        DayPilotMonth1.DataBind();
        DayPilotMonth1.EventTextLeftIndent = 30;
        DayPilotMonth1.EventTextAlignment = TextAlignment.Left;

        Response.Clear();
        Response.ContentType = "image/png";
        Response.AddHeader("content-disposition", "attachment;filename=print.png");

        int width = 1000;
        int height = 1000;

        Bitmap bmp = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(bmp);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TextRenderingHint = TextRenderingHint.AntiAlias;

        Image cal = DayPilotMonth1.ExportBitmap();
        g.DrawImage(cal, 0, 50);

        string title = String.Format("{0:MMMM yyyy}", DayPilotMonth1.StartDate);
        Font font = new Font("Tahoma", 16, GraphicsUnit.Point);
        Brush brush = new SolidBrush(Color.Black);
        g.DrawString(title, font, brush, 0, 0);

        // PNG requires random access to the output stream
        MemoryStream mem = new MemoryStream();
        bmp.Save(mem, ImageFormat.Png);
        mem.WriteTo(Response.OutputStream);

        Response.End();
    }

Related

How to force download of exported calendar/scheduler image (PNG)
Writing PNG to Response.OutputStream - System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
How to save the exported PNG image to a file (without a web page)
Sys.WebForms.PageRequestManagerParserErrorException on PNG export button click