Beispiel #1
0
        internal InteropBitmap(IntPtr hbitmap, IntPtr hpalette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions, WICBitmapAlphaChannelOption alphaOptions)
            : base(true) // Use virtuals
        {
            _bitmapInit.BeginInit();

            using (FactoryMaker myFactory = new FactoryMaker())
            {
                HRESULT.Check(UnsafeNativeMethods.WICImagingFactory.CreateBitmapFromHBITMAP(
                        myFactory.ImagingFactoryPtr,
                        hbitmap,
                        hpalette,
                        alphaOptions,
                        out _unmanagedSource));
                Debug.Assert (_unmanagedSource != null && !_unmanagedSource.IsInvalid);
            }

            _unmanagedSource.CalculateSize();
            _sizeOptions = sizeOptions;
            _sourceRect = sourceRect;
            _syncObject = _unmanagedSource;

            _bitmapInit.EndInit();

            FinalizeCreation();
        }
Beispiel #2
0
        unsafe public static BitmapSource CreateBitmapSourceFromHBitmap( 
            IntPtr bitmap,
            IntPtr palette, 
            Int32Rect sourceRect, 
            BitmapSizeOptions sizeOptions)
        { 
            SecurityHelper.DemandUnmanagedCode();

            // CR: [....] (1681459)
            return CriticalCreateBitmapSourceFromHBitmap(bitmap, palette, sourceRect, sizeOptions, WICBitmapAlphaChannelOption.WICBitmapUseAlpha); 
        }
Beispiel #3
0
        unsafe internal static BitmapSource CriticalCreateBitmapSourceFromHBitmap(
            IntPtr bitmap,
            IntPtr palette,
            Int32Rect sourceRect, 
            BitmapSizeOptions sizeOptions,
            WICBitmapAlphaChannelOption alphaOptions) 
        { 
            if (bitmap == IntPtr.Zero)
            { 
                throw new ArgumentNullException("bitmap");
            }

            return new InteropBitmap(bitmap, palette, sourceRect, sizeOptions, alphaOptions); // use the critical version 
        }
Beispiel #4
0
        public static BitmapSource ConverToBitmapSource(this Bitmap b)
        {
            try
            {
                //Use existing Interop functionality to perform conversion
                IntPtr HBitmap = ((System.Drawing.Bitmap)b).GetHbitmap();

                System.Windows.Media.Imaging.BitmapSizeOptions sizeOptions =
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions();

                return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                           HBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions));
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        /// Constructs an BitmapSizeOptions that does not preserve the aspect ratio and
        /// instead uses dimensions pixelWidth x pixelHeight.
        /// </summary>
        /// <param name="rotation">Angle to rotate</param>
        public static BitmapSizeOptions FromRotation(Rotation rotation)
        {
            switch(rotation)
            {
                case Rotation.Rotate0:
                case Rotation.Rotate90:
                case Rotation.Rotate180:
                case Rotation.Rotate270:
                    break;
                default:
                    throw new ArgumentException(SR.Get(SRID.Image_SizeOptionsAngle), "rotation");
            }

            BitmapSizeOptions sizeOptions = new BitmapSizeOptions();

            sizeOptions._rotationAngle          = rotation;
            sizeOptions._preservesAspectRatio = true;
            sizeOptions._pixelWidth          = 0;
            sizeOptions._pixelHeight         = 0;

            return sizeOptions;
        }
        /// <summary>
        /// Constructs an BitmapSizeOptions that does not preserve the aspect ratio and
        /// instead uses dimensions pixelWidth x pixelHeight.
        /// </summary>
        /// <param name="pixelWidth">Width of the resulting Bitmap</param>
        /// <param name="pixelHeight">Height of the resulting Bitmap</param>
        public static BitmapSizeOptions FromWidthAndHeight(int pixelWidth, int pixelHeight)
        {
            if (pixelWidth <= 0)
            {
                throw new System.ArgumentOutOfRangeException("pixelWidth", SR.Get(SRID.ParameterMustBeGreaterThanZero));
            }

            if (pixelHeight <= 0)
            {
                throw new System.ArgumentOutOfRangeException("pixelHeight", SR.Get(SRID.ParameterMustBeGreaterThanZero));
            }

            BitmapSizeOptions sizeOptions = new BitmapSizeOptions();

            sizeOptions._rotationAngle          = Rotation.Rotate0;
            sizeOptions._preservesAspectRatio = false;
            sizeOptions._pixelWidth          = pixelWidth;
            sizeOptions._pixelHeight         = pixelHeight;

            return sizeOptions;
        }
        /// <summary>
        /// Constructs an identity BitmapSizeOptions (when passed to a TransformedBitmap, the
        /// input is the same as the output).
        /// </summary>
        public static BitmapSizeOptions FromEmptyOptions()
        {
            BitmapSizeOptions sizeOptions = new BitmapSizeOptions();

            sizeOptions._rotationAngle          = Rotation.Rotate0;
            sizeOptions._preservesAspectRatio = true;
            sizeOptions._pixelHeight         = 0;
            sizeOptions._pixelWidth          = 0;

            return sizeOptions;
        }
Beispiel #8
0
        internal InteropBitmap(IntPtr hicon, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)
            : base(true) // Use virtuals
        {
            SecurityHelper.DemandUnmanagedCode();

            _bitmapInit.BeginInit();

            using (FactoryMaker myFactory = new FactoryMaker())
            {
                HRESULT.Check(UnsafeNativeMethods.WICImagingFactory.CreateBitmapFromHICON(
                    myFactory.ImagingFactoryPtr,
                    hicon,
                    out _unmanagedSource));
                Debug.Assert (_unmanagedSource != null && !_unmanagedSource.IsInvalid);
            }

            _unmanagedSource.CalculateSize();
            _sourceRect = sourceRect;
            _sizeOptions = sizeOptions;
            _syncObject = _unmanagedSource;

            _bitmapInit.EndInit();

            FinalizeCreation();
        }
Beispiel #9
0
        unsafe public static BitmapSource CreateBitmapSourceFromHIcon(
            IntPtr icon, 
            Int32Rect sourceRect,
            BitmapSizeOptions sizeOptions) 
        { 
            SecurityHelper.DemandUnmanagedCode();
 
            if (icon == IntPtr.Zero)
            {
                throw new ArgumentNullException("icon");
            } 

            return new InteropBitmap(icon, sourceRect, sizeOptions); 
        }