Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // This example requires the NuGet package 'stakx.WIC'.
            // https://www.nuget.org/packages/stakx.WIC/
            // https://github.com/stakx/WIC

            const string filename = @"<filename>";

            IWICImagingFactory    factory         = new WICImagingFactory();
            IWICBitmapDecoder     decoder         = null;
            IWICBitmapFrameDecode frame           = null;
            IWICFormatConverter   formatConverter = null;

            decoder = factory.CreateDecoderFromFilename(filename, IntPtr.Zero, StreamAccessMode.GENERIC_READ,
                                                        WICDecodeOptions.WICDecodeMetadataCacheOnDemand);

            int count = decoder.GetFrameCount();

            frame = decoder.GetFrame(0);

            int width  = 0;
            int height = 0;

            frame.GetSize(out width, out height);

            Guid pixelFormat = frame.GetPixelFormat();

            // 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,
                                       GUID_WICPixelFormat24bppRGB,
                                       WICBitmapDitherType.WICBitmapDitherTypeNone,
                                       null,
                                       0.0,
                                       WICBitmapPaletteType.WICBitmapPaletteTypeCustom);

            int bytesPerPixel = 3; // Because we have converted the frame to 24-bit RGB
            int stride        = width * bytesPerPixel;

            byte[] bytes = new byte[stride * height];

            formatConverter.CopyPixels(IntPtr.Zero, stride, stride * height, bytes);
        }
Ejemplo n.º 3
0
        private void DisplayImageInternal()
        {
            IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();

            IWICBitmapScaler    scaler        = factory.CreateBitmapScaler();
            IWICFormatConverter formatConvert = factory.CreateFormatConverter();
            GCHandle            h             = new GCHandle();
            Image image = rawPictureBox.Image;

            try
            {
                uint pixelColorWidth = 3; // 3 bytes/channel for Consts.GUID_WICPixelFormat24bppRGB, or more generally (((bits / pixel) + 7) / 8)

                uint width  = (uint)rawPictureBox.Width;
                uint height = (uint)rawPictureBox.Height;

                scaler.Initialize(frame, width, height, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);

                formatConvert.Initialize(scaler, Consts.GUID_WICPixelFormat24bppBGR, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut);

                uint stride = width * pixelColorWidth;
                uint size   = stride * height;

                byte[] pixels = new byte[size];

                formatConvert.CopyPixels(null, stride, size, pixels);

                h = GCHandle.Alloc(pixels, GCHandleType.Pinned);

                Bitmap bitmap = new Bitmap((int)width, (int)height, (int)stride, PixelFormat.Format24bppRgb, h.AddrOfPinnedObject());

                rawPictureBox.Image = bitmap;
            }
            catch
            {
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
                if (h.IsAllocated)
                {
                    h.Free();
                }
                scaler.ReleaseComObject();
                formatConvert.ReleaseComObject();
                factory.ReleaseComObject();
            }
        }