private Bitmap resizeImageToBitmap(string trackingId, Image image, int width, int height) { var innerRect = ResizeMath.KeepRatio(image.Width, image.Height, width, height); _log.info(trackingId, () => $"resize {image.Width}x{image.Height} --> {width}x{height} = [{innerRect.Top} {innerRect.Left} {innerRect.Width}x{innerRect.Height}]"); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, innerRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } fillMargins(graphics, width, height, innerRect, Brushes.Black); } return(destImage); }
void IApi.Thumbnail(ISrvRequest request) { var config = _config; var http = request.Http.Request; var trackingId = request.TrackingId; var url = http.Require("url"); var width = http.OptionalInt("width", config.thumbnail.defaultWidth); var height = http.OptionalInt("height", config.thumbnail.defaultHeight); if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) { throw new ApplicationException($"Bad url '{url}'"); } _log.info(trackingId, () => $"thumbnail request [{width} x {height}] for '{url}'"); var key = $"{url}_{width}x{height}"; _thumbnail.Process(new ThumbnailRequest { Srv = request, Key = key, Url = url, Width = width, Height = height }); }
private Task <byte[]> downloadImage(ThumbnailRequest request, string url) { var trackingId = request.Srv.TrackingId; byte[] download() { try { _log.info(trackingId, () => $"Downloading '{url}'"); using (var web = new WebClient()) { return (web.DownloadData(url)); } } catch (Exception ex) { _log.error(trackingId, ex, $"Failed to download '{url}' ({ex.Message})"); return (_helpers.TextToImage(request.Width, request.Height, $"{ex.Message} (trackingId={trackingId})")); } } return (Task.Run(() => download())); }