ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#html转图片

2021-03-04 16:01:06  阅读:90  来源: 互联网

标签:IntPtr UnmanagedType web C# int html MarshalAs new 图片


 

摘抄方法

/// <summary>
/// 类说明:HTML代码转换为图片的公共类
/// 作者:段江涛
/// 时间:2019/10/17 11:16:38
/// </summary>
public class HtmlToPicHelper
{
private static Bitmap ConverHTML(string htmPath)
{
string ImagePath = string.Empty;
WebBrowser web = new WebBrowser();
//web.Navigate(htmPath);
web.DocumentText = htmPath;
while (web.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
Rectangle screen = Screen.PrimaryScreen.Bounds;
Size? imgsize = null;
//set the webbrowser width and hight
web.Width = screen.Width;
web.Height = screen.Height;
//suppress script errors and hide scroll bars
web.ScriptErrorsSuppressed = true;
web.ScrollBarsEnabled = false;
Rectangle body = web.Document.Body.ScrollRectangle;

//check if the document width/height is greater than screen width/height
Rectangle docRectangle = new Rectangle()
{
Location = new Point(0, 0),
Size = new Size(body.Width > screen.Width ? body.Width : screen.Width,
body.Height > screen.Height ? body.Height : screen.Height)
};
//set the width and height of the WebBrowser object
web.Width = docRectangle.Width;
web.Height = docRectangle.Height;

//if the imgsize is null, the size of the image will
//be the same as the size of webbrowser object
//otherwise set the image size to imgsize
Rectangle imgRectangle;
if (imgsize == null)
imgRectangle = docRectangle;
else
imgRectangle = new Rectangle()
{
Location = new Point(0, 0),
Size = imgsize.Value
};
//create a bitmap object
Bitmap bitmap = new Bitmap(imgRectangle.Width - 24, imgRectangle.Height);
//get the viewobject of the WebBrowser
IViewObject ivo = web.Document.DomDocument as IViewObject;

using (Graphics g = Graphics.FromImage(bitmap))
{
//get the handle to the device context and draw
IntPtr hdc = g.GetHdc();
ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero, hdc, ref imgRectangle,
ref docRectangle, IntPtr.Zero, 0);
g.ReleaseHdc(hdc);
}
return bitmap;
}

public static byte[] ConverPic(string htmlPath)
{
//必须放到独立的线程里面来处理
Bitmap m_Bitmap = null;
Thread m_thread = new Thread(new ThreadStart(() =>
{
m_Bitmap = ConverHTML(htmlPath);
}));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();

if (m_Bitmap != null)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
m_Bitmap.Save(ms, ImageFormat.Jpeg);
byte[] bs = ms.GetBuffer();
ms.Close();
return bs;
}
return new byte[0];
}
}


[ComVisible(true), ComImport()]
[GuidAttribute("0000010d-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IViewObject
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int Draw(
[MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
int lindex,
IntPtr pvAspect,
[In] IntPtr ptd,
IntPtr hdcTargetDev,
IntPtr hdcDraw,
[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcBounds,
[MarshalAs(UnmanagedType.Struct)] ref Rectangle lprcWBounds,
IntPtr pfnContinue,
[MarshalAs(UnmanagedType.U4)] UInt32 dwContinue);
[PreserveSig]
int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect,
int lindex, IntPtr pvAspect, [In] IntPtr ptd,
IntPtr hicTargetDev, [Out] IntPtr ppColorSet);
[PreserveSig]
int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect,
int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
[PreserveSig]
int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);
void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects,
[In, MarshalAs(UnmanagedType.U4)] int advf,
[In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects,
[In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf,
[In, Out, MarshalAs(UnmanagedType.LPArray)] IAdviseSink[] pAdvSink);
}

 

引用:

byte[] bmp = ConsoleApplication1.HtmlToPicHelper.ConverPic(this.TextBox1.Text);
Response.BinaryWrite(bmp);
System.IO.File.WriteAllBytes(@"D:\test.jpg", bmp);

 

标签:IntPtr,UnmanagedType,web,C#,int,html,MarshalAs,new,图片
来源: https://www.cnblogs.com/DaSir/p/14480791.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有