public virtual async Task <(int Height, int Width)> GetImageDimensions(IFormFile file) { // Based on https://stackoverflow.com/questions/50377114/how-to-get-the-image-width-height-when-doing-upload-in-asp-net-core if (file != null) { List <string> AcceptableImageExtentions = new List <string> { ".jpg", ".jpeg", ".png", ".bmp" }; string fileExtention = System.IO.Path.GetExtension(file.FileName); if (AcceptableImageExtentions.Contains(fileExtention)) { using (System.IO.Stream stream = new System.IO.MemoryStream()) { await file.CopyToAsync(stream); SixLabors.ImageSharp.Formats.IImageDecoder imageDecoder; if (fileExtention == ".jpeg" || fileExtention == ".jpg") { imageDecoder = new SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder(); } else if (fileExtention == ".png") { imageDecoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder(); } else { imageDecoder = new SixLabors.ImageSharp.Formats.Bmp.BmpDecoder(); } if (stream.Position == stream.Length) //Check this because if your image is a .png, it might just throw an error { stream.Position = stream.Seek(0, SeekOrigin.Begin); } SixLabors.ImageSharp.Image <SixLabors.ImageSharp.PixelFormats.Rgba32> imageSharp = imageDecoder.Decode <SixLabors.ImageSharp.PixelFormats.Rgba32>(SixLabors.ImageSharp.Configuration.Default, stream); if (imageSharp != null) { return(imageSharp.Height, imageSharp.Width); } } } } return(0, 0); }
private static void ReadPngFile(CommandLineOptions opts, ParseContext context, IcoFrame frame, byte[] sourceFile, out bool canSourceBePreserved) { using (var stream = new MemoryStream(sourceFile)) { var decoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder(); frame.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream); } var encoding = PngDecoder.GetPngFileEncoding(new Memory <byte>(sourceFile)); frame.Encoding.Type = IcoEncodingType.Png; frame.Encoding.ClaimedBitDepth = 32; frame.Encoding.ActualBitDepth = 32; canSourceBePreserved = encoding.ColorType == PngColorType.RGBA && encoding.BitsPerChannel == 8; }
public static void DoPngEntry(ByteReader bitmapHeader, ParseContext context, IcoFrame source) { if (source.Encoding.ClaimedBitDepth != 32) { context.Reporter.WarnLine(IcoErrorCode.PngNot32Bit, $"PNG-encoded image with bit depth {source.Encoding.ClaimedBitDepth} (expected 32).", context.DisplayedPath, context.ImageDirectoryIndex.Value); } using (var stream = new MemoryStream(bitmapHeader.Data.ToArray())) { var decoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder(); source.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream); } source.Encoding.Type = IcoEncodingType.Png; source.Encoding.PixelFormat = BmpUtil.IsAnyPartialTransparency(source.CookedData) ? BitmapEncoding.Pixel_argb32 : BitmapEncoding.Pixel_0rgb32; source.Mask = GenerateMaskFromAlpha(source.CookedData); // Conservatively assume that the output wouldn't have used palette trimming, if it had been a bmp frame. if (source.Encoding.ClaimedBitDepth < 16) { source.Encoding.PaletteSize = 1u << (int)source.Encoding.ClaimedBitDepth; } var encoding = GetPngFileEncoding(bitmapHeader.Data); if (encoding.ColorType != PngColorType.RGBA) { context.Reporter.WarnLine(IcoErrorCode.PngNotRGBA32, $"ICO files require the embedded PNG image to be encoded in RGBA32 format; this is {encoding.ColorType}", context.DisplayedPath, context.ImageDirectoryIndex.Value); } else if (encoding.BitsPerChannel != 8) { context.Reporter.WarnLine(IcoErrorCode.PngNotRGBA32, $"ICO files require the embedded PNG image to be encoded in RGBA32 format; this is RGBA{encoding.BitsPerChannel * 4}", context.DisplayedPath, context.ImageDirectoryIndex.Value); } uint numChannels = 0; switch (encoding.ColorType) { case PngColorType.Grayscale: numChannels = 1; break; case PngColorType.RGB: numChannels = 3; break; case PngColorType.GrayscaleAlpha: numChannels = 2; break; case PngColorType.RGBA: numChannels = 4; break; case PngColorType.Palette: default: break; } source.Encoding.ActualHeight = encoding.Height; source.Encoding.ActualWidth = encoding.Width; source.Encoding.ActualBitDepth = encoding.BitsPerChannel * numChannels; }