Convert a HTML Control (Div or Table) to an image using C#

后端 未结 6 1081
滥情空心
滥情空心 2020-12-14 03:52

Is it possible to convert a Html Control to an image in C#?

Is there any C# method where I can pass the Html Control object and return an image of that html control?

6条回答
  •  遥遥无期
    2020-12-14 04:09

    Consider this (untested!) library over at guangmingsoft called htmlsnapshot.

    add a reference to the htmlsnap2.dll

    There's a sample project there for download.

    Here's their sample code, lifted straight from that link:

    snap = new CHtmlSnapClass();
    snap.Url("www.google.com", "*")
    byte[] data = (byte[])snap.GetImageBytes(".jpg");
    //byte[] data = (byte[])snap.GetThumbImageBytes(".jpg", 100, 100, 1);
    
    
    FileStream fs = File.OpenWrite(@"c:\1.jpg");
    BinaryWriter br = new BinaryWriter(fs);
    br.Write(data);
    br.Close();
    fs.Close();
    

    Update If you wanted only a particular control, you could write yourself a page whose job is to re-render your target control as the only bits of HTML on the page.

提交回复
热议问题