Ejemplo n.º 1
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();
            }
        }
Ejemplo n.º 2
0
        public static IWICBitmapSource GetScaledImageSource(this IWICImagingFactory factory, IWICBitmapSource source, float scale)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int num;
            int num2;

            source.GetSize(out num, out num2);
            IWICBitmapScaler iwicbitmapScaler;

            factory.CreateBitmapScaler(out iwicbitmapScaler);
            iwicbitmapScaler.Initialize(source, (int)((float)num / scale), (int)((float)num2 / scale), WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);
            return(iwicbitmapScaler);
        }