Exemplo n.º 1
0
 public void Change(Bitmap image, IImageFilter filter)
 {
     foreach (var pixel in image.GetPixels())
     {
         var newColor = filter.Apply(pixel.Color);
         image.SetPixel(pixel.X, pixel.Y, newColor);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the image to a silverlight bitmap, which can be assigned
        /// to a image control.
        /// </summary>
        /// <param name="image">The image, which should be converted. Cannot be null
        /// (Nothing in Visual Basic).</param>
        /// <param name="filter">The filter, which should be applied before converting the
        /// image or null, if no filter should be applied to. Cannot be null.</param>
        /// <returns>The resulting bitmap.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="filter"/> is null
        /// (Nothing in Visual Basic).</exception>
        public static WriteableBitmap ToBitmap(this ImageBase image, IImageFilter filter)
        {
            Contract.Requires <ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires <ArgumentException>(image.IsFilled, "Image has not been loaded.");

            WriteableBitmap bitmap = new WriteableBitmap(image.PixelWidth, image.PixelHeight);

            ImageBase temp = image;

            if (filter != null)
            {
                temp = new ImageBase(image);

                filter.Apply(temp, image, temp.Bounds);
            }

            byte[] pixels = temp.Pixels;

            if (pixels != null)
            {
                int[] raster = bitmap.Pixels;

                if (raster != null)
                {
                    Buffer.BlockCopy(pixels, 0, raster, 0, pixels.Length);

                    for (int i = 0; i < raster.Length; i++)
                    {
                        int abgr = raster[i];
                        int a    = (abgr >> 24) & 0xff;

                        float m = a / 255f;

                        int argb = a << 24 |
                                   (int)(((abgr >> 0) & 0xff) * m) << 16 |
                                   (int)(((abgr >> 8) & 0xff) * m) << 8 |
                                   (int)(((abgr >> 16) & 0xff) * m);
                        raster[i] = argb;
                    }
                }
            }

            bitmap.Invalidate();

            return(bitmap);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts the image to a png stream, which can be assigned to
        /// a silverlight image control as image source and applies the specified
        /// filter before converting the image.
        /// </summary>
        /// <param name="image">The image, which should be converted. Cannot be null
        /// (Nothing in Visual Basic).</param>
        /// <param name="filter">The filter, which should be applied before converting the
        /// image or null, if no filter should be applied to. Cannot be null.</param>
        /// <returns>The resulting stream.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="filter"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public static Stream ToStream(this ExtendedImage image, IImageFilter filter)
        {
            Contract.Requires <ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires <ArgumentException>(image.IsFilled, "Image has not been loaded.");

            MemoryStream memoryStream = new MemoryStream();

            try
            {
                ExtendedImage temp = image;

                if (filter != null)
                {
                    temp = image.Clone();

                    filter.Apply(temp, image, temp.Bounds);
                }

                PngEncoder encoder = new PngEncoder();
                encoder.IsWritingUncompressed = true;
                encoder.Encode(temp, memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
            }
            catch
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                    memoryStream = null;
                }
                throw;
            }

            return(memoryStream);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Applies the processor to the image.
 /// <remarks>This method does not resize the target image.</remarks>
 /// </summary>
 /// <typeparam name="TColor">The pixel format.</typeparam>
 /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
 /// <param name="source">The image this method extends.</param>
 /// <param name="sourceRectangle">
 /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
 /// </param>
 /// <param name="processor">The processors to apply to the image.</param>
 /// <returns>The <see cref="Image{TColor, TPacked}"/>.</returns>
 internal static Image <TColor, TPacked> Process <TColor, TPacked>(this Image <TColor, TPacked> source, Rectangle sourceRectangle, IImageFilter <TColor, TPacked> processor)
     where TColor : struct, IPackedPixel <TPacked>
     where TPacked : struct
 {
     return(PerformAction(source, (sourceImage) => processor.Apply(sourceImage, sourceRectangle)));
 }
Exemplo n.º 5
0
 public static Image Filter(this Image source, Rectangle rectangle, IImageFilter filter)
 {
     return(PerformAction(source, image => filter.Apply(image, rectangle)));
 }
Exemplo n.º 6
0
 public static Image Filter(this Image source, Rectangle rectangle, IImageFilter filter)
 {
     return PerformAction(source, image => filter.Apply(image, rectangle));
 }