private void compressFile(string fname) { byte[] photoBytes = File.ReadAllBytes(fname); int quality = 70; ImageProcessor.Imaging.Formats.ISupportedImageFormat format = new ImageProcessor.Imaging.Formats.JpegFormat(); Size size = new Size(800, 600); using (MemoryStream inStream = new MemoryStream(photoBytes)) { using (MemoryStream outStream = new MemoryStream()) { using (ImageFactory imageFactory = new ImageFactory()) { // Load, resize, set the format and quality and save an image. imageFactory.Load(inStream) //.Resize(size) .Format(format) .Quality(quality) .Resolution(100, 100) .Save(outStream); FileStream file = new FileStream(fname, FileMode.Create, FileAccess.Write); outStream.WriteTo(file); file.Close(); outStream.Close(); } // Do something with the stream. } } }
public bool CreateThumbnail2(string thumbnailFolder, string filename) { if (!Directory.Exists(thumbnailFolder)) { return(false); } if (!File.Exists(filename)) { return(false); } FileInfo fInfo = new FileInfo(filename); string namePart = fInfo.Name.Replace(fInfo.Extension, ""); try { ImageProcessor.ImageFactory mFact = new ImageProcessor.ImageFactory(); ImageProcessor.Imaging.Formats.JpegFormat format = new ImageProcessor.Imaging.Formats.JpegFormat(); format.Quality = 100; mFact.Load(filename).Resize(new Size(300, 300)).Format(format).BackgroundColor(Color.White).Save(thumbnailFolder + @"\" + namePart + ".jpg"); return(true); } catch { return(false); } }
public static string StoreImage(HttpPostedFileBase image) { // Read a file and resize it. //byte[] photoBytes = File.ReadAllBytes(file); int quality = 70; var format = new ImageProcessor.Imaging.Formats.JpegFormat(); Size size = new Size(300, 0); using (var imageStream = new MemoryStream()) { image.InputStream.CopyTo(imageStream); imageStream.Position = 0; using (var resizeImageStream = new MemoryStream()) { using (ImageFactory imageFactory = new ImageFactory()) { // Load, resize, set the format and quality and save an image. imageFactory.Load(imageStream) .Resize(size) .Format(format) .Quality(quality) .Save(resizeImageStream); } using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USWest2)) { var newFileName = getUniqueName(image.FileName); var request = new PutObjectRequest(); request.BucketName = _bucketName; request.CannedACL = S3CannedACL.PublicRead; // Save Resize version to folder "thumb" request.Key = $"thumb/{newFileName}"; request.InputStream = resizeImageStream; var resp = client.PutObject(request); // Save Original version to root folder request.Key = newFileName; request.InputStream = imageStream; client.PutObject(request); if (resp.HttpStatusCode == System.Net.HttpStatusCode.OK) { return(getFullName(newFileName)); } return(null); } } } }
/// <summary>Crops original image from the stream to the specific width and heigth.</summary> /// <param name="imageStream">Original image stream.</param> /// <param name="width">Width.</param> /// <param name="height">Height.</param> /// <returns>Cropped image bytes.</returns> public byte[] CropImage(Stream imageStream, int width, int height) { byte[] imageBytes; using (var outStream = new MemoryStream()) { using (var imageFactory = new ImageFactory()) { var format = new ImageProcessor.Imaging.Formats.JpegFormat(); var size = new Size(width, height); // sets Crop, but u can use Stretch for example imageFactory.Load(imageStream).Resize(new ResizeLayer(size, ResizeMode.Crop)).Format(format).Save(outStream); } imageBytes = outStream.ToArray(); } return(imageBytes); }
//converting graphic files, the names of which are specified in the FileNames array into the ResultDirectory directory. public static bool ConvertPhotos(ItemCollection FileNames, string ResultDirectory, IProgress <int> progress) { System.Drawing.Size MainPic, StandartPic, SmallPic; ImageProcessor.Imaging.Formats.ISupportedImageFormat newformat; StandartPic = new System.Drawing.Size(PicsConfig.StandartPicWidth, PicsConfig.StandartPicHeight); MainPic = new System.Drawing.Size(PicsConfig.MainPicWidth, PicsConfig.MainPicHeight); SmallPic = new System.Drawing.Size(PicsConfig.SmallPicWidth, PicsConfig.SmallPicHeight); newformat = new ImageProcessor.Imaging.Formats.JpegFormat(); using (ImageFactory Image_Factory = new ImageFactory(preserveExifData: true)) { for (int Index = 0; Index < FileNames.Count; ++Index) { Image_Factory.Load(FileNames[Index].ToString()); Image_Factory.Resize(StandartPic); Image_Factory.BackgroundColor(System.Drawing.Color.White); Image_Factory.Format(newformat); Image_Factory.Quality(100); Image_Factory.Save(ResultDirectory + "\\" + (Index + 1).ToString("D4") + ".jpg"); if (Index == 0) { Image_Factory.Resize(MainPic); Image_Factory.BackgroundColor(System.Drawing.Color.White); Image_Factory.Save(ResultDirectory + "\\" + (Index + 1).ToString("D4") + "_f.jpg"); } Image_Factory.Resize(SmallPic); Image_Factory.BackgroundColor(System.Drawing.Color.White); Image_Factory.Save(ResultDirectory + "\\" + (Index + 1).ToString("D4") + "_sm.jpg"); progress.Report(Index + 1); } } MessageBox.Show("Image conversion is completed", "Information!", MessageBoxButton.OK, MessageBoxImage.Information); return(true); }
private void ResizeRotateImg(string fname) { // determine image format ImageFormat img_format = (fname.ToLower().Contains(".jpg") || fname.ToLower().Contains(".jpeg")) ? ImageFormat.Jpeg : ImageFormat.Png; // rotate the image if needed, and save it back out to the file using (System.Drawing.Image myImage = System.Drawing.Image.FromFile(fname)) { if (myImage.PropertyIdList.Contains(0x0112)) { int rotationValue = myImage.GetPropertyItem(0x0112).Value[0]; switch (rotationValue) { case 1: // landscape, do nothing break; case 8: // rotated 90 right, so de-rotate myImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone); myImage.Save(fname, img_format); break; case 3: // bottoms up myImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone); myImage.Save(fname, img_format); break; case 6: // rotated 90 left myImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone); myImage.Save(fname, img_format); break; } } } // read the image into a buffer byte[] photoBytes = System.IO.File.ReadAllBytes(fname); int quality = 80; ImageProcessor.Imaging.Formats.ISupportedImageFormat format; if (img_format == ImageFormat.Jpeg) { format = new ImageProcessor.Imaging.Formats.JpegFormat { Quality = 70 }; } else { format = new ImageProcessor.Imaging.Formats.PngFormat { }; } Size size = new Size(300, 0); using (MemoryStream inStream = new MemoryStream(photoBytes)) { using (MemoryStream outStream = new MemoryStream()) { using (ImageFactory imageFactory = new ImageFactory()) { // Load, resize, set the format and quality and save an image. imageFactory.Load(inStream).Resize(size).Format(format).Quality(quality).Save(outStream); System.IO.File.Delete(fname); using (FileStream file = new FileStream(fname, FileMode.Create, System.IO.FileAccess.Write)) { byte[] bytes = new byte[outStream.Length]; outStream.Read(bytes, 0, (int)outStream.Length); file.Write(bytes, 0, bytes.Length); outStream.Close(); } } } } }