コード例 #1
0
        public void Save(string filePath,
                         Guid?encoderContainerFormat = null,
                         Guid?pixelFormat            = null,
                         WICBitmapEncoderCacheOption cacheOptions = WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache,
                         IEnumerable <KeyValuePair <string, object> > encoderOptions = null,
                         IEnumerable <WicMetadataKeyValue> metadata = null,
                         WicPalette encoderPalette = null,
                         WicPalette framePalette   = null,
                         WICRect?sourceRectangle   = null)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            Guid format;

            if (!encoderContainerFormat.HasValue)
            {
                var encoder = WicEncoder.FromFileExtension(Path.GetExtension(filePath));
                if (encoder == null)
                {
                    throw new WicNetException("WIC0003: Cannot determine encoder from file path.");
                }

                format = encoder.ContainerFormat;
            }
            else
            {
                format = encoderContainerFormat.Value;
            }

            using (var file = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                Save(file, format, pixelFormat, cacheOptions, encoderOptions, metadata, encoderPalette, framePalette, sourceRectangle);
        }
コード例 #2
0
        public void Save(
            Stream stream,
            Guid encoderContainerFormat,
            Guid?pixelFormat = null,
            WICBitmapEncoderCacheOption cacheOptions = WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache,
            IEnumerable <KeyValuePair <string, object> > encoderOptions = null,
            IEnumerable <WicMetadataKeyValue> metadata = null,
            WicPalette encoderPalette = null,
            WicPalette framePalette   = null,
            WICRect?sourceRectangle   = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var encoder = WICImagingFactory.CreateEncoder(encoderContainerFormat))
            {
                var mis = new ManagedIStream(stream);
                encoder.Initialize(mis, cacheOptions);

                if (encoderPalette != null)
                {
                    // gifs...
                    encoder.SetPalette(encoderPalette.ComObject);
                }

                var frameBag = encoder.CreateNewFrame();

                if (encoderOptions != null)
                {
                    frameBag.Item2.Write(encoderOptions);
                }

                frameBag.Initialize();

                if (metadata?.Any() == true)
                {
                    using (var writer = frameBag.GetMetadataQueryWriter())
                    {
                        writer.EncodeMetadata(metadata);
                    }
                }

                if (pixelFormat.HasValue)
                {
                    frameBag.SetPixelFormat(pixelFormat.Value);
                }

                if (framePalette != null)
                {
                    frameBag.Item1.SetPalette(framePalette.ComObject);
                }

                // "WIC error 0x88982F0C. The component is not initialized" here can mean the palette is not set
                // "WIC error 0x88982F45. The bitmap palette is unavailable" here means for example we're saving a file that doesn't support palette (even if we called SetPalette before, it may be useless)
                frameBag.WriteSource(_comObject, sourceRectangle);
                frameBag.Item1.Commit();
                encoder.Commit();
            }
        }
コード例 #3
0
        public void ConvertTo(Guid pixelFormat, WICBitmapDitherType ditherType = WICBitmapDitherType.WICBitmapDitherTypeNone, WicPalette palette = null, double alphaThresholdPercent = 0, WICBitmapPaletteType paletteTranslate = WICBitmapPaletteType.WICBitmapPaletteTypeCustom)
        {
            if (WicPixelFormat == null)
            {
                throw new InvalidOperationException();
            }

            var cvt = WicPixelFormat.GetPixelFormatConvertersTo(pixelFormat).FirstOrDefault();

            if (cvt == null)
            {
                throw new InvalidOperationException();
            }

            var converter = cvt.Convert(this, pixelFormat, ditherType, palette, alphaThresholdPercent, paletteTranslate);

            _comObject?.Dispose();
            _comObject = converter;
        }
コード例 #4
0
ファイル: WicPalette.cs プロジェクト: smourier/WicNet
 public WicPalette(WicPalette palette)
     : this((object)palette)
 {
 }