如何在Asp.net C#中将HTML页面输出保存为Image? [英] How do I save the HTML page output as Image in Asp.net C#?

查看:342
本文介绍了如何在Asp.net C#中将HTML页面输出保存为Image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一种情况需要将HTML div中的内容保存为Web服务器中的Image。



我试图使用Graphic类,但是那些可以将Text部分转换为Image。如果我在HTML div或任何其他对象中有任何图像,那么我会得到无效的转换错误。



我想知道是否还有其他方法可以做到这一点?



以下是我正在使用的代码:



系统.Net.WebRequest webRequest = System.Net.WebRequest.Create(url); 
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream(),System.Text.Encoding.GetEncoding( UTF-8));
content = sr.ReadToEnd();

// 保存到文件
byte [] b = StreamFile(content);


System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms, false true );
img.Save( @ D:\ pic.jpg,System.Drawing .Imaging.ImageFormat.Jpeg);

img.Dispose();
ms.Close();









谢谢任何帮助

解决方案

此链接将帮助您更好。



获取ASP.NET C#2.0网站缩略图屏幕截图 [ ^ ]



谢谢


  protected   void  Page_Load( object  sender,EventArgs e)
{
if (IsPostBack)
{
// 创建一个新的位图对象
位图oBitmap = 位图( 468 60 );
// 创建一个新的Graphics对象,这将允许我们在我们的位图上绘制:
Graphics oGraphic = Graphics.FromImage(oBitmap);

// 定义动态图片的背景颜色:
String scolor = BackgroundColor.SelectedValue;

// 获取已回发的值
< span class =code-sdkkeyword> String
sText = Text.Text;
String sFont = Font.Text;
颜色oColor = Color.FromName(BackgroundColor.SelectedValue);

// 创建两个画笔,这将有助于我们绘制图像。
// 第一个,oBrush,将用于绘制图像的背景:
// 第二个画笔将用于绘制用户输入的文本
SolidBrush oBrush = new SolidBrush(oColor);
SolidBrush oBrushwrite = new SolidBrush(Color.White);

// 填充图像矩形,
oGraphic.FillRectangle (oBrush, 0 0 468 60 );

// 用户为文本选择了字体,因此我们将sFont作为第一个参数传递,并硬编码大小:
字体oFont = 字体(sFont, 13 );
// X和Y点文本
PointF oPoint = new PointF(5f,5f);

// 绘制我们的文字:
oGraphic.DrawString( sText,oFont,oBrushwrite,oPoint);

// 将图像直接发送到浏览器
响应。 ContentType = image / jpeg;
oBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);
}
}





积分转到: http://www.sitepoint.com/generating-asp-net-images-fly/


我发现这个.NET SDK将HTML转换为.net中的图像

几行代码需要将url转换为图像。

下载此库并尝试以下代码。< br $>


http ://www.converthtmltoimage.com/Convert-html-to-png-in-c-sharp-net.html [ ^ ]



 WebsitesScreenshot.WebsitesScreenshot _Obj; 
_Obj = new WebsitesScreenshot.WebsitesScreenshot();
WebsitesScreenshot.WebsitesScreenshot.Result _Result;
_Result = _Obj.CaptureWebpage( http://www.msn.com) ;
if (_Result == WebsitesScreenshot。
WebsitesScreenshot.Result.Captured)
{
_Obj.ImageWidth = 200 ;
_Obj.ImageHeight = 300 ;
_Obj.ImageFormat =网站截图。
WebsitesScreenshot.ImageFormats.PNG;
_Obj.SaveImage( c:\\ msn.png);
}
_Obj.Dispose();


Hi all,

I have a situation where I need to save the content in HTML div as Image in web server.

I tried to use the Graphic classes, but those could convert the Text part into Image. If I have any image inside that HTML div or any other object then I get Invalid conversion error.

I would like to find out if there's any other way to do it?

Following is the code I am using:

System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.WebResponse webResponse = webRequest.GetResponse() ;
System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
content = sr.ReadToEnd();

//save to file
byte[] b = StreamFile(content );


System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms,false,true);
img.Save(@"D:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

img.Dispose();
ms.Close();





Thanks for any help

解决方案

This link will help you better.

Get ASP.NET C# 2.0 Website Thumbnail Screenshot[^]

thanks


protected void Page_Load(object sender, EventArgs e)
 {
 if (IsPostBack)
 {
 //create a new Bitmap objec
 Bitmap oBitmap = new Bitmap(468, 60);
 //create a new Graphics object, which will allow us to draw on our bitmap:
 Graphics oGraphic = Graphics.FromImage(oBitmap);

 //define the background color of our dynamic image:
 String scolor = BackgroundColor.SelectedValue;

 //get the values that were posted back
 String sText = Text.Text;
 String sFont = Font.Text;
 Color oColor = Color.FromName(BackgroundColor.SelectedValue);

 // create two brushes, which will help us to draw our image.
 //The first one, oBrush, will be used to draw the background of the image:
 // second brush will be used to draw the text entered by the user
 SolidBrush oBrush = new SolidBrush(oColor);
 SolidBrush oBrushwrite = new SolidBrush(Color.White);

 //fill the image rectangle,
 oGraphic.FillRectangle(oBrush, 0, 0, 468, 60);

 //user chose a font for the text, so we pass sFont as the first parameter, and hardcode the size:
 Font oFont = new Font(sFont, 13);
 // X and Y point of text
 PointF oPoint = new PointF(5f,5f);

 //draw our text:
 oGraphic.DrawString(sText, oFont, oBrushwrite, oPoint);

 //send image directly to the browser
 Response.ContentType = "image/jpeg";
 oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
 }
 }



Credit goes to : http://www.sitepoint.com/generating-asp-net-images-fly/


I found this .NET SDK to convert HTML to Image in .net
few lines of code require to convert url to image.
download this library and try the following code.

http://www.converthtmltoimage.com/Convert-html-to-png-in-c-sharp-net.html[^]

WebsitesScreenshot.WebsitesScreenshot _Obj;
_Obj = new WebsitesScreenshot.WebsitesScreenshot();
WebsitesScreenshot.WebsitesScreenshot.Result _Result;            
_Result = _Obj.CaptureWebpage("http://www.msn.com");
if (_Result == WebsitesScreenshot.
			WebsitesScreenshot.Result.Captured)
{
	_Obj.ImageWidth = 200;
	_Obj.ImageHeight = 300;
	_Obj.ImageFormat = WebsitesScreenshot.
		WebsitesScreenshot.ImageFormats.PNG;
	_Obj.SaveImage("c:\\msn.png");
} 
_Obj.Dispose();


这篇关于如何在Asp.net C#中将HTML页面输出保存为Image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆