Ejemplo n.º 1
0
 public static ImageFormat ToImageFormat(this KnownImageFormat knownImageFormat)
 {
     return(knownImageFormat switch
     {
         KnownImageFormat.Png => ImageFormat.Png,
         KnownImageFormat.Jpeg => ImageFormat.Jpeg,
         KnownImageFormat.Bmp => ImageFormat.Bmp,
         KnownImageFormat.Gif => ImageFormat.Gif,
         KnownImageFormat.Tiff => ImageFormat.Tiff,
         KnownImageFormat.Wmf => ImageFormat.Wmf,
         KnownImageFormat.Emf => ImageFormat.Emf,
         KnownImageFormat.Icon => ImageFormat.Icon,
         KnownImageFormat.Exif => ImageFormat.Exif,
         _ => ImageFormat.Png
     });
Ejemplo n.º 2
0
        public async Task <IActionResult> Index(
            int height              = 100,
            int width               = 100,
            KnownColor color        = KnownColor.LightGray,
            string text             = "thumbnail",
            KnownImageFormat format = KnownImageFormat.Png
            )
        {
            var request = Request.QueryString.ToUriComponent();

            var cacheKey = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(request)));

            var(data, contentType, name) =
                await _placeholderLogic.Resolve(height, width, color, text, format, cacheKey);

            return(File(data, contentType, name));
        }
Ejemplo n.º 3
0
        public async Task <(byte[] file, string contentType, string name)> Resolve(int height, int width, KnownColor color,
                                                                                   string text,
                                                                                   KnownImageFormat format, string cacheKey)
        {
            var contentType = format.ToContentType();
            var name        = $"thumbnail.{contentType.Split('/')[1]}";
            var fontSize    = Math.Max(height, width) / 10;

            var response = await _cache.GetAsync <byte[]>(cacheKey);

            if (response.HasValue)
            {
                return(response.Value, contentType, name);
            }

            var bitmap = new Bitmap(width, height);

            using var gfx       = Graphics.FromImage(bitmap);
            using var brush     = new SolidBrush(Color.FromKnownColor(color));
            using var arialFont = new Font("Arial", fontSize);

            gfx.FillRectangle(brush, 0, 0, width, height);

            var stringFormat = new StringFormat
            {
                LineAlignment = StringAlignment.Center,
                Alignment     = StringAlignment.Center
            };

            gfx.DrawString(text, arialFont, Brushes.Black, (float)(width / 2.0), (float)(height / 2.0), stringFormat);

            var stream = new MemoryStream();

            bitmap.Save(stream, format.ToImageFormat());

            var bytes = stream.ToArray();

            await _cache.SetAsync(cacheKey, bytes, TimeSpan.FromMinutes(5));

            return(bytes, contentType, name);
        }
Ejemplo n.º 4
0
        public void SaveImageToFile(string filePath, Bitmap image, KnownImageFormat format)
        {
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                ImageFormat finalFormat = ImageFormat.Png;
                switch (format)
                {
                case KnownImageFormat.png:
                {
                    finalFormat = ImageFormat.Png;
                    break;
                }

                case KnownImageFormat.bmp:
                {
                    finalFormat = ImageFormat.Bmp;
                    break;
                }

                case KnownImageFormat.gif:
                {
                    finalFormat = ImageFormat.Gif;
                    break;
                }

                case KnownImageFormat.jpeg:
                {
                    finalFormat = ImageFormat.Jpeg;
                    break;
                }

                default:
                {
                    finalFormat = ImageFormat.Png;
                    break;
                }
                }
                image.Save(fileStream, finalFormat);
            }
        }