public T LoadBitmapFromStream <T>(Stream stream, CreateBitmapDelegate <T> create) { using (GdkPixBufBitmapSource source = LoadSourceFromStream(stream)) { return(create(source)); } }
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(); } }
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); } }
public T LoadBitmapFromStream <T>(Stream stream, CreateBitmapDelegate <T> createBitmap) { return(LoadBitmapFromStream(stream, WICPixelFormat.GUID_WICPixelFormat32bppBGRA, createBitmap)); }