如何保存网页为图片 [英] How to save a web page as image
问题描述
我有网页源字符串;
我怎么可以将其保存在字节[]作为图像
i have a string with webpage source; how can i save it in byte[] as image?
推荐答案
下面:?的 http://pietschsoft.com/post/2008/07/C-Generate-好康-Thumbmail-截图-Image.aspx
是如何使用WebBrowser.DrawToBitmap方法的例子。
Is an example of how to use the WebBrowser.DrawToBitmap method.
您已经生成您的位图后,您可以使用任何你想要的编码进行压缩结果
这是从MSDN如何压缩为PNG(无损,小)一个例子:< BR>
如何:编码和解码PNG图片
After you've generated your bitmap, you can compress it using any encoding you want.
This is an example from MSDN for how to compress to PNG (lossless and small):
How to: Encode and Decode a PNG Image
祝你好运:)
编辑:结果
。为了得到字节数组,你可能想使用一个内存流的输出流
In order to get the byte array, you might want to use a memory stream as output stream.
下面是如何这样的事情会工作的工作例如:
Here's a working example of how something like that would work:
public static void Main(string[] args)
{
byte[] test = new byte[] { 2, 5, 6, 1, 9 };
MemoryStream ms = new MemoryStream();
ms.Write(test, 0, 5);
byte[] image = new byte[ms.Length];
Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length);
for (int i = 0; i < ms.Length; i++)
Console.WriteLine(image[i]);
Console.ReadKey();
}
和这里有一个如何它会在你的情况下,工作的范例:
And here's an example of how it'd work in your case:
public static void Main(string[] args)
{
MemoryStream ms = new MemoryStream();
// You have a PNGBitmapEncoder, and you call this:
encoder.Save(ms);
byte[] image = new byte[ms.Length];
Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length);
for (int i = 0; i < ms.Length; i++)
Console.WriteLine(image[i]);
Console.ReadKey();
}
这篇关于如何保存网页为图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!