示例#1
0
        public override void OnStartPrint(PrintDocument document, PrintEventArgs 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.OnStartPrint(document, e);


            try {
                if (!document.PrinterSettings.IsValid)
                {
                    throw new InvalidPrinterException(document.PrinterSettings);
                }

                IntSecurity.AllPrintingAndUnmanagedCode.Assert();

                // We need a DC as a reference; we don't actually draw on it.
                // We make sure to reuse the same one to improve performance.
                dc = document.PrinterSettings.CreateInformationContext(modeHandle);
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
        }
示例#2
0
        /// <include file='doc\DefaultPrintController.uex' path='docs/doc[@for="StandardPrintController.OnEndPrint"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Implements EndPrint for printing to a physical printer.
        ///    </para>
        /// </devdoc>
        public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
        {
            Debug.Assert(dc != IntPtr.Zero && 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(document);

            IntSecurity.UnmanagedCode.Assert();

            if (dc != IntPtr.Zero)
            {
                int result = (e.Cancel) ? SafeNativeMethods.AbortDoc(new HandleRef(this, dc)) : SafeNativeMethods.EndDoc(new HandleRef(this, dc));
                if (result <= 0)
                {
                    throw new Win32Exception();
                }
                bool success = SafeNativeMethods.DeleteDC(new HandleRef(this, dc));
                if (!success)
                {
                    throw new Win32Exception();
                }

                dc = IntPtr.Zero;
            }

            CodeAccessPermission.RevertAssert();

            base.OnEndPrint(document, e);
        }
示例#3
0
        public Stream OpenFile()
        {
            Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "FileDialogOpenFile Demanded");
            IntSecurity.FileDialogOpenFile.Demand();

            string filename = FileNamesInternal[0];

            if (filename == null || (filename.Length == 0))
            {
                throw new ArgumentNullException("FileName");
            }

            Stream s = null;

            // SECREVIEW : We demanded the FileDialog permission above, so it is safe
            //           : to assert this here. Since the user picked the file, it
            //           : is OK to give them readonly access to the stream.
            //
            new FileIOPermission(FileIOPermissionAccess.Read, IntSecurity.UnsafeGetFullPath(filename)).Assert();
            try
            {
                s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            return(s);
        }
示例#4
0
        public Stream OpenFile()
        {
            Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "FileDialogOpenFile Demanded");
            IntSecurity.FileDialogOpenFile.Demand();

            string filename = FileNamesInternal[0];

            if (filename == null || (filename.Length == 0))
            {
                throw new ArgumentNullException(nameof(FileName));
            }

            Stream s = null;

            //



            new FileIOPermission(FileIOPermissionAccess.Read, IntSecurity.UnsafeGetFullPath(filename)).Assert();
            try
            {
                s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            return(s);
        }
示例#5
0
        public Stream OpenFile()
        {
            Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "FileDialogSaveFile Demanded");
            IntSecurity.FileDialogSaveFile.Demand();

            string filename = FileNamesInternal[0];

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("FileName");
            }

            Stream s = null;

            //



            new FileIOPermission(FileIOPermissionAccess.AllAccess, IntSecurity.UnsafeGetFullPath(filename)).Assert();
            try {
                s = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            return(s);
        }
        public override void OnEndPrint(PrintDocument document, PrintEventArgs 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(document);
            IntSecurity.UnmanagedCode.Assert();

            try
            {
                if (dc != null)
                {
                    try {
                        int result = (e.Cancel) ? SafeNativeMethods.AbortDoc(new HandleRef(dc, dc.Hdc)) : SafeNativeMethods.EndDoc(new HandleRef(dc, dc.Hdc));
                        if (result <= 0)
                        {
                            throw new Win32Exception();
                        }
                    }
                    finally {
                        dc.Dispose();
                        dc = null;
                    }
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            base.OnEndPrint(document, e);
        }
示例#7
0
        /// <devdoc>
        ///     Initializes this image for the first time.  This should only be called once.
        /// </devdoc>
        private void Initialize(SafeNativeMethods.IPicture picture)
        {
            if (this.picture != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.IllegalState, GetType().Name));
            }

            this.picture = picture;

            // SECUNDONE : Assert shouldn't be needed, however we can't put SuppressUnmanagedCode
            //           : on the IPicture interface, so we must do an Assert.
            //
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();

            try {
                if (picture != null && picture.GetPictureType() == NativeMethods.Ole.PICTYPE_ICON)
                {
                    handle    = picture.GetHandle();
                    ownHandle = false;
                }
                else
                {
                    throw new ArgumentException(SR.GetString(SR.InvalidPictureType,
                                                             "picture",
                                                             "Cursor"), "picture");
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
        }
        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(document);

            base.OnStartPage(document, e);
            try {
                IntSecurity.AllPrintingAndUnmanagedCode.Assert();

                e.PageSettings.CopyToHdevmode(modeHandle);
                IntPtr modePointer = SafeNativeMethods.GlobalLock(new HandleRef(this, modeHandle));
                try {
                    IntPtr result = SafeNativeMethods.ResetDC(new HandleRef(this.dc, dc.Hdc), new HandleRef(null, modePointer));
                    Debug.Assert(result == dc.Hdc, "ResetDC didn't return the same handle I gave it");
                }
                finally {
                    SafeNativeMethods.GlobalUnlock(new HandleRef(this, modeHandle));
                }
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }

            // int horizontalResolution = Windows.GetDeviceCaps(dc.Hdc, SafeNativeMethods.HORZRES);
            // int verticalResolution = Windows.GetDeviceCaps(dc.Hdc, SafeNativeMethods.VERTRES);

            graphics = Graphics.FromHdcInternal(dc.Hdc);

            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(this.dc, dc.Hdc), SafeNativeMethods.LOGPIXELSX);
                int   dpiY           = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, dc.Hdc), SafeNativeMethods.LOGPIXELSY);
                int   hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX);
                int   hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(this.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);
            }


            int result2 = SafeNativeMethods.StartPage(new HandleRef(this.dc, dc.Hdc));

            if (result2 <= 0)
            {
                throw new Win32Exception();
            }
            return(graphics);
        }
        /// <include file='doc\PreviewPrintController.uex' path='docs/doc[@for="PreviewPrintController.OnEndPrint"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Implements EndPrint for generating print preview information.
        ///    </para>
        /// </devdoc>
        public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
        {
            Debug.Assert(dc != IntPtr.Zero && 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();

            IntSecurity.UnmanagedCode.Assert();

            SafeNativeMethods.DeleteDC(new HandleRef(this, dc));
            dc = IntPtr.Zero;

            CodeAccessPermission.RevertAssert();

            base.OnEndPrint(document, e);
        }
示例#10
0
        /// <include file='doc\DefaultPrintController.uex' path='docs/doc[@for="StandardPrintController.OnEndPage"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Implements EndPage for printing to a physical printer.
        ///    </para>
        /// </devdoc>
        public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
        {
            Debug.Assert(dc != IntPtr.Zero && 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(document);

            IntSecurity.UnmanagedCode.Assert();

            int result = SafeNativeMethods.EndPage(new HandleRef(this, dc));

            if (result <= 0)
            {
                throw new Win32Exception();
            }
            graphics.Dispose(); // Dispose of GDI+ Graphics; keep the DC
            graphics = null;

            CodeAccessPermission.RevertAssert();

            base.OnEndPage(document, e);
        }
示例#11
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();
                if (e.CopySettingsToDevMode)
                {
                    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);
        }