0

I want to get a screen shot from different URL. For this task, i am using System.Net.WebRequest and System.Net.WebResponse and get System.IO.StreamReader

But as follow code giving error:

System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

Parameter is not valid.

System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +397 System.Drawing.Image.FromStream(Stream stream) +9 WebApplication10.WebForm1.ConvertHtmlToImage() in E:\Prectices\WebApplication10\WebApplication10\WebForm1.aspx.cs:124 WebApplication10.WebForm1.Button1_Click(Object sender, EventArgs e) in E:\Prectices\WebApplication10\WebApplication10\WebForm1.aspx.cs:104 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +109 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +109 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +31 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3466


My code is as follow:

string content = "";
            string url = "http://www.contoso.com/PostAccepter.aspx";
            System.Net.WebRequest webRequest = WebRequest.Create(url);
            System.Net.WebResponse webResponse = webRequest.GetResponse();
            System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
            content = sr.ReadToEnd();
            //save to file
            //byte[] b = Convert.FromBase64String(content);
            byte[] b = Encoding.UTF8.GetBytes(content);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            img.Save(@"E:\Test1.png\image.png", System.Drawing.Imaging.ImageFormat.Jpeg);

            img.Dispose();
            ms.Close();
  • The line `img.Save(@"E:\Test1.png\image.png", System.Drawing.Imaging.ImageFormat.Jpeg);` won't work because you're specifying a filename twice in the path – Braydie Jun 29 '17 at 07:59
  • `E:\Test1.png\image.png` is certainly an invalid directory path, it should be full path & file name: `img.Save(@"E:\Test1\image.png", System.Drawing.Imaging.ImageFormat.Jpeg);`. Also consider `using` statement for `MemoryStream` related actions - always dispose finished streams to free up system resources afterwards. – Tetsuya Yamamoto Jun 29 '17 at 08:08
  • I suspect you want to use `BinaryReader` rather than `StreamReader`. See https://stackoverflow.com/a/10353961/34092 – mjwills Jun 29 '17 at 08:12
  • Is the URL you are using actually returning a valid binary image data? If the URL just returns some kind of website this will not work. What exactly are you trying to do? Do you want to create a screenshot of a website? – bassfader Jun 29 '17 at 08:48
  • Yes, I want to get a screen shot of website. Because the URL returns a MAP in HTML format. – Rahmany Usama Jun 29 '17 at 09:09
  • Well you cannot load a `System.Drawing.Image` from HTML mark-up, you'll need valid binary image data. So to take a screenshot you'll first need to render the HTML into an image in someway. I guess one possibility would be to use a `WebBrowser` control (or something similar) to render the HTML of the page into a valid image. – bassfader Jun 29 '17 at 09:31
  • Possible duplicate of [Convert HTML string to image](https://stackoverflow.com/questions/17832304/convert-html-string-to-image) – mjwills Jun 29 '17 at 09:56
  • Is there any way to convert HTML to image, I found HtmlRenderer.HtmlRender.Render but I didn't get the library. Can you give me any link to download it? – Rahmany Usama Jun 29 '17 at 11:05
  • https://htmlrenderer.codeplex.com/releases/view/134958 – mjwills Jun 29 '17 at 11:53
  • You can also simply install the `HtmlRenderer` library using NuGet. – bassfader Jun 29 '17 at 12:19
  • I used HtmlRender.Render() method but it gives error HtmlRender.cs is not found. Please, i think that I am in the final step. – Rahmany Usama Jun 29 '17 at 13:32

0 Answers0