public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
 {
     this.CheckSecurity();
     base.OnStartPage(document, e);
     try
     {
         IntSecurity.AllPrintingAndUnmanagedCode.Assert();
         e.PageSettings.CopyToHdevmode((IntPtr) base.modeHandle);
         Size size = e.PageBounds.Size;
         Size size2 = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);
         Metafile image = new Metafile(this.dc.Hdc, new Rectangle(0, 0, size2.Width, size2.Height), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusOnly);
         PreviewPageInfo info = new PreviewPageInfo(image, size);
         this.list.Add(info);
         PrintPreviewGraphics graphics = new PrintPreviewGraphics(document, e);
         this.graphics = Graphics.FromImage(image);
         if ((this.graphics != null) && document.OriginAtMargins)
         {
             int deviceCaps = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x58);
             int num2 = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 90);
             int num3 = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x70);
             int num4 = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, this.dc.Hdc), 0x71);
             float num5 = (num3 * 100) / deviceCaps;
             float num6 = (num4 * 100) / num2;
             this.graphics.TranslateTransform(-num5, -num6);
             this.graphics.TranslateTransform((float) document.DefaultPageSettings.Margins.Left, (float) document.DefaultPageSettings.Margins.Top);
         }
         this.graphics.PrintingHelper = graphics;
         if (this.antiAlias)
         {
             this.graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
             this.graphics.SmoothingMode = SmoothingMode.AntiAlias;
         }
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     return this.graphics;
 }
Пример #2
0
        public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e) {
            Debug.Assert(dc != null && graphics == null, "PrintController methods called in the wrong order?");

            // For security purposes, don't assume our public methods methods are called in any particular order
            CheckSecurity();

            base.OnStartPage(document, e);

            
            try {
                IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                e.PageSettings.CopyToHdevmode(modeHandle);
                Size size = e.PageBounds.Size;

                // Metafile framing rectangles apparently use hundredths of mm as their unit of measurement,
                // instead of the GDI+ standard hundredth of an inch.
                Size metafileSize = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);

                // Create a Metafile which accepts only GDI+ commands since we are the ones creating
                // and using this ...
                // Framework creates a dual-mode EMF for each page in the preview. 
                // When these images are displayed in preview, 
                // they are added to the dual-mode EMF. However, 
                // GDI+ breaks during this process if the image 
                // is sufficiently large and has more than 254 colors. 
                // This code path can easily be avoided by requesting
                // an EmfPlusOnly EMF..
                Metafile metafile = new Metafile(dc.Hdc, new Rectangle(0,0, metafileSize.Width, metafileSize.Height), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusOnly);
    
                PreviewPageInfo info = new PreviewPageInfo(metafile, size);
                list.Add(info);
                PrintPreviewGraphics printGraphics = new PrintPreviewGraphics(document, e);
                graphics = Graphics.FromImage(metafile);

                if (graphics != null && document.OriginAtMargins) {
                    
                    // Adjust the origin of the graphics object to be at the
                    // user-specified margin location
                    //
                    int dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSX);
                    int dpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSY);
                    int hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX);
                    int hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY);                
                    float hardMarginX = hardMarginX_DU * 100 / dpiX;
                    float hardMarginY = hardMarginY_DU * 100 / dpiY;
                    
                    graphics.TranslateTransform(-hardMarginX, -hardMarginY);
                    graphics.TranslateTransform(document.DefaultPageSettings.Margins.Left, document.DefaultPageSettings.Margins.Top);
                }


                graphics.PrintingHelper = printGraphics;
                
    
                if (antiAlias) {
                    graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            return graphics;
        }