public void GetImage(string horizontalFlip = "", string verticalFlip = "", string rotateLeft = "", string rotateRight = "") { var imagePath = Server.MapPath("~/images/bunny-peanuts.jpg"); var image = new WebImage(imagePath); if (!string.IsNullOrWhiteSpace(verticalFlip)) { image = image.FlipVertical(); } if (!string.IsNullOrWhiteSpace(horizontalFlip)) { image = image.FlipHorizontal(); } if (!string.IsNullOrWhiteSpace(rotateLeft)) { image = image.RotateLeft(); } if (!string.IsNullOrWhiteSpace(rotateRight)) { image = image.RotateRight(); } image.Write(); }
public void RotateRightReturnsRotatedImage() { WebImage image = new WebImage(_PngImageBytes); image.RotateRight(); Assert.Equal(152, image.Width); Assert.Equal(160, image.Height); }
public void Process(string p) { WebImage image = new WebImage(this.ImagePath); switch (p) { case "Original": break; case "RotateLeft": image.RotateLeft(); break; case "RotateRight": image.RotateRight(); break; case "FlipHorizontal": image.FlipHorizontal(); break; case "FlipVertical": image.FlipVertical(); break; case "Resize": image.Resize(image.Width / 2, image.Height / 2, preserveAspectRatio: true); break; case "AddTextWatermark": image.AddTextWatermark("Elif Furuncu", fontColor: "Red", fontSize: 14, horizontalAlign: "Center", verticalAlign: "Bottom"); break; case "AddImageWatermark": WebImage watermark = new WebImage(this.ImagePath); watermark.Resize(50, 50); watermark = watermark.Save(Server.MapPath("~/images/watermark.jpeg"), imageFormat: "jpeg"); image.AddImageWatermark(watermark.FileName, 50, 50, verticalAlign: "Top", horizontalAlign: "Right", opacity: 75); break; case "Crop": image.Crop(0, 0, 100, 100); break; default: break; } string savePath = Server.MapPath("~/images/last.jpeg"); WebImage savedImage = image.Save(savePath, imageFormat: "jpeg"); image.Write(); }
// Code adapted from ReenignE, https://stackoverflow.com/questions/6222053/problem-reading-jpeg-metadata-orientation /// <summary> /// Fixes the orientation of an image /// </summary> /// <param name="imageStream">Image stream of image to fix</param> /// <returns>Fixed image stream</returns> public void FixOrientation(Stream imageStream) { Image img = Image.FromStream(imageStream); if (Array.IndexOf(img.PropertyIdList, 274) > -1) { var orientation = (int)img.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. return; case 2: _webImage = _webImage.FlipHorizontal(); //img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: _webImage = _webImage.RotateRight().RotateRight(); //img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: _webImage = _webImage.RotateRight().RotateRight().FlipHorizontal(); //img.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: _webImage = _webImage.RotateRight().FlipHorizontal(); //img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: _webImage = _webImage.RotateRight(); //img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: _webImage = _webImage.RotateLeft().FlipHorizontal(); //img.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: _webImage = _webImage.RotateLeft(); img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. //img.RemovePropertyItem(274); } }
public static WebImage RotateImage(WebImage image, string rotateVal) { if (rotateVal != "0") { if (rotateVal == "90") { image.RotateRight(); } else if (rotateVal == "180") { image.RotateRight(); image.RotateRight(); } else if (rotateVal == "270") { image.RotateRight(); image.RotateRight(); image.RotateRight(); } else if (rotateVal == "-90") { image.RotateLeft(); } else if (rotateVal == "-180") { image.RotateLeft(); image.RotateLeft(); } else if (rotateVal == "-270") { image.RotateLeft(); image.RotateLeft(); image.RotateLeft(); } } return(image); }