/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Image image = factory.Image; try { float angle = this.DynamicParameter; // Center of the image float rotateAtX = Math.Abs(image.Width / 2); float rotateAtY = Math.Abs(image.Height / 2); // Create a rotated image. image = this.RotateImage(image, rotateAtX, rotateAtY, angle); if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the width EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width); // Set the height EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height); } return image; } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <param name="image">The current image to process</param> /// <param name="newImage">The new Image to return</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image TransformImage(ImageFactory factory, Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(this.Matrix); Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); graphics.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } } // Add a vignette to finish the effect. factory.Update(newImage); Vignette vignette = new Vignette(); newImage = (Bitmap)vignette.ProcessImage(factory); // Reassign the image. image.Dispose(); image = newImage; return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { // TODO: Optimize this one day when I can break the API. newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); IMatrixFilter matrix = this.DynamicParameter; newImage = matrix.TransformImage(image, newImage); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory">The current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing /// the image to process.</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Bitmap image = (Bitmap)factory.Image; try { newImage = new Bitmap(image); GaussianLayer gaussianLayer = this.DynamicParameter; Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold }; double[,] kernel = convolution.CreateGuassianSharpenFilter(gaussianLayer.Size); newImage = convolution.ProcessKernel(newImage, kernel); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { RotateFlipType rotateFlipType = this.DynamicParameter; newImage = new Bitmap(image); // Flip newImage.RotateFlip(rotateFlipType); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { int percentage = this.DynamicParameter; newImage = new Bitmap(image); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); newImage = Adjustments.Alpha(newImage, percentage); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory">The current instance of the /// <see cref="T:ImageProcessor.ImageFactory" /> class containing /// the image to process.</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Image image = factory.Image; try { const int Orientation = (int)ExifPropertyTag.Orientation; if (!factory.PreserveExifData && factory.ExifPropertyItems.ContainsKey(Orientation)) { int rotationValue = factory.ExifPropertyItems[Orientation].Value[0]; switch (rotationValue) { case 8: // Rotated 90 right // De-rotate: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; case 3: // Bottoms up image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 6: // Rotated 90 left image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; } } return image; } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory">The current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing /// the image to process.</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class. /// </returns> /// <remarks> /// Based on <see href="http://math.stackexchange.com/questions/1070853/"/> /// </remarks> public Image ProcessImage(ImageFactory factory) { Image image = factory.Image; try { Tuple<float, bool> rotateParams = this.DynamicParameter; // Create a rotated image. image = this.RotateImage(image, rotateParams.Item1, rotateParams.Item2); if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the width EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width); // Set the height EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height); } return image; } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { float angle = this.DynamicParameter; // Center of the image float rotateAtX = Math.Abs(image.Width / 2); float rotateAtY = Math.Abs(image.Height / 2); // Create a rotated image. newImage = this.RotateImage(image, rotateAtX, rotateAtY, angle); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory">The current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing /// the image to process.</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class. /// </returns> /// <remarks> /// Based on <see href="http://math.stackexchange.com/questions/1070853/"/> /// </remarks> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { Tuple<float, bool> rotateParams = this.DynamicParameter; // Create a rotated image. newImage = this.RotateImage(image, rotateParams.Item1, rotateParams.Item2); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Image image = factory.Image; try { RotateFlipType rotateFlipType = this.DynamicParameter; // Flip image.RotateFlip(rotateFlipType); if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the width EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width); // Set the height EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height); } } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
public void ThenResultingImageSizeShouldBeLikeCropLayer(float left, float top, float right, float bottom, CropMode mode) { // When crop mode is percentage. The right and bottom values should represent // the percentage amount to remove from those sides. const int SizeX = 200; const int SizeY = 200; int expectedWidth = 160; int expectedHeight = 144; CropLayer cl = new CropLayer(left, top, right, bottom, mode); // Arrange using (Bitmap bitmap = new Bitmap(SizeX, SizeY)) using (MemoryStream memoryStream = new MemoryStream()) { bitmap.Save(memoryStream, ImageFormat.Bmp); memoryStream.Position = 0; using (ImageFactory imageFactory = new ImageFactory()) using (ImageFactory resultImage = imageFactory.Load(memoryStream).Crop(cl)) { // Act // Assert Assert.AreEqual(expectedWidth, resultImage.Image.Width); Assert.AreEqual(expectedHeight, resultImage.Image.Height); } } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; Tuple<IEdgeFilter, bool> parameters = this.DynamicParameter; IEdgeFilter filter = parameters.Item1; bool greyscale = parameters.Item2; try { ConvolutionFilter convolutionFilter = new ConvolutionFilter(filter, greyscale); // Check and assign the correct method. Don't use reflection for speed. newImage = filter is I2DEdgeFilter ? convolutionFilter.Process2DFilter((Bitmap)image) : convolutionFilter.ProcessFilter((Bitmap)image); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { Color baseColor = (Color)this.DynamicParameter; newImage = new Bitmap(image); newImage = Effects.Vignette(newImage, baseColor); // Reassign the image. image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { int threshold = (int)this.DynamicParameter; newImage = new Bitmap(image); newImage = Adjustments.Contrast(newImage, threshold); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { RoundedCornerLayer roundedCornerLayer = this.DynamicParameter; int radius = roundedCornerLayer.Radius; bool topLeft = roundedCornerLayer.TopLeft; bool topRight = roundedCornerLayer.TopRight; bool bottomLeft = roundedCornerLayer.BottomLeft; bool bottomRight = roundedCornerLayer.BottomRight; // Create a rounded image. newImage = this.RoundCornerImage(image, radius, topLeft, topRight, bottomLeft, bottomRight); image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="processor"> /// The processor. /// </param> /// <param name="factory"> /// The factory. /// </param> private static void ApplyProcessor(Func<ImageFactory, Image> processor, ImageFactory factory) { ImageInfo imageInfo = factory.Image.GetImageInfo(factory.ImageFormat); if (imageInfo.IsAnimated) { OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); // We don't dispose of the memory stream as that is disposed when a new image is created and doing so // beforehand will cause an exception. MemoryStream stream = new MemoryStream(); using (GifEncoder encoder = new GifEncoder(stream, null, null, imageInfo.LoopCount)) { foreach (GifFrame frame in imageInfo.GifFrames) { factory.Update(frame.Image); frame.Image = quantizer.Quantize(processor.Invoke(factory)); encoder.AddFrame(frame); } } stream.Position = 0; factory.Update(Image.FromStream(stream)); } else { factory.Update(processor.Invoke(factory)); } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Image image = factory.Image; int width = image.Width; int height = image.Height; Bitmap newImage = null; Bitmap edgeBitmap = null; try { HalftoneFilter filter = new HalftoneFilter(5); newImage = new Bitmap(image); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); newImage = filter.ApplyFilter(newImage); bool comicMode = this.DynamicParameter; if (comicMode) { // Draw the edges. edgeBitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb); edgeBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution); edgeBitmap = Effects.Trace(image, edgeBitmap, 120); using (Graphics graphics = Graphics.FromImage(newImage)) { // Overlay the image. graphics.DrawImage(edgeBitmap, 0, 0); Rectangle rectangle = new Rectangle(0, 0, width, height); // Draw an edge around the image. using (Pen blackPen = new Pen(Color.Black)) { blackPen.Width = 4; graphics.DrawRectangle(blackPen, rectangle); } } edgeBitmap.Dispose(); } image.Dispose(); image = newImage; } catch (Exception ex) { if (edgeBitmap != null) { edgeBitmap.Dispose(); } if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
public void BackgroundColorIsChanged() { ImageFactory imageFactory = new ImageFactory(); imageFactory.Load(@"Images\text.png"); Image original = (Image)imageFactory.Image.Clone(); imageFactory.BackgroundColor(Color.Yellow); AssertionHelpers.AssertImagesAreDifferent(original, imageFactory.Image, "because the background color operation should have been applied on {0}", imageFactory.ImagePath); }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { Tuple<int, bool> parameters = this.DynamicParameter; int degrees = parameters.Item1; bool rotate = parameters.Item2; int width = image.Width; int height = image.Height; newImage = new Bitmap(image); using (FastBitmap fastBitmap = new FastBitmap(newImage)) { if (!rotate) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { HslaColor original = HslaColor.FromColor(fastBitmap.GetPixel(x, y)); HslaColor altered = HslaColor.FromHslaColor(degrees / 360f, original.S, original.L, original.A); fastBitmap.SetPixel(x, y, altered); } } } else { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { HslaColor original = HslaColor.FromColor(fastBitmap.GetPixel(x, y)); HslaColor altered = HslaColor.FromHslaColor((original.H + (degrees / 360f)) % 1, original.S, original.L, original.A); fastBitmap.SetPixel(x, y, altered); } } } } image.Dispose(); image = newImage; } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { const float InchInCm = 0.3937007874015748f; Image image = factory.Image; try { Tuple<int, int, PropertyTagResolutionUnit> resolution = this.DynamicParameter; // Set the bitmap resolution data. // Ensure that the resolution is recalculated for bitmap since it only // supports inches. if (resolution.Item3 == PropertyTagResolutionUnit.Cm) { float h = resolution.Item1 / InchInCm; float v = resolution.Item2 / InchInCm; ((Bitmap)image).SetResolution(h, v); } else { ((Bitmap)image).SetResolution(resolution.Item1, resolution.Item2); } if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the horizontal EXIF data. int horizontalKey = (int)ExifPropertyTag.XResolution; PropertyItem horizontal = this.GetResolutionItem(horizontalKey, resolution.Item1); // Set the vertical EXIF data. int verticalKey = (int)ExifPropertyTag.YResolution; PropertyItem vertical = this.GetResolutionItem(verticalKey, resolution.Item1); factory.ExifPropertyItems[horizontalKey] = horizontal; factory.ExifPropertyItems[verticalKey] = vertical; // Set the unit data int unitKey = (int)ExifPropertyTag.ResolutionUnit; PropertyItem unit = FormatUtilities.CreatePropertyItem(); unit.Id = unitKey; unit.Len = 2; unit.Type = (short)ExifPropertyTagType.Int16; Int32Converter measure = (int)resolution.Item3; unit.Value = new[] { measure.Byte1, measure.Byte2 }; // TODO: Create nice streamline getter/setters for EXIF. factory.ExifPropertyItems[unitKey] = unit; } } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
public static ImageFactory GetInstance() { if (instance == null) { GameObject go = new GameObject(); instance = go.AddComponent<ImageFactory>(); } return instance; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Bitmap grey = null; Image image = factory.Image; byte threshold = this.DynamicParameter; try { // Detect the edges then strip out middle shades. grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter(image); grey = new BinaryThreshold(threshold).ProcessFilter(grey); // Search for the first white pixels Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(grey, 0); grey.Dispose(); newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.DrawImage( image, new Rectangle(0, 0, rectangle.Width, rectangle.Height), rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, GraphicsUnit.Pixel); } // Reassign the image. image.Dispose(); image = newImage; if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the width EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width); // Set the height EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height); } } catch (Exception ex) { grey?.Dispose(); newImage?.Dispose(); throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Image image = factory.Image; try { ResizeLayer resizeLayer = this.DynamicParameter; // Augment the layer with the extra information. resizeLayer.RestrictedSizes = this.RestrictedSizes; Size maxSize = new Size(); int maxWidth; int maxHeight; int.TryParse(this.Settings["MaxWidth"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxWidth); int.TryParse(this.Settings["MaxHeight"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxHeight); maxSize.Width = maxWidth; maxSize.Height = maxHeight; resizeLayer.MaxSize = maxSize; Resizer resizer = new Resizer(resizeLayer) { ImageFormat = factory.CurrentImageFormat, AnimationProcessMode = factory.AnimationProcessMode }; newImage = resizer.ResizeImage(image, factory.FixGamma); // Check that the original image has not been returned. if (newImage != image) { // Reassign the image. image.Dispose(); image = newImage; if (factory.PreserveExifData && factory.ExifPropertyItems.Any()) { // Set the width EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width); // Set the height EXIF data. factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height); } } } catch (Exception ex) { if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Bitmap grey = null; Image image = factory.Image; try { HaarCascade cascade = this.DynamicParameter; grey = new Bitmap(image.Width, image.Height); grey.SetResolution(image.HorizontalResolution, image.VerticalResolution); grey = MatrixFilters.GreyScale.TransformImage(image, grey); HaarObjectDetector detector = new HaarObjectDetector(cascade) { SearchMode = ObjectDetectorSearchMode.NoOverlap, ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller, ScalingFactor = 1.5f }; // Process frame to detect objects Rectangle[] rectangles = detector.ProcessFrame(grey); grey.Dispose(); newImage = new Bitmap(image); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newImage)) { using (Pen blackPen = new Pen(Color.White)) { blackPen.Width = 4; graphics.DrawRectangles(blackPen, rectangles); } } image.Dispose(); image = newImage; } catch (Exception ex) { if (grey != null) { grey.Dispose(); } if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
public void TestLoadImageFromFile(string fileName, string expectedMime) { var testPhoto = Path.Combine(this.localPath, string.Format("Images/{0}", fileName)); using (ImageFactory imageFactory = new ImageFactory()) { imageFactory.Load(testPhoto); Assert.AreEqual(testPhoto, imageFactory.ImagePath); Assert.AreEqual(expectedMime, imageFactory.MimeType); Assert.IsNotNull(imageFactory.Image); } }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <param name="image">The current image to process</param> /// <param name="newImage">The new Image to return</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image TransformImage(ImageFactory factory, Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(this.Matrix); Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); graphics.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); // Add a glow to the image. using (GraphicsPath path = new GraphicsPath()) { path.AddEllipse(rectangle); using (PathGradientBrush brush = new PathGradientBrush(path)) { // Fill a rectangle with an elliptical gradient brush that goes from orange to transparent. // This has the effect of painting the far corners transparent and fading in to orange on the // way in to the centre. brush.WrapMode = WrapMode.Tile; brush.CenterColor = Color.FromArgb(70, 255, 153, 102); brush.SurroundColors = new Color[] { Color.FromArgb(0, 0, 0, 0) }; Blend blend = new Blend { Positions = new float[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }, Factors = new float[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f } }; brush.Blend = blend; Region oldClip = graphics.Clip; graphics.Clip = new Region(rectangle); graphics.FillRectangle(brush, rectangle); graphics.Clip = oldClip; } } } } // Add a vignette to finish the effect. factory.Update(newImage); Vignette vignette = new Vignette(); newImage = (Bitmap)vignette.ProcessImage(factory); // Reassign the image. image.Dispose(); image = newImage; return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { Bitmap newImage = null; Bitmap grey = null; Image image = factory.Image; byte threshold = this.DynamicParameter; try { // Detect the edges then strip out middle shades. grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter(image); grey = new BinaryThreshold(threshold).ProcessFilter(grey); // Search for the first white pixels Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(grey, 0); grey.Dispose(); newImage = new Bitmap(rectangle.Width, rectangle.Height); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.DrawImage( image, new Rectangle(0, 0, rectangle.Width, rectangle.Height), rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, GraphicsUnit.Pixel); } // Reassign the image. image.Dispose(); image = newImage; } catch (Exception ex) { if (grey != null) { grey.Dispose(); } if (newImage != null) { newImage.Dispose(); } throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return image; }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image ProcessImage(ImageFactory factory) { try { factory.PreserveExifData = this.DynamicParameter; } catch (Exception ex) { throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); } return factory.Image; }
public void ImageIsLoadedFromFile() { foreach (FileInfo file in this.ListInputFiles()) { using (ImageFactory imageFactory = new ImageFactory()) { imageFactory.Load(file.FullName); imageFactory.ImagePath.Should().Be(file.FullName, "because the path should have been memorized"); imageFactory.Image.Should().NotBeNull("because the image should have been loaded"); } } }
public void TestAddHsl() { ImageFactory.GenerateHsl(50, 50).Add(default(HSL)); }
public void TestModifyHsl() { TestModify(ImageFactory.GenerateHsl(BunnyPath)); }
public void TestAddCmyk() { ImageFactory.GenerateCmyk(50, 50).Add(default(CMYK)); }
public void TestClearDouble() { ImageFactory.Generate(50, 50).Clear(); }
public void TestAddBgra() { ImageFactory.GenerateBgra(50, 50).Add(default(BGRA)); }
public void TestAddDouble() { ImageFactory.Generate(50, 50).Add(default(double)); }
public void TestClearComplex() { ImageFactory.GenerateComplex(50, 50).Clear(); }
public void TestIsReadOnlyBgra() { Assert.IsTrue(ImageFactory.GenerateBgra(50, 50).IsReadOnly); }
public void TestRemoveHsl() { ImageFactory.GenerateHsl(50, 50).Remove(default(HSL)); }
public void TestRemoveDouble() { ImageFactory.Generate(50, 50).Remove(default(double)); }
public ActionResult Edit(Commander input, string pwd, HttpPostedFileBase file) { using (var session = DB.Instance.GetSession()) { var commander = session.Load <Commander>(CommanderId); commander.PlayerName = input.PlayerName; commander.Story = input.Story; //Password if (!String.IsNullOrEmpty(pwd)) { if (pwd.Length < 6) { throw new Exception("Password must be at least 6 characters long"); } commander.Salt = Password.GenerateSalt(); commander.Password = Password.HashPassword(pwd, commander.Salt); } //Country foreach (var ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { var info = new RegionInfo(ci.LCID); if (info.TwoLetterISORegionName == input.Country.Code) { commander.Country = new Country { Code = info.TwoLetterISORegionName, Name = info.DisplayName }; break; } } //Image if (IsImage(file)) { var ext = Path.GetExtension(file.FileName); var imagePath = "/App_Data/upload/commander_" + CommanderId + ".jpg"; using (file.InputStream) { using (FileStream fs = new FileStream(Server.MapPath(imagePath), FileMode.Create)) { using (ImageFactory imageFactory = new ImageFactory()) { imageFactory.Load(file.InputStream) .Quality(60) .Resize(new ImageProcessor.Imaging.ResizeLayer( anchorPosition: ImageProcessor.Imaging.AnchorPosition.Center, resizeMode: ImageProcessor.Imaging.ResizeMode.Crop, upscale: false, size: new System.Drawing.Size { Width = 360, Height = 480 } )) .Format(new ImageProcessor.Imaging.Formats.JpegFormat()) .Save(fs); } } } } session.SaveChanges(); } return(RedirectToAction("View", new { id = CommanderId })); }
public void TestClearBgra() { ImageFactory.GenerateBgra(50, 50).Clear(); }
public void TestClearCmyk() { ImageFactory.GenerateCmyk(50, 50).Clear(); }
public void TestClearHsl() { ImageFactory.GenerateHsl(50, 50).Clear(); }
public void TestAddComplex() { ImageFactory.GenerateComplex(50, 50).Add(default(Complex)); }
public void TestModifyRgb() { TestModify(ImageFactory.GenerateRgb(BunnyPath)); }
public void TestAddRgb() { ImageFactory.GenerateRgb(50, 50).Add(default(RGB)); }
public void TestRemoveBgra() { ImageFactory.GenerateBgra(50, 50).Remove(default(BGRA)); }
public void TestModifyComplex() { TestModify(ImageFactory.Generate(BunnyPath).ToComplexImage()); }
public void TestIsSynchronizedComplex() { Assert.IsFalse(ImageFactory.GenerateComplex(50, 50).IsSynchronized); }
public void TestModifyCmyk() { TestModify(ImageFactory.GenerateCmyk(BunnyPath)); }
public void TestModifyDouble() { TestModify(ImageFactory.Generate(BunnyPath)); }
public void TestRemoveCmyk() { ImageFactory.GenerateCmyk(50, 50).Remove(default(CMYK)); }
public void TestModifyBgra() { TestModify(ImageFactory.GenerateBgra(BunnyPath)); }
public void TestRemoveComplex() { ImageFactory.GenerateComplex(50, 50).Remove(default(Complex)); }
public void TestIsReadOnlyCmyk() { Assert.IsTrue(ImageFactory.GenerateCmyk(50, 50).IsReadOnly); }
public void TestRemoveRgb() { ImageFactory.GenerateRgb(50, 50).Remove(default(RGB)); }
public void TestIsReadOnlyDouble() { Assert.IsTrue(ImageFactory.Generate(50, 50).IsReadOnly); }
public void TestClearRgb() { ImageFactory.GenerateRgb(50, 50).Clear(); }
public void TestIsSynchronizedBgra() { Assert.IsFalse(ImageFactory.GenerateBgra(50, 50).IsSynchronized); }