Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Media / Imaging / BitmapSourceSafeMILHandle.cs / 1305600 / BitmapSourceSafeMILHandle.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // A sub-class of SafeMILHandle that can estimate size for bitmap // source objects. //--------------------------------------------------------------------------- using System; using System.Diagnostics; using System.Security; using MS.Internal; using MS.Win32; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media.Imaging { ////// Constructor which computes size of the handle and delegates /// to baseclass safe handle. /// internal class BitmapSourceSafeMILHandle : SafeMILHandle { ////// Critical - initializes critical static field (autogenerated ctor) /// TreatAsSafe - sets them to the correct values, it's ok /// [SecurityCritical, SecurityTreatAsSafe] static BitmapSourceSafeMILHandle() { } ////// Use this constructor if the handle isn't ready yet and later /// set the handle with SetHandle. /// ////// Critical: This derives from a class that has a link demand and inheritance demand /// TreatAsSafe: Ok to call constructor /// [SecurityCritical,SecurityTreatAsSafe] internal BitmapSourceSafeMILHandle() : base() { } ////// Use this constructor if the handle exists at construction time. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: It is used to keep memory around /// [SecurityCritical] internal BitmapSourceSafeMILHandle(IntPtr handle) : base() { SetHandle(handle); } ////// Use this constructor if the handle exists at construction time and memory pressure /// should be shared with another SafeMILHandle. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: It is used to keep memory around /// [SecurityCritical] internal BitmapSourceSafeMILHandle(IntPtr handle, SafeMILHandle copyMemoryPressureFrom) : this(handle) { CopyMemoryPressure(copyMemoryPressureFrom); } ////// Calculate the rough size for this handle /// ////// Critical - access unmanaged code /// TreatAsSafe - queries size of the bitmap, safe operation /// /// Attributes are required for UnsafeNativeMethods.* calls /// [SecurityCritical,SecurityTreatAsSafe] internal void CalculateSize() { UpdateEstimatedSize(ComputeEstimatedSize(handle)); } ////// Compute a rough estimate of the size in bytes for the image /// ////// Critical - access unmanaged code and takes an IntPtr /// [SecurityCritical] private static long ComputeEstimatedSize(IntPtr bitmapObject) { long estimatedSize = 0; if (bitmapObject != null && bitmapObject != IntPtr.Zero) { IntPtr wicBitmap; // // QueryInterface for the bitmap source to ensure we are // calling through the right vtable on the pinvoke. // int hr = UnsafeNativeMethods.MILUnknown.QueryInterface( bitmapObject, ref _uuidBitmap, out wicBitmap ); if (hr == HRESULT.S_OK) { Debug.Assert(wicBitmap != IntPtr.Zero); // // The safe handle will release the ref added by the above QI // // There's no need to copy memory pressure. Partly because this SafeMILHandle // is temporary and will be collected after this method returns, partly // because there might no memory pressure calculated yet. // SafeMILHandle bitmapSourceSafeHandle = new SafeMILHandle(wicBitmap); uint pixelWidth = 0; uint pixelHeight = 0; hr = UnsafeNativeMethods.WICBitmapSource.GetSize( bitmapSourceSafeHandle, out pixelWidth, out pixelHeight); if (hr == HRESULT.S_OK) { Guid guidFormat; hr = UnsafeNativeMethods.WICBitmapSource.GetPixelFormat(bitmapSourceSafeHandle, out guidFormat); if (hr == HRESULT.S_OK) { // // Go to long space to avoid overflow and check for overflow // PixelFormat pixelFormat = new PixelFormat(guidFormat); long scanlineSize = (long)pixelWidth * pixelFormat.InternalBitsPerPixel / 8; // // Check that scanlineSize is small enough that we can multiply by pixelHeight // without an overflow. Since pixelHeight is a 32-bit value and we multiply by pixelHeight, // then we can only have a 32-bit scanlineSize. Since we need a sign bit as well, // we need to check that scanlineSize can fit in 30 bits. // if (scanlineSize < 0x40000000) { estimatedSize = pixelHeight * scanlineSize; } } } } } return estimatedSize; } ////// This is overridden to prevent JIT'ing due to new behavior in the CLR. (#708970) /// ////// Critical - Calls into other SecurityCritical code. not treat as safe because /// the caller must validate that the underlying handle is a valid COM object. /// [SecurityCritical] protected override bool ReleaseHandle() { return base.ReleaseHandle(); } ////// Guid for IWICBitmapSource /// ////// Critical - guid used for COM interop, need to be careful not to overwrite /// [SecurityCritical] private static Guid _uuidBitmap = MILGuidData.IID_IWICBitmapSource; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // A sub-class of SafeMILHandle that can estimate size for bitmap // source objects. //--------------------------------------------------------------------------- using System; using System.Diagnostics; using System.Security; using MS.Internal; using MS.Win32; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media.Imaging { ////// Constructor which computes size of the handle and delegates /// to baseclass safe handle. /// internal class BitmapSourceSafeMILHandle : SafeMILHandle { ////// Critical - initializes critical static field (autogenerated ctor) /// TreatAsSafe - sets them to the correct values, it's ok /// [SecurityCritical, SecurityTreatAsSafe] static BitmapSourceSafeMILHandle() { } ////// Use this constructor if the handle isn't ready yet and later /// set the handle with SetHandle. /// ////// Critical: This derives from a class that has a link demand and inheritance demand /// TreatAsSafe: Ok to call constructor /// [SecurityCritical,SecurityTreatAsSafe] internal BitmapSourceSafeMILHandle() : base() { } ////// Use this constructor if the handle exists at construction time. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: It is used to keep memory around /// [SecurityCritical] internal BitmapSourceSafeMILHandle(IntPtr handle) : base() { SetHandle(handle); } ////// Use this constructor if the handle exists at construction time and memory pressure /// should be shared with another SafeMILHandle. /// SafeMILHandle owns the release of the parameter. /// ////// Critical: It is used to keep memory around /// [SecurityCritical] internal BitmapSourceSafeMILHandle(IntPtr handle, SafeMILHandle copyMemoryPressureFrom) : this(handle) { CopyMemoryPressure(copyMemoryPressureFrom); } ////// Calculate the rough size for this handle /// ////// Critical - access unmanaged code /// TreatAsSafe - queries size of the bitmap, safe operation /// /// Attributes are required for UnsafeNativeMethods.* calls /// [SecurityCritical,SecurityTreatAsSafe] internal void CalculateSize() { UpdateEstimatedSize(ComputeEstimatedSize(handle)); } ////// Compute a rough estimate of the size in bytes for the image /// ////// Critical - access unmanaged code and takes an IntPtr /// [SecurityCritical] private static long ComputeEstimatedSize(IntPtr bitmapObject) { long estimatedSize = 0; if (bitmapObject != null && bitmapObject != IntPtr.Zero) { IntPtr wicBitmap; // // QueryInterface for the bitmap source to ensure we are // calling through the right vtable on the pinvoke. // int hr = UnsafeNativeMethods.MILUnknown.QueryInterface( bitmapObject, ref _uuidBitmap, out wicBitmap ); if (hr == HRESULT.S_OK) { Debug.Assert(wicBitmap != IntPtr.Zero); // // The safe handle will release the ref added by the above QI // // There's no need to copy memory pressure. Partly because this SafeMILHandle // is temporary and will be collected after this method returns, partly // because there might no memory pressure calculated yet. // SafeMILHandle bitmapSourceSafeHandle = new SafeMILHandle(wicBitmap); uint pixelWidth = 0; uint pixelHeight = 0; hr = UnsafeNativeMethods.WICBitmapSource.GetSize( bitmapSourceSafeHandle, out pixelWidth, out pixelHeight); if (hr == HRESULT.S_OK) { Guid guidFormat; hr = UnsafeNativeMethods.WICBitmapSource.GetPixelFormat(bitmapSourceSafeHandle, out guidFormat); if (hr == HRESULT.S_OK) { // // Go to long space to avoid overflow and check for overflow // PixelFormat pixelFormat = new PixelFormat(guidFormat); long scanlineSize = (long)pixelWidth * pixelFormat.InternalBitsPerPixel / 8; // // Check that scanlineSize is small enough that we can multiply by pixelHeight // without an overflow. Since pixelHeight is a 32-bit value and we multiply by pixelHeight, // then we can only have a 32-bit scanlineSize. Since we need a sign bit as well, // we need to check that scanlineSize can fit in 30 bits. // if (scanlineSize < 0x40000000) { estimatedSize = pixelHeight * scanlineSize; } } } } } return estimatedSize; } ////// This is overridden to prevent JIT'ing due to new behavior in the CLR. (#708970) /// ////// Critical - Calls into other SecurityCritical code. not treat as safe because /// the caller must validate that the underlying handle is a valid COM object. /// [SecurityCritical] protected override bool ReleaseHandle() { return base.ReleaseHandle(); } ////// Guid for IWICBitmapSource /// ////// Critical - guid used for COM interop, need to be careful not to overwrite /// [SecurityCritical] private static Guid _uuidBitmap = MILGuidData.IID_IWICBitmapSource; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PropertyCondition.cs
- DataObjectSettingDataEventArgs.cs
- HostSecurityManager.cs
- LogLogRecord.cs
- ThreadExceptionDialog.cs
- XamlDebuggerXmlReader.cs
- ProtectedConfigurationProviderCollection.cs
- UInt64Converter.cs
- CircleEase.cs
- DataGridViewCellEventArgs.cs
- MailAddressCollection.cs
- InfoCardCryptoHelper.cs
- ZipIORawDataFileBlock.cs
- FrameworkRichTextComposition.cs
- XmlSecureResolver.cs
- FirstMatchCodeGroup.cs
- SharedMemory.cs
- PipelineModuleStepContainer.cs
- TabControl.cs
- DiscoveryVersionConverter.cs
- SqlSelectClauseBuilder.cs
- CodeCastExpression.cs
- StringDictionaryWithComparer.cs
- hresults.cs
- DataGridViewTextBoxCell.cs
- CredentialCache.cs
- ListViewDeleteEventArgs.cs
- GridViewRowCollection.cs
- TextMetrics.cs
- InternalBase.cs
- JsonEncodingStreamWrapper.cs
- Int32CollectionConverter.cs
- ClonableStack.cs
- BitSet.cs
- KeyGesture.cs
- TextEditorCharacters.cs
- QueryContinueDragEvent.cs
- DrawingGroup.cs
- UnsafeNativeMethods.cs
- GestureRecognizer.cs
- AuthenticatingEventArgs.cs
- NavigationPropertyEmitter.cs
- OptimizedTemplateContentHelper.cs
- RenderCapability.cs
- ProxyElement.cs
- GroupBoxAutomationPeer.cs
- ValidationRule.cs
- RowUpdatingEventArgs.cs
- Options.cs
- UIElement3D.cs
- FormatVersion.cs
- WebPartTransformerAttribute.cs
- Setter.cs
- _LocalDataStore.cs
- EpmSyndicationContentSerializer.cs
- HwndSourceParameters.cs
- KnownAssembliesSet.cs
- _Rfc2616CacheValidators.cs
- EnumConverter.cs
- DeliveryStrategy.cs
- Hash.cs
- DataTemplate.cs
- NavigationProperty.cs
- StickyNoteAnnotations.cs
- DataGridViewBand.cs
- SoapExtensionReflector.cs
- ProvidersHelper.cs
- PrePostDescendentsWalker.cs
- SettingsPropertyWrongTypeException.cs
- COM2ColorConverter.cs
- ActivityExecutionContext.cs
- ClockGroup.cs
- XmlSchemaObjectTable.cs
- BoundField.cs
- MethodExpression.cs
- SqlClientWrapperSmiStreamChars.cs
- DataGrid.cs
- OracleConnectionString.cs
- Section.cs
- EdmComplexTypeAttribute.cs
- TableLayoutStyleCollection.cs
- ToolbarAUtomationPeer.cs
- DataGridViewComboBoxColumn.cs
- HostingPreferredMapPath.cs
- SqlOuterApplyReducer.cs
- FloaterBaseParagraph.cs
- Setter.cs
- webbrowsersite.cs
- Encoding.cs
- DataGridViewImageColumn.cs
- CatalogPart.cs
- SecurityCriticalDataForSet.cs
- DataRowChangeEvent.cs
- PingOptions.cs
- MulticastOption.cs
- ResXFileRef.cs
- FileLogRecordEnumerator.cs
- Focus.cs
- FrugalMap.cs
- PrintDialog.cs