/// <summary> /// Transfers a single ARGB channel from one image to another. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <param name="sourceChannel"> /// The source channel. /// </param> /// <param name="destinationChannel"> /// The destination channel. /// </param> private static void TransferOneArgbChannelFromOneBitmapToAnother(Bitmap source, Bitmap destination, ChannelArgb sourceChannel, ChannelArgb destinationChannel) { if (source.Size != destination.Size) { throw new ArgumentException(); } Rectangle rectangle = new Rectangle(Point.Empty, source.Size); // Lockbits the source. BitmapData bitmapDataSource = source.LockBits(rectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Declare an array to hold the bytes of the bitmap. int bytes = bitmapDataSource.Stride * bitmapDataSource.Height; // Allocate a buffer for the source image byte[] sourceRgbValues = new byte[bytes]; // Copy the RGB values into the array. Marshal.Copy(bitmapDataSource.Scan0, sourceRgbValues, 0, bytes); // Unlockbits the source. source.UnlockBits(bitmapDataSource); // Lockbits the destination. BitmapData bitmapDataDestination = destination.LockBits(rectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Allocate a buffer for image byte[] destinationRgbValues = new byte[bytes]; // Copy the RGB values into the array. Marshal.Copy(bitmapDataDestination.Scan0, destinationRgbValues, 0, bytes); int s = (int)sourceChannel; int d = (int)destinationChannel; for (int i = rectangle.Height * rectangle.Width; i > 0; i--) { destinationRgbValues[d] = sourceRgbValues[s]; d += 4; s += 4; } // Copy the RGB values back to the bitmap Marshal.Copy(destinationRgbValues, 0, bitmapDataDestination.Scan0, bytes); // Unlock bits the destination. destination.UnlockBits(bitmapDataDestination); }
/// <summary> /// Transfers a single ARGB channel from one image to another. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <param name="sourceChannel"> /// The source channel. /// </param> /// <param name="destinationChannel"> /// The destination channel. /// </param> private static void TransferOneArgbChannelFromOneBitmapToAnother(Bitmap source, Bitmap destination, ChannelArgb sourceChannel, ChannelArgb destinationChannel) { if (source.Size != destination.Size) { throw new ArgumentException(); } Rectangle rectangle = new Rectangle(Point.Empty, source.Size); // Lock the source. BitmapData bitmapDataSource = source.LockBits(rectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Declare an array to hold the bytes of the bitmap. int bytes = bitmapDataSource.Stride * bitmapDataSource.Height; // Allocate a buffer for the source image byte[] sourceRgbValues = new byte[bytes]; // Copy the RGB values into the array. Marshal.Copy(bitmapDataSource.Scan0, sourceRgbValues, 0, bytes); // Unlock the source. source.UnlockBits(bitmapDataSource); // Lock the destination. BitmapData bitmapDataDestination = destination.LockBits(rectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Allocate a buffer for image byte[] destinationRgbValues = new byte[bytes]; // Copy the RGB values into the array. Marshal.Copy(bitmapDataDestination.Scan0, destinationRgbValues, 0, bytes); int s = (int)sourceChannel; int d = (int)destinationChannel; for (int i = rectangle.Height * rectangle.Width; i > 0; i--) { // Copy the alpha values across. if (destinationRgbValues[d] != 0) { destinationRgbValues[d] = sourceRgbValues[s]; } d += 4; s += 4; } // Copy the RGB values back to the bitmap Marshal.Copy(destinationRgbValues, 0, bitmapDataDestination.Scan0, bytes); // Unlock bits the destination. destination.UnlockBits(bitmapDataDestination); }