using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using CoreHtmlToImage;

namespace Mall.Common.Plugin
{
    /// <summary>
    /// htmlToimg工具类
    /// </summary>
    public class HtmlToImgHelper
    {
        //Bitmap m_Bitmap;
        //string m_Url;
        //int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
        //public HtmlToImgHelper(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        //{
        //    m_Url = Url;
        //    m_BrowserHeight = BrowserHeight;
        //    m_BrowserWidth = BrowserWidth;
        //    m_ThumbnailWidth = ThumbnailWidth;
        //    m_ThumbnailHeight = ThumbnailHeight;
        //}
        //public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        //{
        //    HtmlToImgHelper thumbnailGenerator = new HtmlToImgHelper(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
        //    return thumbnailGenerator.GenerateWebSiteThumbnailImage();
        //}
        //public Bitmap GenerateWebSiteThumbnailImage()
        //{
        //    Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
        //    m_thread.SetApartmentState(ApartmentState.STA);
        //    m_thread.Start();
        //    m_thread.Join();
        //    return m_Bitmap;
        //}
        //private void _GenerateWebSiteThumbnailImage()
        //{
        //    WebBrowser m_WebBrowser = new WebBrowser();
        //    m_WebBrowser.ScrollBarsEnabled = false;
        //    m_WebBrowser.Navigate(m_Url);
        //    m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
        //    while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
        //        Application.DoEvents();
        //    m_WebBrowser.Dispose();
        //}
        //private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        //{
        //    WebBrowser m_WebBrowser = (WebBrowser)sender;
        //    m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
        //    m_WebBrowser.ScrollBarsEnabled = false;
        //    m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
        //    m_WebBrowser.BringToFront();
        //    m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
        //    m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
        //}


        //public string GetHtmlToImgString() {
        //    Bitmap m_Bitmap = GetWebSiteThumbnail("这里是html的连接地址", 990, 2798, 990, 2798);
        //    MemoryStream ms = new MemoryStream();
        //    m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可
        //    byte[] buff = ms.ToArray();
        //    Response.BinaryWrite(buff);

        //    System.Drawing.Image img = byteArrayToImage(buff);
        //    string url = "图片保存路径.png";
        //    img.Save(url);
        //}

        public string GetHtmlToImage() {
            var converter = new HtmlConverter();
            string html = "<div>hello world</div>";
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            html = StringHelper.GetFileContent("E:/LDWork/LiuDongWork/OnLineItem/MallNew/Mall.WebApi/upfile/temimg/新建文本文档.txt", Encoding.GetEncoding("gb2312"));
            byte[] byte1 = converter.FromHtmlString(html, 385);

            string str = System.Text.Encoding.Default.GetString(byte1);

            //Bitmap bitmap = BytesToBitmap(byte1, 385, 675);
            Bitmap bitmap = BytesToBitmap2(byte1);
            string url = "E:/LDWork/LiuDongWork/OnLineItem/MallNew/Mall.WebApi/upfile/temimg/图片保存路径.jpg";
            bitmap.Save(url, System.Drawing.Imaging.ImageFormat.Jpeg);
            return url;
        }

        //public string GetHtmlToImg_V2(string fileName) {
        //    byte[] byte1 = StreamToBytes(FileToStream(fileName));
        //    Bitmap bitmap = BytesToBitmap(byte1, 385, 675);
        //    //MemoryStream ms = new MemoryStream();
        //    //bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//JPG、GIF、PNG等均可            
        //    string url = "图片保存路径.png";
        //    bitmap.Save(url, System.Drawing.Imaging.ImageFormat.Jpeg);
        //    return url;
        //}

        //byte[] 转图片  
        public static Bitmap BytesToBitmap(byte[] Bytes, int width, int heiht)
        {
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream(Bytes);
                return new Bitmap((Image)new Bitmap(stream), width, heiht);
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        public static Bitmap BytesToBitmap2(byte[] Bytes)
        {
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream(Bytes);
                return new Bitmap((Image)new Bitmap(stream));
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        //图片转byte[]   
        public static byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms, Bitmap.RawFormat);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }
        /// <summary>  
        /// 将 Stream 转成 byte[]  
        /// </summary>  
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 设置当前流的位置为流的开始  
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>  
        /// 将 byte[] 转成 Stream  
        /// </summary>  
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        /// <summary>  
        /// 将 Stream 写入文件  
        /// </summary>  
        public void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 转换成 byte[]  
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始  
            stream.Seek(0, SeekOrigin.Begin);

            // 把 byte[] 写入文件  
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary>  
        /// 从文件读取 Stream  
        /// </summary>  
        public Stream FileToStream(string fileName)
        {
            // 打开文件  
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 读取文件的 byte[]  
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            // 把 byte[] 转换成 Stream  
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
    }
}
