public static IWICBitmapDecoder CreateDecoderFromFilename(this IWICImagingFactory imagingFactory, string wzFilename, Guid?pguidVendor = null, WICDecodeOptions metadataOptions = WICDecodeOptions.WICDecodeMetadataCacheOnDemand) { using (var pguidVendorPtr = CoTaskMemPtr.From(pguidVendor)) { return(imagingFactory.CreateDecoderFromFilename(wzFilename, pguidVendorPtr, StreamAccessMode.GENERIC_READ, metadataOptions)); } }
public static IComObject <IWICBitmapDecoder> CreateDecoderFromFilename(this IWICImagingFactory factory, string fileName, Guid?guidVendor = null, FileAccess desiredAccess = FileAccess.Read, WICDecodeOptions metadataOptions = WICDecodeOptions.WICDecodeMetadataCacheOnDemand) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (fileName == null) { throw new ArgumentNullException(nameof(factory)); } const uint GENERIC_READ = 0x80000000; const uint GENERIC_WRITE = 0x40000000; using (var guid = new ComMemory(guidVendor)) { uint acc = 0; if (desiredAccess.HasFlag(FileAccess.Read)) { acc |= GENERIC_READ; } if (desiredAccess.HasFlag(FileAccess.Read)) { acc |= GENERIC_WRITE; } factory.CreateDecoderFromFilename(fileName, guid.Pointer, acc, metadataOptions, out var value).ThrowOnError(); return(new ComObject <IWICBitmapDecoder>(value)); } }
static void Main(string[] args) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoder decoder = null; IWICBitmapFrameDecode frame = null; IWICFormatConverter formatConverter = null; try { const string filename = @"<filename>"; decoder = factory.CreateDecoderFromFilename(filename, null, NativeMethods.GenericAccessRights.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnDemand); uint count = decoder.GetFrameCount(); frame = decoder.GetFrame(0); uint width = 0; uint height = 0; frame.GetSize(out width, out height); Guid pixelFormat; frame.GetPixelFormat(out pixelFormat); // The frame can use many different pixel formats. // You can copy the raw pixel values by calling "frame.CopyPixels( )". // This method needs a buffer that can hold all bytes. // The total number of bytes is: width x height x bytes per pixel // The disadvantage of this solution is that you have to deal with all possible pixel formats. // You can make your life easy by converting the frame to a pixel format of // your choice. The code below shows how to convert the pixel format to 24-bit RGB. formatConverter = factory.CreateFormatConverter(); formatConverter.Initialize(frame, Consts.GUID_WICPixelFormat24bppRGB, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom); uint bytesPerPixel = 3; // Because we have converted the frame to 24-bit RGB uint stride = width * bytesPerPixel; byte[] bytes = new byte[stride * height]; formatConverter.CopyPixels(null, stride, stride * height, bytes); } finally { frame.ReleaseComObject(); decoder.ReleaseComObject(); factory.ReleaseComObject(); } }
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); }
private void filesListView_SelectedIndexChanged(object sender, EventArgs e) { if (filesListView.SelectedItems.Count > 0) { string file = filesListView.SelectedItems[0].Text; if (decoder != null) { decoder.ReleaseComObject(); decoder = null; } if (frame != null) { frame.ReleaseComObject(); frame = null; } IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); decoder = factory.CreateDecoderFromFilename(file, null, NativeMethods.GenericAccessRights.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnLoad); if (decoder != null) { uint frameCount = decoder.GetFrameCount(); if (frameCount > 0) { frame = decoder.GetFrame(0); } if (frame != null) { setupMode = true; SetupDevelopRaw(); setupMode = false; DisplayImage(); } } factory.ReleaseComObject(); } }