Exemplo n.º 1
0
        public static void FillBlobFromBitmapSource(this IWICImagingFactory factory, IWICBitmapSource source, IntPtr blob, int bufferStride, int bufferSize, Guid imageFormat)
        {
            IWICBitmapSource iwicbitmapSource = factory.ConvertFormat(source, imageFormat);

            iwicbitmapSource.FillBlobFromBitmapSource(blob, bufferStride, bufferSize);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapSource);
        }
Exemplo n.º 2
0
        public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, IWICStream stream, Guid encoderId)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            IWICBitmapEncoder iwicbitmapEncoder = factory.CreateEncoder(ref encoderId, null);

            iwicbitmapEncoder.Initialize(stream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
            IWICBitmapFrameEncode iwicbitmapFrameEncode;

            iwicbitmapEncoder.CreateNewFrame(out iwicbitmapFrameEncode, null);
            iwicbitmapFrameEncode.Initialize(null);
            iwicbitmapFrameEncode.WriteSource(imageSource, null);
            iwicbitmapFrameEncode.Commit();
            iwicbitmapEncoder.Commit();
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapEncoder);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapFrameEncode);
        }
Exemplo n.º 3
0
        public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, string fileName)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            string extension = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extension))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "File name '{0}' does not have an extension so encoder type can not be determined.", new object[]
                {
                    fileName
                }));
            }
            Guid encoderId;

            if (extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase))
            {
                encoderId = WicGuids.GUID_ContainerFormatJpeg;
            }
            else if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase))
            {
                encoderId = WicGuids.GUID_ContainerFormatPng;
            }
            else if (extension.Equals(".bmp", StringComparison.OrdinalIgnoreCase))
            {
                encoderId = WicGuids.GUID_ContainerFormatBmp;
            }
            else
            {
                if (!extension.Equals(".tiff", StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "File name '{0}' has extension {1} which we can't handle.", new object[]
                    {
                        fileName,
                        extension
                    }));
                }
                encoderId = WicGuids.GUID_ContainerFormatTiff;
            }
            IWICStream iwicstream;

            factory.CreateStream(out iwicstream);
            iwicstream.InitializeFromFilename(fileName, GenericAccess.GENERIC_WRITE);
            factory.Save(imageSource, iwicstream, encoderId);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicstream);
        }
Exemplo n.º 4
0
        public static IWICBitmapFrameDecode Load(this IWICImagingFactory factory, string fileName)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            IWICBitmapDecoder     iwicbitmapDecoder = factory.CreateDecoderFromFilename(fileName, null, GenericAccess.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnDemand);
            IWICBitmapFrameDecode result;

            iwicbitmapDecoder.GetFrame(0, out result);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapDecoder);
            return(result);
        }
Exemplo n.º 5
0
        public static IWICBitmapFrameDecode Load(this IWICImagingFactory factory, Stream stream)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            StreamWrapper         pIStream          = new StreamWrapper(stream);
            IWICBitmapDecoder     iwicbitmapDecoder = factory.CreateDecoderFromStream(pIStream, null, WICDecodeOptions.WICDecodeMetadataCacheOnDemand);
            IWICBitmapFrameDecode result;

            iwicbitmapDecoder.GetFrame(0, out result);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapDecoder);
            return(result);
        }
Exemplo n.º 6
0
        public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, Stream destinationStream, Guid encoderId)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }
            if (destinationStream == null)
            {
                throw new ArgumentNullException("destinationStream");
            }
            IWICStream iwicstream;

            factory.CreateStream(out iwicstream);
            iwicstream.InitializeFromIStream(new StreamWrapper(destinationStream));
            factory.Save(imageSource, iwicstream, encoderId);
            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicstream);
        }
Exemplo n.º 7
0
        public static IWICBitmapSource GetOrientedImageSource(this IWICImagingFactory factory, IWICBitmapSource source, WICBitmapTransformOptions opt)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (opt == WICBitmapTransformOptions.WICBitmapTransformRotate0)
            {
                return(source);
            }
            IWICBitmap iwicbitmap = null;

            factory.CreateBitmapFromSource(source, WICBitmapCreateCacheOption.WICBitmapCacheOnDemand, out iwicbitmap);
            IWICBitmapFlipRotator iwicbitmapFlipRotator = null;

            factory.CreateBitmapFlipRotator(out iwicbitmapFlipRotator);
            iwicbitmapFlipRotator.Initialize(iwicbitmap, opt);
            IWICBitmapSource result = iwicbitmapFlipRotator;

            GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmap);
            return(result);
        }