internal WebImage Save(HttpContextBase context, Action <string, byte[]> saveAction, string filePath, string imageFormat, bool forceWellKnownExtension) { filePath = filePath ?? FileName; if (String.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath", CommonResources.Argument_Cannot_Be_Null_Or_Empty); } // GetBytes takes care of executing pending transformations. // todo: this could be made more efficient by avoiding cloning array // when format is same byte[] content = GetBytes(imageFormat); if (forceWellKnownExtension) { ImageFormat saveImageFormat; ImageFormat requestedImageFormat = String.IsNullOrEmpty(imageFormat) ? _initialFormat : GetImageFormat(imageFormat); var extension = Path.GetExtension(filePath).TrimStart('.'); // TryFromStringToImageFormat accepts mime types and image names. For images supported by System.Drawing.Imaging, the image name maps to the extension. // Replace the extension with the current format in the following two events: // * The extension format cannot be converted to a known format // * The format does not match. if (!ConversionUtil.TryFromStringToImageFormat(extension, out saveImageFormat) || !saveImageFormat.Equals(requestedImageFormat)) { extension = requestedImageFormat.ToString().ToLowerInvariant(); filePath = filePath + "." + extension; } } saveAction(VirtualPathUtil.MapPath(context, filePath), content); // Update the FileName since it may have changed whilst saving. FileName = filePath; return(this); }
internal static WebImage GetImageFromRequest(HttpRequestBase request, string postedFileName = null) { Debug.Assert(request != null); if ((request.Files == null) || (request.Files.Count == 0)) { return(null); } HttpPostedFileBase file = String.IsNullOrEmpty(postedFileName) ? request.Files[0] : request.Files[postedFileName]; if (file == null || file.ContentLength < 1) { return(null); } // The content type is specified by the browser and is unreliable. // Disregard content type, acquire mime type. ImageFormat format; string mimeType = MimeMapping.GetMimeMapping(file.FileName); if (!ConversionUtil.TryFromStringToImageFormat(mimeType, out format)) { // Unsupported image format. return(null); } WebImage webImage = new WebImage(file.InputStream); webImage.FileName = file.FileName; return(webImage); }
private static ImageFormat GetImageFormat(string format) { Debug.Assert(!String.IsNullOrEmpty(format), "format cannot be null"); ImageFormat result; if (!ConversionUtil.TryFromStringToImageFormat(format, out result)) { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, HelpersResources.Image_IncorrectImageFormat, format), "format"); } return(result); }