您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

C#表格字符串转图片处理方法

wiki 2023/1/6 17:29:56 dotnet 字数 3268 阅读 1920

C#表格字符串转图片处理方法
 public class TxtToImgHelper
    {
        WebBrowser webBrowser = null;
        private string _mbname = string.Empty;
        private string httpvisitfile = string.Empty; 
        private bool isend = false;
        public string ConvertToImg(DataTable dt, string mbname)
        {
            if (dt.Rows.Count == 0) return "";
            _mbname = mbname;
            string html2 = @"
 
<table style='width:500px;'>
<thead><tr><th>月份</th><th>交易额</th><th>欠款</th><th>回款</th></tr></thead>
<tbody> {1}</tbody></table> "; 
            var tbody = new StringBuilder();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var dr = dt.Rows[i];
                tbody.Append($"<tr><td>{dr["M"]}</td><td>{dr["OrderMoney"]}</td><td>{dr["OweMoney"]}</td><td>{dr["PayedMoney"]}</td></tr>");
            }

            var html = html2.Replace("{0}", mbname);
            html = html.Replace("{1}", tbody.ToStr()); 
            isend = false;
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadHand));
            thread.TrySetApartmentState(System.Threading.ApartmentState.STA);
            thread.Start(html);
            var totalsecond = 0;
            while (!isend)
            {
                if (isend) break;
                if (totalsecond >= 3000) break;
                System.Threading.Thread.Sleep(100);
                totalsecond += 100;
            }
          
            return httpvisitfile;

        }
        private void ThreadHand(object html22)
        {
            webBrowser = new WebBrowser();//是否显式滚动条 
            webBrowser.ScrollBarsEnabled = false;//加载 html 
            webBrowser.DocumentText = html22.ToString();//页面加载完成执行事件 
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
            webBrowser.Dispose();
            isend = true;
        }

        private void webBrowser_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作

        {//获取解析后HTML的大小

            System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle; int width = rectangle.Width;
            int height = rectangle.Height;//设置解析后HTML的可视区域

            webBrowser.Width = width;

            webBrowser.Height = height;

            Bitmap bitmap = new System.Drawing.Bitmap(width, height);

            webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, width, height));//设置图片文件保存路径和图片格式,格式可以自定义
  
            var mm = _mbname;
            var temp = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
            if (!System.IO.Directory.Exists(temp)) System.IO.Directory.CreateDirectory(temp);
            var filename = _mbname + Utils.GetGuidFileName() + ".jpg";
            string filePath = temp + filename;

            bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);// 
          

        }
    }


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶