/// <summary> /// Gets a stream containing the image binary in the specified property (or the default Binary field). /// Image dimensions can be determined by providing the width and height values among the parameters. /// The image is also redacted if the user does not have enough permissions to see the full image /// in case of preview images, only restricted ones. /// </summary> /// <param name="propertyName">Name of the binary property to serve, default is Binary.</param> /// <param name="parameters">Optional parameters that may include image width and height.</param> /// <param name="contentType">Out parameter, filled with the binary content type to be served to the client.</param> /// <param name="fileName">Out parameter, filled with the file name to serve to the client.</param> public Stream GetImageStream(string propertyName, IDictionary <string, object> parameters, out string contentType, out BinaryFileName fileName) { Stream imageStream; object widthParam = null; object heightParam = null; parameters?.TryGetValue("width", out widthParam); parameters?.TryGetValue("height", out heightParam); var binaryData = !string.IsNullOrEmpty(propertyName) ? GetBinary(propertyName) : Binary; contentType = binaryData?.ContentType ?? string.Empty; fileName = string.IsNullOrEmpty(propertyName) || string.CompareOrdinal(propertyName, "Binary") == 0 ? new BinaryFileName(Name) : binaryData?.FileName ?? string.Empty; if (DocumentPreviewProvider.Current != null && DocumentPreviewProvider.Current.IsPreviewOrThumbnailImage(NodeHead.Get(Id))) { // get preview image with watermark or redaction if necessary imageStream = DocumentPreviewProvider.Current.GetRestrictedImage(this, new PreviewImageOptions { BinaryFieldName = propertyName }); } else { imageStream = binaryData?.GetStream(); } if (imageStream == null) { return(new MemoryStream()); } imageStream.Position = 0; int ConvertImageParameter(object param) { if (param != null) { // we recognize int and string values as well switch (param) { case int i: return(i); case string s when int.TryParse(s, out var pint): return(pint); } } return(200); } // no resize parameters: return the original stream if (widthParam == null || heightParam == null) { return(imageStream); } var width = ConvertImageParameter(widthParam); var height = ConvertImageParameter(heightParam); // compute a new, resized stream on-the-fly var resizedStream = ImageResizer.CreateResizedImageFile(imageStream, width, height, 80, getImageFormat(contentType)); // in case the method created a new stream, we have to close the original to prevent memory leak if (!ReferenceEquals(resizedStream, imageStream)) { imageStream.Dispose(); } return(resizedStream); }