Example #1
0
        internal static Image CreateImageObject(IntPtr nativeImage)
        {
            Image image;

            int type = -1;

            int status = SafeNativeMethods.Gdip.GdipGetImageType(new HandleRef(null, nativeImage), out type);

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            switch ((ImageTypeEnum)type)
            {
            case ImageTypeEnum.Bitmap:
                image = Bitmap.FromGDIplus(nativeImage);
                break;

            case ImageTypeEnum.Metafile:
                image = Metafile.FromGDIplus(nativeImage);
                break;

            default:
                throw new ArgumentException(SR.Format(SR.InvalidImage));
            }

            return(image);
        }
Example #2
0
        public Bitmap Clone(RectangleF rect, PixelFormat format)
        {
            //validate the rect
            if (rect.Width == 0 || rect.Height == 0)
            {
                throw new ArgumentException(SR.GetString(SR.GdiplusInvalidRectangle, rect.ToString()));
            }

            IntPtr dstHandle = IntPtr.Zero;

            int status = SafeNativeMethods.Gdip.GdipCloneBitmapArea(
                rect.X,
                rect.Y,
                rect.Width,
                rect.Height,
                unchecked ((int)format),
                new HandleRef(this, nativeImage),
                out dstHandle);

            if (status != SafeNativeMethods.Gdip.Ok || dstHandle == IntPtr.Zero)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            return(Bitmap.FromGDIplus(dstHandle));
        }
Example #3
0
        /// <summary>
        /// Creates a <see cref='Bitmap'/> from the specified Windows handle with the specified color palette.
        /// </summary>
        public static Bitmap FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
        {
            IntPtr bitmap = IntPtr.Zero;
            int    status = SafeNativeMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, hpalette), out bitmap);

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            return(Bitmap.FromGDIplus(bitmap));
        }
        public static Bitmap FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
        {
            System.Drawing.IntSecurity.ObjectFromWin32Handle.Demand();
            IntPtr zero   = IntPtr.Zero;
            int    status = SafeNativeMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, hpalette), out zero);

            if (status != 0)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
            return(Bitmap.FromGDIplus(zero));
        }
Example #5
0
        /// <include file='doc\Bitmap.uex' path='docs/doc[@for="Bitmap.FromHicon"]/*' />
        /// <devdoc>
        ///    Creates a <see cref='System.Drawing.Bitmap'/> from a Windows handle to an
        ///    Icon.
        /// </devdoc>
        public static Bitmap FromHicon(IntPtr hicon)
        {
            IntPtr bitmap = IntPtr.Zero;

            int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromHICON(new HandleRef(null, hicon), out bitmap);

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            return(Bitmap.FromGDIplus(bitmap));
        }
Example #6
0
        /// <include file='doc\Bitmap.uex' path='docs/doc[@for="Bitmap.FromResource"]/*' />
        /// <devdoc>
        /// </devdoc>
        public static Bitmap FromResource(IntPtr hinstance, String bitmapName)
        {
            IntPtr bitmap;

            IntPtr name = Marshal.StringToHGlobalUni(bitmapName);

            int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromResource(new HandleRef(null, hinstance),
                                                                             new HandleRef(null, name),
                                                                             out bitmap);

            Marshal.FreeHGlobal(name);

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            return(Bitmap.FromGDIplus(bitmap));
        }
        internal static Image CreateImageObject(IntPtr nativeImage)
        {
            int type   = -1;
            int status = SafeNativeMethods.Gdip.GdipGetImageType(new HandleRef(null, nativeImage), out type);

            if (status != 0)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
            switch (((ImageTypeEnum)type))
            {
            case ImageTypeEnum.Bitmap:
                return(Bitmap.FromGDIplus(nativeImage));

            case ImageTypeEnum.Metafile:
                return(Metafile.FromGDIplus(nativeImage));
            }
            throw new ArgumentException(System.Drawing.SR.GetString("InvalidImage"));
        }
Example #8
0
        /// <include file='doc\Bitmap.uex' path='docs/doc[@for="Bitmap.Clone1"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Creates a copy of the section of this
        ///       Bitmap defined by <paramref term="rect"/> with a specified <see cref='System.Drawing.Imaging.PixelFormat'/>.
        ///    </para>
        /// </devdoc>
        // float version
        public Bitmap Clone(RectangleF rect, PixelFormat format)
        {
            IntPtr dstHandle = IntPtr.Zero;

            int status = SafeNativeMethods.GdipCloneBitmapArea(
                rect.X,
                rect.Y,
                rect.Width,
                rect.Height,
                (int)format,
                new HandleRef(this, nativeImage),
                out dstHandle);

            if (status != SafeNativeMethods.Ok || dstHandle == IntPtr.Zero)
            {
                throw SafeNativeMethods.StatusException(status);
            }

            return(Bitmap.FromGDIplus(dstHandle));
        }