Пример #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();
            }
        }
Пример #2
0
    private static IWICBitmapScaler Scale(IWICBitmapSource source, int width, int height, WicBitmapInterpolationMode mode)
    {
        var wfac = (IWICImagingFactory) new WICImagingFactory();
        IWICBitmapScaler scaler = null;

        try
        {
            scaler = wfac.CreateBitmapScaler();
            scaler.Initialize(source, width, height, mode);
            Marshal.ReleaseComObject(source);
            return(scaler);
        }
        finally
        {
            Release(wfac);
        }
    }
Пример #3
0
 public static void Initialize(this IWICBitmapScaler bitmapScaler, IWICBitmapSource pISource, Size size, WICBitmapInterpolationMode mode)
 {
     bitmapScaler.Initialize(pISource, size.Width, size.Height, mode);
 }