Exemplo n.º 1
0
 public T LoadBitmapFromStream <T>(Stream stream, CreateBitmapDelegate <T> create)
 {
     using (GdkPixBufBitmapSource source = LoadSourceFromStream(stream))
     {
         return(create(source));
     }
 }
Exemplo n.º 2
0
        private T LoadBitmapFromStream <T>(Stream stream, Guid pixelFormat, CreateBitmapDelegate <T> createImage)
        {
            byte[] streamContent;
            using (var mem = new MemoryStream())
            {
                stream.CopyTo(mem);
                streamContent = mem.ToArray();
            }

            GCHandle pinnedContent = GCHandle.Alloc(streamContent, GCHandleType.Pinned);

            try
            {
                IntPtr pinnedContentPtr = pinnedContent.AddrOfPinnedObject();
                return(LoadBitmapFromMemory(pinnedContentPtr, (uint)streamContent.Length, pixelFormat, createImage));
            }
            finally
            {
                pinnedContent.Free();
            }
        }
Exemplo n.º 3
0
        private T LoadBitmapFromMemory <T>(IntPtr sourceBuffer, uint sourceBufferSize, Guid pixelFormat, CreateBitmapDelegate <T> createBitmap)
        {
            IWICImagingFactory    imagingFactory  = null;
            IWICStream            wicStream       = null;
            IWICBitmapDecoder     decoder         = null;
            IWICBitmapFrameDecode frame           = null;
            IWICFormatConverter   formatConverter = null;

            try
            {
                imagingFactory = new IWICImagingFactory();

                wicStream = imagingFactory.CreateStream();
                wicStream.InitializeFromMemory(sourceBuffer, sourceBufferSize);

                decoder = imagingFactory.CreateDecoderFromStream(
                    wicStream,
                    Guid.Empty,
                    WICDecodeOptions.WICDecodeMetadataCacheOnLoad
                    );

                frame = decoder.GetFrame(0);

                IWICBitmapSource source;

                if (frame.GetPixelFormat() == pixelFormat)
                {
                    source = frame;
                }
                else
                {
                    formatConverter = imagingFactory.CreateFormatConverter();
                    formatConverter.Initialize(
                        frame,
                        WICPixelFormat.GUID_WICPixelFormat32bppPBGRA,
                        WICBitmapDitherType.WICBitmapDitherTypeNone,
                        null,
                        0.0f,
                        WICBitmapPaletteType.WICBitmapPaletteTypeCustom
                        );
                    source = formatConverter;
                }

                source.GetSize(out uint width, out uint height);
                if (width * 4UL > int.MaxValue || height * 4UL > int.MaxValue || 4UL * width * height > int.MaxValue)
                {
                    throw new IOException($"Image is too large: {width}x{height}.");
                }

                WicBitmapSource bitmapSource = new WicBitmapSource(source, (int)width, (int)height);
                return(createBitmap(bitmapSource));
            }
            finally
            {
                SafeRelease(formatConverter);
                SafeRelease(frame);
                SafeRelease(decoder);
                SafeRelease(wicStream);
                SafeRelease(imagingFactory);
            }
        }
Exemplo n.º 4
0
 public T LoadBitmapFromStream <T>(Stream stream, CreateBitmapDelegate <T> createBitmap)
 {
     return(LoadBitmapFromStream(stream, WICPixelFormat.GUID_WICPixelFormat32bppBGRA, createBitmap));
 }