コード例 #1
0
        private static Rect GetImageableRect(PrintDialog dialog)
        { 
            Invariant.Assert(dialog != null, "Dialog should not be null."); 

            PrintQueue queue = null; 
            PrintCapabilities capabilities = null;
            PageImageableArea imageableArea = null;
            Rect imageableRect = Rect.Empty;
 
            // This gets the PringDocumentImageableArea.OriginWidth/OriginHeight
            // of the PrintQueue the user chose in the dialog. 
            CodeAccessPermission printp = new System.Drawing.Printing.PrintingPermission(PrintingPermissionLevel.DefaultPrinting); 
            printp.Assert();//Blessed Assert to get PrintQueue and call GetPrintCapabilities
            try 
            {
                queue = dialog.PrintQueue;
                if (queue != null)
                { 
                    capabilities = queue.GetPrintCapabilities();
                } 
            } 
            finally
            { 
                CodeAccessPermission.RevertAssert();
            }

            if (capabilities != null) 
            {
                imageableArea = capabilities.PageImageableArea; 
                if (imageableArea != null) 
                {
                    imageableRect = new Rect(imageableArea.OriginWidth, 
                                             imageableArea.OriginHeight,
                                             imageableArea.ExtentWidth,
                                             imageableArea.ExtentHeight);
                } 
            }
 
            // If for any reason we couldn't get the actual printer's values 
            // we fallback to a constant and the values available from the
            // PrintDialog. 
            if (imageableRect == Rect.Empty)
            {
                imageableRect = new Rect(NON_PRINTABLE_MARGIN,
                    NON_PRINTABLE_MARGIN, 
                    dialog.PrintableAreaWidth,
                    dialog.PrintableAreaHeight); 
            } 

            return imageableRect; 
        }