Example #1
0
        /// <summary>
        /// Convert the specified HTML string to a PNG image and send the 
        /// image as an attachment to the browser
        /// </summary>
        private void ConvertHTMLStringToImage()
        {
            string htmlString = textBoxHTMLCode.Text;
            string baseURL = textBoxBaseURL.Text.Trim();

            // Create the Image converter. Optionally the HTML viewer width can be specified as parameter
            // The default HTML viewer width is 1024 pixels.
            ImgConverter imgConverter = new ImgConverter();

            // set the license key
            imgConverter.LicenseKey = "B4mYiJubiJiInIaYiJuZhpmahpGRkZE=";

            // set if the JavaScript is enabled during conversion - default is true
            imgConverter.JavaScriptEnabled = cbClientScripts.Checked;

            // Performs the conversion and get the image bytes that can be further
            // saved to a file or sent as a browser response
            //
            // The baseURL parameter helps the converter to get the CSS files and images
            // referenced by a relative URL in the HTML string.
            byte[] imgBytes = null;
            if (baseURL.Length > 0)
                imgBytes = imgConverter.GetImageBytesFromHtmlString(htmlString, System.Drawing.Imaging.ImageFormat.Png, baseURL);
            else
                imgBytes = imgConverter.GetImageBytesFromHtmlString(htmlString, System.Drawing.Imaging.ImageFormat.Png);

            // send the image as a response to the browser for download
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.Clear();
            response.AddHeader("Content-Type", "image/png");
            response.AddHeader("Content-Disposition", String.Format("attachment; filename=GettingStarted.png; size={0}", imgBytes.Length.ToString()));
            response.BinaryWrite(imgBytes);
            // Note: it is important to end the response, otherwise the ASP.NET
            // web page will render its content to image stream
            response.End();
        }