指定した Format32bppArgb 形式のビットマップの指定した範囲のコピーを作成します。
プロジェクト-プロジェクト名のプロパティ-構成プロパティ-ビルドの
セーフモード以外のコードブロックの許可をtrueにする必要があります。
/// <summary>
/// 指定した Format32bppArgb 形式のビットマップの指定した範囲のコピーを作成します。
/// </summary>
/// <remarks>
/// プロジェクト-プロジェクト名のプロパティ-構成プロパティ-ビルドの
/// セーフモード以外のコードブロックの許可をtrueにする必要があります。
///
/// rectSrc が bitmapSrc の範囲外かのチェックは行っておりません。
/// </remarks>
/// <param name="bitmapSrc">Format32bppArgb 形式のビットマップ</param>
/// <param name="rectSrc">コピー範囲の四角形</param>
/// <returns>Bitmap コピーしたFormat32bppArgb 形式のビットマップ</returns>
public Bitmap CopyBitmap( Bitmap bitmapSrc, Rectangle rectSrc )
{
int x, y, ySrc, yDst ;
Color color ;
Bitmap bitmapDst = new Bitmap( rectSrc.Width, rectSrc.Height ) ;
Rectangle rectDst = new Rectangle( 0,0, bitmapDst.Width, bitmapDst.Height ) ;
BitmapData bitmapDataSrc = bitmapSrc.LockBits( rectSrc, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ) ;
BitmapData bitmapDataDst = bitmapDst.LockBits( rectDst, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ) ;
unsafe
{
uint * src = (uint *)(void *)bitmapDataSrc.Scan0 ;
uint * dst = (uint *)(void *)bitmapDataDst.Scan0 ;
for( y=0 ; y < rectSrc.Height ; y++ )
{
ySrc = y*bitmapDataSrc.Stride/sizeof(uint) ;
yDst = y*bitmapDataDst.Stride/sizeof(uint) ;
for( x=0 ; x < rectSrc.Width ; x++ )
{
color = Color.FromArgb( (int)src[ySrc + x] ) ;
dst[yDst + x] = (uint)color.ToArgb() ;
}
}
}
bitmapDst.UnlockBits(bitmapDataDst);
bitmapSrc.UnlockBits(bitmapDataSrc);
return bitmapDst ;
}