asp.net – 如何将网页转换为图像?
发布时间:2020-12-16 06:43:19 所属栏目:asp.Net 来源:网络整理
导读:我想将* .aspx( HTML)页面(用户界面)转换为JPEG等图像. 我正在使用下面的代码 Protected Sub btnGet_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles btnGet.Click saveURLToImage("http://google.co.in")End SubPrivate Sub saveURLToI
我想将* .aspx(
HTML)页面(用户界面)转换为JPEG等图像.
我正在使用下面的代码 Protected Sub btnGet_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles btnGet.Click saveURLToImage("http://google.co.in") End Sub Private Sub saveURLToImage(ByVal url As String) If Not String.IsNullOrEmpty(url) Then Dim content As String = "" Dim webRequest__1 As System.Net.WebRequest = WebRequest.Create(url) Dim webResponse As System.Net.WebResponse = webRequest__1.GetResponse() Dim sr As System.IO.StreamReader = New StreamReader(webResponse.GetResponseStream(),System.Text.Encoding.GetEncoding("UTF-8")) content = sr.ReadToEnd() 'save to file Dim b As Byte() = Convert.FromBase64String(content) Dim ms As New System.IO.MemoryStream(b,b.Length) Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms) img.Save("c:pic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg) img.Dispose() ms.Close() End If End Sub 但我得到的错误为“Base-64字符串中的无效字符” 解决方法
您没有使用webRequest获取页面的渲染图像,您只能使用HTML代码.
要生成图像,请遵循以下代码(直接从this post翻录) public Bitmap GenerateScreenshot(string url) { // This method gets a screenshot of the webpage // rendered at its full size (height and width) return GenerateScreenshot(url,-1,-1); } public Bitmap GenerateScreenshot(string url,int width,int height) { // Load the webpage into a WebBrowser control WebBrowser wb = new WebBrowser(); wb.ScrollBarsEnabled = false; wb.ScriptErrorsSuppressed = true; wb.Navigate(url); while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } // Set the size of the WebBrowser control wb.Width = width; wb.Height = height; if (width == -1) { // Take Screenshot of the web pages full width wb.Width = wb.Document.Body.ScrollRectangle.Width; } if (height == -1) { // Take Screenshot of the web pages full height wb.Height = wb.Document.Body.ScrollRectangle.Height; } // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control Bitmap bitmap = new Bitmap(wb.Width,wb.Height); wb.DrawToBitmap(bitmap,new Rectangle(0,wb.Width,wb.Height)); wb.Dispose(); return bitmap; } 以下是上述方法的一些示例用法: // Generate screenshot of a webpage at 1024x768 resolution Bitmap screenshot = GenerateScreenshot("http://pietschsoft.com",1024,768); // Generate screenshot of a webpage at the webpage's full size (height and width) screenshot = GenerateScreenshot("http://pietschsoft.com"); // Display screenshot in PictureBox control pictureBox1.Image = thumbnail; /* // Save screenshot to a File screenshot.Save("screenshot.png",System.Drawing.Imaging.ImageFormat.Png); */ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC授权和使用模型类的权限
- 如何通过给定的项目值显示Motobit Multi.Dictionary中的键?
- .NET Core中Object Pool的简单使用
- asp.net – 根据自己的主机Web API Windows服务验证HTTP .N
- 如何在ASP.NET中设置TextBox中的对齐中心?
- 如何打印与phpinfo()但ASP.NET类似的信息?
- asp.net-mvc – BreadCrumb trail MVC3和Razor
- asp.net – 如何在web.config中加密连接
- asp.net-mvc-3 – 根据asp.net mvc3中的下拉列表数据获取列
- asp.net-mvc-3 – mvc3 OutputCache RemoveOutputCacheItem
推荐文章
站长推荐
- asp.net-mvc – ModelState.IsValid不排除必需的
- 如何在ASP.NET Core MVC 1.0中创建和管理插件?
- asp.net-mvc-3 – MVC3 OutputCache无法按预期在
- asp.net-mvc – ASP.NET MVC Scheduler开源?
- asp.net – SQL Server未找到或无法访问
- asp.net-mvc – 在ASP.NET MVC应用程序中使用Ent
- asp.net – 关闭/停止浏览器或选项卡关闭时的会话
- asp.net-mvc-3 – 如何重置/获取razor intellise
- asp.net-mvc – Url.Action with RouteValueDict
- asp.net – 如何崩溃App Pool?
热点阅读