public static CalorieImage ProcessImage(MemoryStream Input) { var NewImg = new CalorieImage(); ISupportedImageFormat format = new PngFormat { Quality = 99 }; //image using (MemoryStream maxStream = new MemoryStream()) { var RL = new ImageProcessor.Imaging.ResizeLayer(new Size(500, 500), ImageProcessor.Imaging.ResizeMode.Pad) {Upscale = false}; using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { imageFactory.Load(Input).Resize(RL) .Format(format) .BackgroundColor(Color.Transparent) .Save(maxStream); } NewImg.ImageData = maxStream.ToArray(); } //thumbnail using (MemoryStream thumbStream = new MemoryStream()) { var RL = new ImageProcessor.Imaging.ResizeLayer(new Size(100, 100), ImageProcessor.Imaging.ResizeMode.Pad) {Upscale = false}; using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { imageFactory.Load(Input).Resize(RL) .Format(format) .BackgroundColor(Color.Transparent) .Save(thumbStream); } NewImg.ThumbData= thumbStream.ToArray(); } return NewImg; }
public static ISupportedImageFormat DetectFormat(this MediaTypes mediaType, int quality = Quality) { ISupportedImageFormat result; switch (mediaType) { case MediaTypes.JPG: result = new JpegFormat(); break; case MediaTypes.PNG: result = new PngFormat(); break; default: throw new NotSupportedException($"Not Support MediaType: {mediaType}"); } result.Quality = quality; return result; }
private ISupportedImageFormat GetFormat(string extension, ImageInstruction ins) { ISupportedImageFormat format = null; if (extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) format = new JpegFormat { Quality = ins.JpegQuality }; else if (extension.Equals(".gif", StringComparison.OrdinalIgnoreCase)) format = new GifFormat { }; else if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase)) format = new PngFormat { }; return format; }
public void SavePicture(HttpPostedFileBase file, string name, Size size, string formatFile = "jpg") { if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { // Format is automatically detected though can be changed. ISupportedImageFormat format = new JpegFormat { Quality = 90 }; if (formatFile == "png") format = new PngFormat() { Quality = 90 }; //https://naimhamadi.wordpress.com/2014/06/25/processing-images-in-c-easily-using-imageprocessor/ // Initialize the ImageFactory using the overload to preserve EXIF metadata. using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { var path = Path.Combine(Server.MapPath("~/images/community"), string.Format("{0}.{1}", name, formatFile)); // Load, resize, set the format and quality and save an image. imageFactory.Load(file.InputStream) .Resize(size) .Format(format) .Save(path); } } }
private static void SaveImage(string sourceFile, string outputLocation, int newWidth, int newHeight = 0, ObservableCollection<string> output = null) { try { var imageBytes = File.ReadAllBytes(sourceFile); var flieExt = Path.GetExtension(sourceFile); var size = new System.Drawing.Size(newWidth, newHeight); string newFileName; if (newHeight == 0) newHeight = newWidth; newFileName = "icon-" + newWidth + "x" + newHeight; using (var inStream = new MemoryStream(imageBytes)) { var outputFileName = Path.Combine(outputLocation, newFileName + flieExt); Application.Current.Dispatcher.Invoke(() => output?.Insert(0, DateTime.Now + ": " + outputFileName)); using (var outStream = new FileStream(outputFileName, FileMode.OpenOrCreate)) { // Initialize the ImageFactory using the overload to preserve EXIF metadata. using (var imageFactory = new ImageFactory(preserveExifData: true)) { ISupportedImageFormat format = new PngFormat(); // Load, resize, set the format and quality and save an image. imageFactory.Load(inStream) .Resize(size) .Format(format) .Save(outStream); Application.Current.Dispatcher.Invoke(() => output?.Insert(0, DateTime.Now + ": " + outputFileName + " Saved")); } // Do something with the stream. } } } catch (Exception e) { Application.Current.Dispatcher.Invoke(() => MessageBox.Show(e.Message)); } }
public void GetPng() { var expected = new PngFormat(); var i = new Imaging(); foreach (var extension in expected.FileExtensions) { var format = i.Get(extension); Assert.AreEqual(expected.GetType(), format.GetType()); } }
private void CreateImage(string sourcePath, string targetPath, Size size, bool cut) { using (var fileStream = new FileStream(targetPath, FileMode.Create)) { byte[] photoBytes = File.ReadAllBytes(sourcePath); // change imagePath with a valid image path int quality = 70; var format = new PngFormat(); // we gonna convert a jpeg image to a png one using (var inStream = new MemoryStream(photoBytes)) { using (var outStream = new MemoryStream()) { // Initialize the ImageFactory using the overload to preserve EXIF metadata. using (var imageFactory = new ImageFactory(preserveExifData: true)) { // Do your magic here if (cut) { imageFactory.Load(inStream) .Resize(size) .Format(format) .Quality(quality) .Save(outStream); } else { imageFactory.Load(inStream) .Resize(size) .Format(format) .Quality(quality) .Save(outStream); } outStream.WriteTo(fileStream); } } } } }