Esempio n. 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);
        }
Esempio n. 2
0
        public virtual int Initialize(
            IStream pIStream,
            WICBitmapEncoderCacheOption cacheOption
            )
        {
            var fp = GetFunctionPointer(3);

            if (m_InitializeFunc == null)
            {
                m_InitializeFunc = (InitializeFunc)Marshal.GetDelegateForFunctionPointer(fp, typeof(InitializeFunc));
            }

            return(m_InitializeFunc(m_ptr, pIStream != null ? pIStream.Ptr : IntPtr.Zero, cacheOption));
        }
Esempio n. 3
0
 public int Initialize(IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
 {
     return(((delegate * unmanaged <IWICBitmapEncoder *, IStream *, WICBitmapEncoderCacheOption, int>)(lpVtbl[3]))((IWICBitmapEncoder *)Unsafe.AsPointer(ref this), pIStream, cacheOption));
 }
Esempio n. 4
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();
            }
        }
Esempio n. 5
0
    private static void Save(IWICBitmapSource source, Stream stream, Guid containerFormat, Guid pixelFormat, WICBitmapEncoderCacheOption cacheOptions, WICRect rect)
    {
        var wfac = (IWICImagingFactory) new WICImagingFactory();
        IWICBitmapEncoder     encoder = null;
        IWICBitmapFrameEncode frame   = null;

        try
        {
            encoder = wfac.CreateEncoder(containerFormat, null);
            encoder.Initialize(new ManagedIStream(stream), cacheOptions);
            encoder.CreateNewFrame(out frame, IntPtr.Zero);
            frame.Initialize(IntPtr.Zero);
            if (pixelFormat != Guid.Empty)
            {
                frame.SetPixelFormat(pixelFormat);
            }
            frame.WriteSource(source, rect);
            frame.Commit();
            encoder.Commit();
        }
        finally
        {
            Release(frame);
            Release(encoder);
            Release(wfac);
        }
    }
 public int Initialize([NativeTypeName("IStream *")] IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
 {
     return(((delegate * unmanaged[Stdcall] < IWICBitmapEncoder *, IStream *, WICBitmapEncoderCacheOption, int >)(lpVtbl[3]))((IWICBitmapEncoder *)Unsafe.AsPointer(ref this), pIStream, cacheOption));
 }
Esempio n. 7
0
        public static void Initialize(this IWICBitmapEncoder encoder, IStream stream, WICBitmapEncoderCacheOption options = WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            encoder.Initialize(stream, options).ThrowOnError();
        }
Esempio n. 8
0
 public static void Initialize(this IComObject <IWICBitmapEncoder> encoder, IStream stream, WICBitmapEncoderCacheOption options = WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache) => Initialize(encoder?.Object, stream, options);
Esempio n. 9
0
 public static void Initialize(this IWICBitmapEncoder bitmapEncoder, Stream stream, WICBitmapEncoderCacheOption cacheOption)
 {
     bitmapEncoder.Initialize(stream.AsCOMStream(), cacheOption);
 }