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="img">Source image</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="special">A string indicating a special image transformation, if any.</param> /// <returns>A byte array containing image data in the specified format.</returns> /// <remarks>This is called internally by ConvertImage(...)</remarks> public static byte[] EncodeImage(WrappedImage img, int size = 100, int quality = 80, ImageFormat format = ImageFormat.Jpeg, RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone, int maxWidth = 0, int maxHeight = 0, string special = "") { byte[] output = new byte[0]; if (size < 1) { size = 1; } if (size > 100) { size = 100; } // Calculate new size int w = img.Width; int h = img.Height; if (size < 100) { w = Math.Max(1, (int)(img.Width * (size / 100.0))); h = Math.Max(1, (int)(img.Height * (size / 100.0))); } if (maxWidth > 0 && maxHeight > 0) { if (w > maxWidth) { double diff = w / maxWidth; w = maxWidth; h = (int)(h / diff); } if (h > maxHeight) { double diff = h / maxHeight; h = maxHeight; w = (int)(w / diff); } } if (w != img.Width || h != img.Height) { img.Resize(w, h); } if (rotateFlipType != RotateFlipType.RotateNoneFlipNone) { img.RotateFlip(rotateFlipType); } if (special == "red") { img.TurnRed(); } output = img.ToByteArray(format, quality); return(output); }