public static void WriteImageThumbnailToFile(byte[] imageData, string path, int width = 128, int height = 96) { WrappedImage wi = null; try { if (imageData == null || imageData.Length == 0) { return; } FileInfo file = new FileInfo(path); Util.EnsureDirectoryExists(file.Directory.FullName); wi = new WrappedImage(imageData); File.WriteAllBytes(file.FullName, wi.ToByteArray(ImageFormat.Jpeg)); } catch (Exception ex) { Logger.Debug(ex); } finally { if (wi != null) { wi.Dispose(); } } }
/// <summary> /// Converts the image to the specified format. /// </summary> /// <param name="input">byte array containing the source image data</param> /// <param name="size">Percentage size. 100 does not affect image size and is the most efficient option. This parameter is overridden if maxWidth and maxHeight are both specified.</param> /// <param name="quality">Compression quality. Acceptable range depends on image format: /// JPG: 0 to 100 /// WebP: 0 to 100 or -1 for lossless encode /// PNG: No effect</param> /// <param name="format">Jpeg, PNG, or WebP</param> /// <param name="rotateFlipType">Image rotation and flipping options.</param> /// <param name="maxWidth">Maximum width of the output image. Original image aspect ratio will be preserved. Requires maxHeight to be specified also. Overrides the "size" parameter.</param> /// <param name="maxHeight">Maximum height of the output image. Original image aspect ratio will be preserved. Requires maxWidth to be specified also. Overrides the "size" parameter.</param> /// <param name="srcImageFormat">The image format of the source data. If this format matches the "format" parameter and no other operations are requested, the image may not be re-encoded at all.</param> /// <param name="special">A string indicating a special image transformation, if any.</param> /// <returns>A byte array containing image data in the specified format.</returns> /// <remarks>Calls EncodeBitmap(...) internally.</remarks> public static byte[] ConvertImage(byte[] input, int size = 100, int quality = 80, ImageFormat format = ImageFormat.Jpeg, RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone, int maxWidth = 0, int maxHeight = 0, ImageFormat srcImageFormat = ImageFormat.Jpeg, string special = "", string errorHelp = "") { if (srcImageFormat == ImageFormat.Webp) { return(input); // Cannot currently read WebP images } byte[] output = input; if (input.Length == 0) { return(input); } WrappedImage wi = null; try { wi = new WrappedImage(input); byte[] encoded = EncodeImage(wi, size, quality, format, rotateFlipType, maxWidth, maxHeight, special); if (encoded.Length > 0) { output = encoded; } } catch (Exception ex) { StringBuilder inputstart = new StringBuilder(); for (int i = 0; i < input.Length && i < 50; i++) { inputstart.Append(input[i]).Append(','); } StringBuilder inputend = new StringBuilder(); for (int i = input.Length - 50; i < input.Length && i > 0; i++) // If i <= 0, inputend would just be the same as inputstart anyway. This would happen if input.Length was <= 50. { inputend.Append(input[i]).Append(','); } Logger.Debug(ex, errorHelp + " - input length: " + input.Length + ", " + inputstart.ToString() + " " + inputend.ToString()); } finally { if (wi != null) { wi.Dispose(); } } return(output); }