SetPixels() public method

Sets the pixel array of the image.
/// is smaller than zero. /// - or - /// is smaller than zero. /// - or - /// is not a multiple of four, /// width and height. /// is null.
public SetPixels ( int width, int height, byte pixels ) : void
width int The new width of the image. /// Must be greater than zero.
height int The new height of the image. /// Must be greater than zero.
pixels byte The array with colors. Must be a multiple /// of four, width and height.
return void
示例#1
0
        static void Rotate90(SimpleImage source, SimpleImage target)
        {
            //Contract.Requires(source != null);
            //Contract.Requires(source.IsFilled);
            //Contract.Requires(target != null);
            //Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will just be calculated by swapping
                    // the x and the y value for the pixel.
                    newIndex = ((x + 1) * source.PixelHeight - y - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
示例#2
0
        static void Rotate180(SimpleImage source, SimpleImage target)
        {
            //Contract.Requires(source != null);
            //Contract.Requires(source.IsFilled);
            //Contract.Requires(target != null);
            //Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelHeight + x) * 4;

                    // The new index will be calculated as if the image would be flipped
                    // at the x and the y axis.
                    newIndex = ((source.PixelHeight - y - 1) * source.PixelWidth + source.PixelWidth - x - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelWidth, source.PixelHeight, targetPixels);
        }
示例#3
0
        static void Rotate270(SimpleImage source, SimpleImage target)
        {
            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will be calculated as if the image would be flipped
                    // at the x and the y axis and rotated about 90 degrees.
                    newIndex = ((source.PixelWidth - x - 1) * source.PixelHeight + y) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
示例#4
0
        /// <summary>
        /// Transforms the specified image by flipping and rotating it. First the image
        /// will be rotated, then the image will be flipped. A new image will be returned. The original image
        /// will not be changed.
        /// </summary>
        /// <param name="source">The image, which should be transformed.</param>
        /// <param name="target">The new transformed image.</param>
        /// <param name="rotationType">Type of the rotation.</param>
        /// <param name="flippingType">Type of the flipping.</param>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="source"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="target"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        internal static void Transform(SimpleImage source, SimpleImage target, RotationType rotationType, FlippingType flippingType)
        {
            //Contract.Requires<ArgumentNullException>(source != null, "Source image cannot be null.");
            //Contract.Requires<ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            //Contract.Requires<ArgumentNullException>(target != null, "Target image cannot be null.");

            switch (rotationType)
            {
            case RotationType.None:
            {
                byte[] targetPixels = source.Pixels;
                byte[] sourcePixels = new byte[targetPixels.Length];

                Array.Copy(targetPixels, sourcePixels, targetPixels.Length);

                target.SetPixels(source.PixelWidth, source.PixelHeight, sourcePixels);
            }
            break;

            case RotationType.Rotate90:
            {
                Rotate90(source, target);
            }
            break;

            case RotationType.Rotate180:
            {
                Rotate180(source, target);
            }
            break;

            case RotationType.Rotate270:
            {
                Rotate270(source, target);
            }
            break;

            default:
                throw new InvalidOperationException();
            }

            switch (flippingType)
            {
            case FlippingType.FlipX:
                FlipX(target);
                break;

            case FlippingType.FlipY:
                FlipY(target);
                break;
            }
        }
        /// <summary>
        /// Transforms the specified image by flipping and rotating it. First the image
        /// will be rotated, then the image will be flipped. A new image will be returned. The original image
        /// will not be changed.
        /// </summary>
        /// <param name="source">The image, which should be transformed.</param>
        /// <param name="target">The new transformed image.</param>
        /// <param name="rotationType">Type of the rotation.</param>
        /// <param name="flippingType">Type of the flipping.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para><paramref name="source"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="target"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        internal static void Transform(SimpleImage source, SimpleImage target, RotationType rotationType, FlippingType flippingType)
        {
            //Contract.Requires<ArgumentNullException>(source != null, "Source image cannot be null.");
            //Contract.Requires<ArgumentException>(source.IsFilled, "Source image has not been loaded.");
            //Contract.Requires<ArgumentNullException>(target != null, "Target image cannot be null.");

            switch (rotationType)
            {
                case RotationType.None:
                    {
                        byte[] targetPixels = source.Pixels;
                        byte[] sourcePixels = new byte[targetPixels.Length];

                        Array.Copy(targetPixels, sourcePixels, targetPixels.Length);

                        target.SetPixels(source.PixelWidth, source.PixelHeight, sourcePixels);
                    }
                    break;
                case RotationType.Rotate90:
                    {
                        Rotate90(source, target);
                    }
                    break;
                case RotationType.Rotate180:
                    {
                        Rotate180(source, target);
                    }
                    break;
                case RotationType.Rotate270:
                    {
                        Rotate270(source, target);
                    }
                    break;
                default:
                    throw new InvalidOperationException();
            }

            switch (flippingType)
            {
                case FlippingType.FlipX:
                    FlipX(target);
                    break;
                case FlippingType.FlipY:
                    FlipY(target);
                    break;
            }
        }
        static void Rotate90(SimpleImage source, SimpleImage target)
        {
            //Contract.Requires(source != null);
            //Contract.Requires(source.IsFilled);
            //Contract.Requires(target != null);
            //Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelWidth + x) * 4;

                    // The new index will just be calculated by swapping
                    // the x and the y value for the pixel.
                    newIndex = ((x + 1) * source.PixelHeight - y - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelHeight, source.PixelWidth, targetPixels);
        }
        static void Rotate180(SimpleImage source, SimpleImage target)
        {
            //Contract.Requires(source != null);
            //Contract.Requires(source.IsFilled);
            //Contract.Requires(target != null);
            //Contract.Ensures(target.IsFilled);

            int oldIndex = 0, newIndex = 0;

            byte[] sourcePixels = source.Pixels;
            byte[] targetPixels = new byte[source.PixelWidth * source.PixelHeight * 4];

            for (int y = 0; y < source.PixelHeight; y++)
            {
                for (int x = 0; x < source.PixelWidth; x++)
                {
                    oldIndex = (y * source.PixelHeight + x) * 4;

                    // The new index will be calculated as if the image would be flipped
                    // at the x and the y axis.
                    newIndex = ((source.PixelHeight - y - 1) * source.PixelWidth + source.PixelWidth - x - 1) * 4;

                    targetPixels[newIndex + 0] = sourcePixels[oldIndex + 0];
                    targetPixels[newIndex + 1] = sourcePixels[oldIndex + 1];
                    targetPixels[newIndex + 2] = sourcePixels[oldIndex + 2];
                    targetPixels[newIndex + 3] = sourcePixels[oldIndex + 3];
                }
            }
            target.SetPixels(source.PixelWidth, source.PixelHeight, targetPixels);
        }