Esempio n. 1
0
        private bool CheckConvertion(MainForm form, IWICFormatConverterInfo info, Guid from, Guid to)
        {
            if (from == to)
            {
                return(true);
            }

            IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();
            IWICPalette        palette = factory.CreatePalette();

            palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false);
            IWICBitmap          bitmap    = null;
            IWICFormatConverter converter = null;

            try
            {
                try
                {
                    converter = info.CreateInstance();
                }
                catch (Exception e)
                {
                    form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e));

                    return(false);
                }

                try
                {
                    bitmap = factory.CreateBitmap(1, 1, from, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad);
                    bitmap.SetPalette(palette);
                }
                catch (Exception e)
                {
                    form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e), new DataEntry(Resources.PixelFormat, from));

                    return(false);
                }

                try
                {
                    converter.Initialize(bitmap, to, WICBitmapDitherType.WICBitmapDitherTypeNone, palette, 0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
                }
                catch (Exception e)
                {
                    form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT, e, new DataEntry(Resources.Source, from), new DataEntry(Resources.Destination, to));

                    return(false);
                }

                return(true);
            }
            finally
            {
                palette.ReleaseComObject();
                converter.ReleaseComObject();
                bitmap.ReleaseComObject();
                factory.ReleaseComObject();
            }
        }
Esempio n. 2
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();
            }
        }