我想从不同的网址获取屏幕截图。对于此任务,我使用System.Net.WebRequest
和System.Net.WebResponse
并获取System.IO.StreamReader
但是按照以下代码给出错误:
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
参数无效。
System.Drawing.Image.FromStream(Stream stream,Boolean useEmbeddedColorManagement,Boolean validateImageData)+397 System.Drawing.Image.FromStream(Stream stream)+9 E:\ Prectices \ WebApplication10 \ WebApplication10 \ WebForm1.aspx.cs中的WebApplication10.WebForm1.ConvertHtmlToImage():124 E:\ Prectices \ WebApplication10 \ WebApplication10 \ WebForm1.aspx.cs中的WebApplication10.WebForm1.Button1_Click(Object sender,EventArgs e):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
我的代码如下:
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();