Html to Image...

Sometimes it's useful to generate an image from a url.


Here's how you can do it for free, without having to pay for expensive 3rd party components.


All you have to do is download wkhtmltoimage.exe from

https://wkhtmltopdf.org/downloads.html



       public static byte[] DownloadImage(string url)
        {
            return DownloadImage(url, 1024, 768);
        }

        public static byte[] DownloadImage(string url, int w, int h)
        {           
            var htmlToImageDir = Path.Combine("Your Path", "HtmlToImage");
            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = false,
                FileName = Path.Combine(htmlToImageDir, "wkhtmltoimage.exe"),
                Arguments = string.Format("--width {0} --height {1} {2} -", w, h, url),
                WorkingDirectory = htmlToImageDir
            };

            process.StartInfo = startInfo;

            process.Start();

            using (var ms = new MemoryStream())
            {
                process.StandardOutput.BaseStream.CopyTo(ms);
                process.WaitForExit();
                return ms.ToArray();
            }
        }



Enjoy!!!!

Commentaires

Posts les plus consultés de ce blog

MVC 3 Helper for Hover Images...

How to delete many-to-many relationship in Entity Framework...

How to encrypt to a url safe string...