private static void ReadBmpFile(CommandLineOptions opts, ParseContext context, IcoFrame frame, byte[] sourceFile) { using (var stream = new MemoryStream(sourceFile)) { var decoder = new SixLabors.ImageSharp.Formats.Bmp.BmpDecoder(); frame.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream); } frame.Encoding.Type = IcoEncodingType.Bitmap; }
private static void WriteBmpFrame(CommandLineOptions opts, ParseContext context, IcoFrame frame) { var encoding = opts.BitmapEncodingOverride ?? BmpUtil.GetIdealBitmapEncoding(frame.CookedData, hasIcoMask: true); if (opts.MaskImagePath != null) { var maskSource = File.ReadAllBytes(opts.MaskImagePath); using (var stream = new MemoryStream(maskSource)) { var decoder = new SixLabors.ImageSharp.Formats.Bmp.BmpDecoder(); var mask = decoder.Decode <Rgba32>(new Configuration(), stream); if (mask.Width != frame.CookedData.Width || mask.Height != frame.CookedData.Height) { Reporter.ErrorLine( IcoErrorCode.BitmapMaskWrongDimensions, $"The mask's dimentions {mask.Height}x{mask.Width} don't match " + $"the frame dimensions {frame.CookedData.Height}x{frame.CookedData.Width}.", opts.MaskImagePath); throw new ArgumentException(); } for (var x = 0; x < mask.Width; x++) { for (var y = 0; y < mask.Height; y++) { if (mask[x, y].PackedValue != 0xffffffff && mask[x, y].PackedValue != 0x000000ff) { Reporter.ErrorLine( IcoErrorCode.BitampMaskWrongColors, $"The mask must be comprised entirely of black and white pixels (where black means transparent).", opts.MaskImagePath); throw new ArgumentException(); } } } frame.Mask = BmpUtil.CreateMaskFromImage(mask, blackIsTransparent: true); } } else { frame.Mask = BmpUtil.CreateMaskFromImage(frame.CookedData, blackIsTransparent: false); } frame.RawData = BmpEncoder.EncodeBitmap(context, encoding, BmpEncoder.Dialect.Ico, frame); if (frame.RawData == null) { Reporter.ErrorLine(context.LastEncodeError, $"Cannot encode the source image as a bitmap of type: {encoding}. Try reducing the number of colors, or changing the bitmap encoding.", opts.SourceImagePath); throw new ArgumentException(); } frame.Encoding.ClaimedBitDepth = (uint)BmpUtil.GetBitDepthForPixelFormat(encoding); frame.Encoding.ActualBitDepth = frame.Encoding.ClaimedBitDepth; }
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); }