如何将网页另存为图像

2024-01-09

我有一个带有网页源的字符串; 我怎样才能将它保存在 byte[] 中作为图像?


Here: http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx

是如何使用 WebBrowser.DrawToBitmap 方法的示例。

生成位图后,您可以使用任何所需的编码对其进行压缩。
这是来自 MSDN 的示例,介绍如何压缩为 PNG(无损且小):
如何:编码和解码 PNG 图像 http://msdn.microsoft.com/en-us/library/aa970062.aspx

祝你好运 :)

EDIT:
为了获取字节数组,您可能需要使用内存流作为输出流。

这是一个类似的工作示例:

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();
}

这是它在您的情况下如何工作的示例:

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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将网页另存为图像 的相关文章

随机推荐