Пример #1
0
 public HTMLCapture()
 {
     wb = new OutlookCustomPrintTest.CustomWebBrowser();
     wb.DocumentCompleted     += wb_DocumentCompleted;
     wb.ScrollBarsEnabled      = false;
     wb.ScriptErrorsSuppressed = true;
     wb.Navigate("about:blank");
     wb.Refresh();
 }
Пример #2
0
        public System.Drawing.Imaging.Metafile ToMetaFile_(string html, double width, double height, double marginL, double marginT, double marginR, double marginB)
        {
            double w             = width - marginL - marginR;
            double h             = height - marginT - marginB;
            string pageSizeStyle = string.Format("width: {0}mm;height: {1}mm;margin-left: 0px;margin-right: 0px;", w, h);

            wb.Document.OpenNew(false);
            wb.Document.Write(html);
            wb.Refresh();

            wb.Document.Body.Style = pageSizeStyle;
            wb.Refresh();

            wb.Width  = wb.Document.Body.ScrollRectangle.Width;
            wb.Height = wb.Document.Body.ScrollRectangle.Height;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            using (var graHandle = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
            {
                double dpix = graHandle.DpiX;
                double dpiy = graHandle.DpiY;
                var    hdc  = graHandle.GetHdc();
                System.Drawing.Imaging.Metafile meta
                    = new System.Drawing.Imaging.Metafile(ms, hdc, new System.Drawing.RectangleF(0, 0, (float)w, (float)h), System.Drawing.Imaging.MetafileFrameUnit.Millimeter);
                using (var g = Graphics.FromImage(meta))
                {
                }
                double hres = meta.HorizontalResolution;
                double vres = meta.VerticalResolution;

                meta = new System.Drawing.Imaging.Metafile(ms, hdc, new System.Drawing.RectangleF(0, 0, (float)w, (float)h), System.Drawing.Imaging.MetafileFrameUnit.Millimeter);
                using (var g = Graphics.FromImage(meta))
                {
                    double scale = hres / 25.4;
                    RECT   r     = new RECT();
                    r.Left   = 0;
                    r.Top    = 0;
                    r.Right  = (int)w;
                    r.Bottom = (int)((double)w * (double)wb.Document.Body.ScrollRectangle.Height / (double)wb.Document.Body.ScrollRectangle.Width);
                    r.Right *= 10;
                    //g.PageUnit = GraphicsUnit.Millimeter;
                    try
                    {
                        var hdc2 = g.GetHdc();
                        try
                        {
                            var unknown = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(wb.ActiveXInstance);
                            try
                            {
                                OleDraw(unknown, (int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT, hdc2, ref r);
                            }
                            finally
                            {
                                System.Runtime.InteropServices.Marshal.Release(unknown);
                            }
                        }
                        finally
                        {
                            g.ReleaseHdc(hdc2);
                        }
                        for (int x = 0; x <= width; x += 10)
                        {
                            g.DrawLine(System.Drawing.Pens.Red, new System.Drawing.Point(x, 0), new System.Drawing.Point(x, 297));
                        }
                    }
                    finally
                    {
                        graHandle.ReleaseHdc(hdc);
                    }
                }
                meta.Dispose();
            }

            ms.Position = 0;
            using (System.IO.FileStream fs = System.IO.File.OpenWrite("j:\\test.emf"))
            {
                ms.WriteTo(fs);
            }
            ms.Position = 0;

            return(System.Drawing.Imaging.Metafile.FromStream(ms) as System.Drawing.Imaging.Metafile);
        }