Example #1
0
        /// <summary>
        /// Converts the specified html documents to a single image.
        /// </summary>
        /// <param name="htmlDocuments"></param>
        /// <param name="pdfConverterLicenseKey"></param>
        /// <returns></returns>
        public static System.Drawing.Image ConvertHtmlToImage(IEnumerable <string> htmlDocuments, string pdfConverterLicenseKey)
        {
            var imgConverter = new ImgConverter();

            if (String.IsNullOrEmpty(pdfConverterLicenseKey) == false)
            {
                imgConverter.LicenseKey = pdfConverterLicenseKey;
            }

            System.Drawing.Image img = null;
            foreach (var htmlDocument in htmlDocuments)
            {
                if (img == null)
                {
                    img = imgConverter.GetImageFromHtmlString(htmlDocument, ImageFormat.Png);
                }
                else
                {
                    using (System.Drawing.Image tempImage = imgConverter.GetImageFromHtmlString(htmlDocument, ImageFormat.Png))
                    {
                        int newImageHeight = img.Height + tempImage.Height;
                        int newImageWidth  = img.Width > tempImage.Width ? img.Width : tempImage.Width;

                        Bitmap newBitmap = new Bitmap(newImageWidth, newImageHeight);
                        using (Graphics newImageGraphics = Graphics.FromImage(newBitmap))
                        {
                            newImageGraphics.DrawImageUnscaled(img, 0, 0);
                            newImageGraphics.DrawImageUnscaled(tempImage, 0, newImageHeight);
                        }
                        img.Dispose();
                        img = newBitmap;
                    }
                }
            }

            if (img != null)
            {
                return(img);
            }

            throw new InvalidOperationException("No image object was created.");
        }