예제 #1
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);
        }
예제 #2
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);
        }
 public static void CheckNativeResult(int hr)
 {
     if (GraphicsInteropNativeMethods.Failed(hr))
     {
         throw Marshal.GetExceptionForHR(hr);
     }
 }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
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);
        }
예제 #7
0
        public static bool TryGetMetadataProperty <T>(this IWICMetadataQueryReader queryReader, string propertyPath, out T value)
        {
            if (queryReader == null)
            {
                throw new ArgumentNullException("queryReader");
            }
            value = default(T);
            PROPVARIANT propvariant    = default(PROPVARIANT);
            int         metadataByName = queryReader.GetMetadataByName(propertyPath, out propvariant);

            if (-2003292352 != metadataByName)
            {
                GraphicsInteropNativeMethods.CheckNativeResult(metadataByName);
                value = (T)((object)propvariant.Value);
                propvariant.Dispose();
                return(true);
            }
            propvariant.Dispose();
            return(false);
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
 public static IWICImagingFactory CreateFactory()
 {
     return(GraphicsInteropNativeMethods.CreateComInstanceFromInterface <IWICImagingFactory>(WicGuids.CLSID_WICImagingFactory));
 }
 public static T CreateComInstanceFromInterface <T>()
 {
     return(GraphicsInteropNativeMethods.CreateComInstanceFromInterface <T>(GraphicsInteropNativeMethods.GetGuidForInterface <T>()));
 }