Example #1
0
        /// <summary>
        /// Convert the HTML code from the specified URL to a PNG image and send the 
        /// image as an attachment to the browser
        /// </summary>
        private void ConvertURLToImage()
        {
            string urlToConvert = textBoxWebPageURL.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 response to browser
            byte[] imgBytes = imgConverter.GetImageBytesFromUrl(urlToConvert, 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();
        }