public static byte[] LoadImage(string fileName, bool premultiplied, out int width, out int height) { WIC.ImagingFactory factory = new WIC.ImagingFactory(); SharpDX.IO.NativeFileStream stream = new SharpDX.IO.NativeFileStream(fileName, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read); WIC.BitmapDecoder decoder = new WIC.BitmapDecoder(factory, stream, WIC.DecodeOptions.CacheOnDemand); WIC.BitmapFrameDecode frame = decoder.GetFrame(0); width = frame.Size.Width; height = frame.Size.Height; byte[] data = new byte[width * height * 4]; frame.CopyPixels(data, width * 4); return(data); }
//------------------------------------------------------------------------------------- // Decodes a single frame //------------------------------------------------------------------------------------- private static Image DecodeSingleFrame(WICFlags flags, ImageDescription metadata, Guid convertGUID, WIC.BitmapFrameDecode frame) { var image = Image.New(metadata); var pixelBuffer = image.PixelBuffer[0]; if (convertGUID == Guid.Empty) { frame.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride); } else { using (var converter = new WIC.FormatConverter(Factory)) { converter.Initialize(frame, convertGUID, GetWICDither(flags), null, 0, WIC.BitmapPaletteType.Custom); converter.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride); } } return(image); }
//------------------------------------------------------------------------------------- // Decodes a single frame //------------------------------------------------------------------------------------- private static Image DecodeSingleFrame(WICFlags flags, ImageDescription metadata, Guid convertGUID, BitmapFrameDecode frame) { var image = Image.New(metadata); var pixelBuffer = image.PixelBuffer[0]; if (convertGUID == Guid.Empty) { frame.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride); } else { using (var converter = new FormatConverter(Factory)) { converter.Initialize(frame, convertGUID, GetWICDither(flags), null, 0, BitmapPaletteType.Custom); converter.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride); } } return image; }
private static Texture DecodeSingleframe(ImagingFactory imagingFactory, WicFlags flags, TextureDescription description, Guid convertGuid, BitmapFrameDecode frame) { var texture = new Texture(description); var image = texture.Images[0]; if (convertGuid == Guid.Empty) { frame.CopyPixels(image.Data, image.RowPitch); } else { using (var converter = new FormatConverter(imagingFactory)) { converter.Initialize(frame, convertGuid, GetWicDither(flags), null, 0, BitmapPaletteType.Custom); converter.CopyPixels(image.Data, image.RowPitch); } } return texture; }