This is the code you need:
protected override void OnPaint( PaintEventArgs e )
{
using ( var bmp = new Bitmap( 100, 100 ) )
using ( var g = Graphics.FromImage( bmp ) )
using ( var ia = new ImageAttributes() )
{
// 1. create a sample bitmap
g.Clear( Color.White );
var p = Point.Empty;
foreach ( var color in new Color[] { Color.Black, Color.Gray, Color.LightBlue, Color.Green, Color.Red, Color.Magenta } )
using ( var brush = new SolidBrush( color ) )
{
g.DrawString( "Some sample text", SystemFonts.DefaultFont, brush, p );
p.Offset( 0, 16 );
}
// 2. transfer the bitmap on screen
e.Graphics.DrawImage( bmp, Point.Empty );
// 3. transfer a part of the bitmap on screen again, this time removing all blue
ia.SetColorMatrix( new ColorMatrix( new float[][] {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}} ) );
e.Graphics.DrawImage(
bmp,
new Rectangle( 30, 0, 40, 100 ),
30, 0, 40, 100,
GraphicsUnit.Pixel,
ia );
}
}