Ejemplo n.º 1
0
        public void GetAdjustedPalette(ColorPalette palette, ColorAdjustType type)
        {
            // does inplace adjustment
            IntPtr memory = palette.ConvertToMemory();

            try
            {
                Gdip.CheckStatus(Gdip.GdipGetImageAttributesAdjustedPalette(
                                     new HandleRef(this, nativeImageAttributes),
                                     memory,
                                     type));
                palette.ConvertFromMemory(memory);
            }
            finally
            {
                if (memory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(memory);
                }
            }
        }
Ejemplo n.º 2
0
        public Image GetThumbnailImage(int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData)
        {
            if ((thumbWidth <= 0) || (thumbHeight <= 0))
            {
                throw new OutOfMemoryException("Invalid thumbnail size");
            }

            Image ThumbNail = new Bitmap(thumbWidth, thumbHeight);

            using (Graphics g = Graphics.FromImage(ThumbNail))
            {
                int status = Gdip.GdipDrawImageRectRectI(g.NativeGraphics, nativeImage,
                                                         0, 0, thumbWidth, thumbHeight,
                                                         0, 0, this.Width, this.Height,
                                                         GraphicsUnit.Pixel, IntPtr.Zero, null, IntPtr.Zero);

                Gdip.CheckStatus(status);
            }

            return(ThumbNail);
        }
Ejemplo n.º 3
0
        public void SetPixel(int x, int y, Color color)
        {
            if ((PixelFormat & PixelFormat.Indexed) != 0)
            {
                throw new InvalidOperationException(SR.Format(SR.GdiplusCannotSetPixelFromIndexedPixelFormat));
            }

            if (x < 0 || x >= Width)
            {
                throw new ArgumentOutOfRangeException(nameof(x), SR.Format(SR.ValidRangeX));
            }

            if (y < 0 || y >= Height)
            {
                throw new ArgumentOutOfRangeException(nameof(y), SR.Format(SR.ValidRangeY));
            }

            int status = Gdip.GdipBitmapSetPixel(new HandleRef(this, nativeImage), x, y, color.ToArgb());

            Gdip.CheckStatus(status);
        }
Ejemplo n.º 4
0
        public TextureBrush(Image image, Rectangle dstRect, ImageAttributes imageAttr)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            IntPtr brush  = IntPtr.Zero;
            int    status = Gdip.GdipCreateTextureIAI(new HandleRef(image, image.nativeImage),
                                                      new HandleRef(imageAttr, (imageAttr == null) ?
                                                                    IntPtr.Zero : imageAttr.nativeImageAttributes),
                                                      dstRect.X,
                                                      dstRect.Y,
                                                      dstRect.Width,
                                                      dstRect.Height,
                                                      out brush);

            Gdip.CheckStatus(status);

            SetNativeBrushInternal(brush);
        }
Ejemplo n.º 5
0
        public TextureBrush(Image image, WrapMode wrapMode)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp)
            {
                throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked ((int)wrapMode), typeof(WrapMode));
            }

            IntPtr brush  = IntPtr.Zero;
            int    status = Gdip.GdipCreateTexture(new HandleRef(image, image.nativeImage),
                                                   (int)wrapMode,
                                                   out brush);

            Gdip.CheckStatus(status);

            SetNativeBrushInternal(brush);
        }
Ejemplo n.º 6
0
        internal ColorPalette retrieveGDIPalette()
        {
            int bytes;
            ColorPalette ret = new ColorPalette();

            int st = Gdip.GdipGetImagePaletteSize(nativeImage, out bytes);
            Gdip.CheckStatus(st);
            IntPtr palette_data = Marshal.AllocHGlobal(bytes);
            try
            {
                st = Gdip.GdipGetImagePalette(nativeImage, palette_data, bytes);
                Gdip.CheckStatus(st);
                ret.ConvertFromMemory(palette_data);
                return ret;
            }

            finally
            {
                Marshal.FreeHGlobal(palette_data);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the specified property item from this <see cref='Image'/>.
        /// </summary>
        public PropertyItem?GetPropertyItem(int propid)
        {
            Gdip.CheckStatus(Gdip.GdipGetPropertyItemSize(new HandleRef(this, nativeImage), propid, out int size));

            if (size == 0)
            {
                return(null);
            }

            IntPtr propdata = Marshal.AllocHGlobal(size);

            try
            {
                Gdip.CheckStatus(Gdip.GdipGetPropertyItem(new HandleRef(this, nativeImage), propid, size, propdata));
                return(PropertyItemInternal.ConvertFromMemory(propdata, 1)[0]);
            }
            finally
            {
                Marshal.FreeHGlobal(propdata);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns the <see cref='MetafileHeader'/> associated with the specified <see cref='Metafile'/>.
        /// </summary>
        public static MetafileHeader GetMetafileHeader(Stream stream)
        {
            MetafileHeader header;

            IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf)));

            try
            {
                using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream));
                Gdip.CheckStatus(Gdip.GdipGetMetafileHeaderFromStream(streamWrapper.Ptr, memory));

                int[] type = new int[] { 0 };

                Marshal.Copy(memory, type, 0, 1);

                MetafileType metafileType = (MetafileType)type[0];

                header = new MetafileHeader();

                if (metafileType == MetafileType.Wmf ||
                    metafileType == MetafileType.WmfPlaceable)
                {
                    // WMF header
                    header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf)) !;
                    header.emf = null;
                }
                else
                {
                    // EMF header
                    header.wmf = null;
                    header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf)) !;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            return(header);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns the <see cref='MetafileHeader'/> associated with the specified <see cref='Metafile'/>.
        /// </summary>
        public static MetafileHeader GetMetafileHeader(string fileName)
        {
            // Called in order to emulate exception behavior from netfx related to invalid file paths.
            Path.GetFullPath(fileName);

            MetafileHeader header = new MetafileHeader();

            IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf)));

            try
            {
                Gdip.CheckStatus(Gdip.GdipGetMetafileHeaderFromFile(fileName, memory));

                int[] type = new int[] { 0 };

                Marshal.Copy(memory, type, 0, 1);

                MetafileType metafileType = (MetafileType)type[0];

                if (metafileType == MetafileType.Wmf ||
                    metafileType == MetafileType.WmfPlaceable)
                {
                    // WMF header
                    header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf));
                    header.emf = null;
                }
                else
                {
                    // EMF header
                    header.wmf = null;
                    header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            return(header);
        }
Ejemplo n.º 10
0
        internal static IntPtr InitFromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // Unix, with libgdiplus
            // We use a custom API for this, because there's no easy way
            // to get the Stream down to libgdiplus.  So, we wrap the stream
            // with a set of delegates.
            GdiPlusStreamHelper sh = new GdiPlusStreamHelper(stream, true);

            int st = Gdip.GdipLoadImageFromDelegate_linux(sh.GetHeaderDelegate, sh.GetBytesDelegate,
                                                          sh.PutBytesDelegate, sh.SeekDelegate, sh.CloseDelegate, sh.SizeDelegate, out IntPtr imagePtr);

            // Since we're just passing to native code the delegates inside the wrapper, we need to keep sh alive
            // to avoid the object being collected and therefore the delegates would be collected as well.
            GC.KeepAlive(sh);
            Gdip.CheckStatus(st);
            return(imagePtr);
        }
Ejemplo n.º 11
0
        // public methods
        // static

        public static Image FromFile(string filename, bool useEmbeddedColorManagement)
        {
            IntPtr imagePtr;
            int    st;

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException(filename);
            }

            if (useEmbeddedColorManagement)
            {
                st = Gdip.GdipLoadImageFromFileICM(filename, out imagePtr);
            }
            else
            {
                st = Gdip.GdipLoadImageFromFile(filename, out imagePtr);
            }
            Gdip.CheckStatus(st);

            return(CreateFromHandle(imagePtr));
        }
Ejemplo n.º 12
0
        public unsafe Bitmap(Stream stream, bool useIcm)
        {
            ArgumentNullException.ThrowIfNull(stream);

            using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream));

            IntPtr bitmap = IntPtr.Zero;

            if (useIcm)
            {
                Gdip.CheckStatus(Gdip.GdipCreateBitmapFromStreamICM(streamWrapper.Ptr, &bitmap));
            }
            else
            {
                Gdip.CheckStatus(Gdip.GdipCreateBitmapFromStream(streamWrapper.Ptr, &bitmap));
            }

            ValidateImage(bitmap);

            SetNativeImage(bitmap);
            EnsureSave(this, null, stream);
        }
Ejemplo n.º 13
0
        public TextureBrush(Image image, WrapMode wrapMode, Rectangle dstRect)
        {
            ArgumentNullException.ThrowIfNull(image);

            if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp)
            {
                throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked ((int)wrapMode), typeof(WrapMode));
            }

            IntPtr brush;
            int    status = Gdip.GdipCreateTexture2I(new HandleRef(image, image.nativeImage),
                                                     unchecked ((int)wrapMode),
                                                     dstRect.X,
                                                     dstRect.Y,
                                                     dstRect.Width,
                                                     dstRect.Height,
                                                     out brush);

            Gdip.CheckStatus(status);

            SetNativeBrushInternal(brush);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Returns the <see cref='MetafileHeader'/> associated with this <see cref='Metafile'/>.
        /// </summary>
        public MetafileHeader GetMetafileHeader()
        {
            MetafileHeader header;

            IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf)));

            try
            {
                Gdip.CheckStatus(Gdip.GdipGetMetafileHeaderFromMetafile(new HandleRef(this, nativeImage), memory));

                int[] type = new int[] { 0 };

                Marshal.Copy(memory, type, 0, 1);

                MetafileType metafileType = (MetafileType)type[0];

                header = new MetafileHeader();

                if (metafileType == MetafileType.Wmf ||
                    metafileType == MetafileType.WmfPlaceable)
                {
                    // WMF header
                    header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf));
                    header.emf = null;
                }
                else
                {
                    // EMF header
                    header.wmf = null;
                    header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            return(header);
        }
Ejemplo n.º 15
0
        internal void storeGDIPalette(ColorPalette palette)
        {
            if (palette == null)
            {
                throw new ArgumentNullException(nameof(palette));
            }
            IntPtr palette_data = palette.ConvertToMemory();
            if (palette_data == IntPtr.Zero)
            {
                return;
            }

            try
            {
                int st = Gdip.GdipSetImagePalette(nativeImage, palette_data);
                Gdip.CheckStatus(st);
            }

            finally
            {
                Marshal.FreeHGlobal(palette_data);
            }
        }
Ejemplo n.º 16
0
        public void SetBlendTriangularShape(float focus, float scale)
        {
            if (focus < 0 || focus > 1)
            {
                throw new ArgumentException(SR.Format(SR.GdiplusInvalidParameter), nameof(focus));
            }

            if (scale < 0 || scale > 1)
            {
                throw new ArgumentException(SR.Format(SR.GdiplusInvalidParameter), nameof(scale));
            }

            int status = Gdip.GdipSetLineLinearBlend(new HandleRef(this, NativeBrush), focus, scale);

            Gdip.CheckStatus(status);

            // Setting a triangular shape overrides the explicitly set interpolation colors. libgdiplus correctly clears
            // the interpolation colors (https://github.com/mono/libgdiplus/blob/master/src/lineargradientbrush.c#L959) but
            // returns WrongState instead of ArgumentException (https://github.com/mono/libgdiplus/blob/master/src/lineargradientbrush.c#L814)
            // when calling GdipGetLinePresetBlend, so it is important we set this to false. This way, we are sure get_InterpolationColors
            // will return an ArgumentException.
            _interpolationColorsWasSet = false;
        }
Ejemplo n.º 17
0
        public void SaveAdd(EncoderParameters encoderParams)
        {
            IntPtr encoder = IntPtr.Zero;

            if (encoderParams != null)
            {
                encoder = encoderParams.ConvertToMemory();
            }

            _rawData = null;

            try
            {
                Gdip.CheckStatus(Gdip.GdipSaveAdd(new HandleRef(this, nativeImage), new HandleRef(encoderParams, encoder)));
            }
            finally
            {
                if (encoder != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(encoder);
                }
            }
        }
Ejemplo n.º 18
0
        public Bitmap(string filename, bool useIcm)
        {
            // GDI+ will read this file multiple times. Get the fully qualified path
            // so if the app's default directory changes we won't get an error.
            filename = Path.GetFullPath(filename);

            IntPtr bitmap = IntPtr.Zero;
            int    status;

            if (useIcm)
            {
                status = Gdip.GdipCreateBitmapFromFileICM(filename, out bitmap);
            }
            else
            {
                status = Gdip.GdipCreateBitmapFromFile(filename, out bitmap);
            }
            Gdip.CheckStatus(status);

            ValidateImage(bitmap);

            SetNativeImage(bitmap);
            EnsureSave(this, filename, null);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref='FontFamily'/> class from the specified generic font family.
        /// </summary>
        public FontFamily(GenericFontFamilies genericFamily)
        {
            IntPtr nativeFamily = IntPtr.Zero;
            int    status;

            switch (genericFamily)
            {
            case GenericFontFamilies.Serif:
                status = Gdip.GdipGetGenericFontFamilySerif(out nativeFamily);
                break;

            case GenericFontFamilies.SansSerif:
                status = Gdip.GdipGetGenericFontFamilySansSerif(out nativeFamily);
                break;

            case GenericFontFamilies.Monospace:
            default:
                status = Gdip.GdipGetGenericFontFamilyMonospace(out nativeFamily);
                break;
            }
            Gdip.CheckStatus(status);

            SetNativeFamily(nativeFamily);
        }
Ejemplo n.º 20
0
        public Bitmap(Type type, string resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            Stream?stream = type.Module.Assembly.GetManifestResourceStream(type, resource);

            if (stream == null)
            {
                throw new ArgumentException(SR.Format(SR.ResourceNotFound, type, resource));
            }

            IntPtr bitmap = IntPtr.Zero;
            int    status = Gdip.GdipCreateBitmapFromStream(new GPStream(stream), out bitmap);

            Gdip.CheckStatus(status);

            ValidateImage(bitmap);

            SetNativeImage(bitmap);
            EnsureSave(this, null, stream);
        }
Ejemplo n.º 21
0
        public EncoderParameters GetEncoderParameterList(Guid encoder)
        {
            int  status;
            uint sz;

            status = Gdip.GdipGetEncoderParameterListSize(nativeImage, ref encoder, out sz);
            Gdip.CheckStatus(status);

            IntPtr            rawEPList = Marshal.AllocHGlobal((int)sz);
            EncoderParameters eps;

            try
            {
                status = Gdip.GdipGetEncoderParameterList(nativeImage, ref encoder, sz, rawEPList);
                eps    = EncoderParameters.ConvertFromMemory(rawEPList);
                Gdip.CheckStatus(status);
            }
            finally
            {
                Marshal.FreeHGlobal(rawEPList);
            }

            return(eps);
        }
Ejemplo n.º 22
0
 public void Translate(int dx, int dy)
 {
     Gdip.CheckStatus(Gdip.GdipTranslateRegionI(new HandleRef(this, NativeRegion), dx, dy));
 }
Ejemplo n.º 23
0
 public void ResetTransform()
 {
     Gdip.CheckStatus(Gdip.GdipResetPathGradientTransform(new HandleRef(this, NativeBrush)));
 }
Ejemplo n.º 24
0
 public void Exclude(Rectangle rect)
 {
     Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Exclude));
 }
Ejemplo n.º 25
0
 public void Complement(Rectangle rect)
 {
     Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Complement));
 }
Ejemplo n.º 26
0
 public void Xor(RectangleF rect)
 {
     Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Xor));
 }
Ejemplo n.º 27
0
 public void MakeEmpty()
 {
     Gdip.CheckStatus(Gdip.GdipSetEmpty(new HandleRef(this, NativeRegion)));
 }
Ejemplo n.º 28
0
 public void MakeInfinite()
 {
     Gdip.CheckStatus(Gdip.GdipSetInfinite(new HandleRef(this, NativeRegion)));
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Initializes a new instance of the <see cref='System.Drawing.Text.PrivateFontCollection'/> class.
        /// </summary>
        public PrivateFontCollection() : base()
        {
            int status = Gdip.GdipNewPrivateFontCollection(out _nativeFontCollection);

            Gdip.CheckStatus(status);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Adds a font contained in system memory to this <see cref='System.Drawing.Text.PrivateFontCollection'/>.
 /// </summary>
 public void AddMemoryFont(IntPtr memory, int length)
 {
     Gdip.CheckStatus(Gdip.GdipPrivateAddMemoryFont(new HandleRef(this, _nativeFontCollection), memory, length));
 }