1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
//pointX et pointY : Abcisse et Ordonné de l'angle haut, gauche de la zone à extraire
//w : width de la zone à extraire
//h : height de la zone à extraire
public static Bitmap ExtraireImage(Bitmap srcImg,int pointX, int pointY, int w, int h)
{
const short R = 2;
const short G = 1;
const short B = 0;
// get source image size
int width = srcImg.Width;
int height = srcImg.Height;
//Verifier les parametres.
if ((pointX + w) > width || (pointY + h) > height)
throw new ArgumentException();
PixelFormat srcFmt = (srcImg.PixelFormat == PixelFormat.Format8bppIndexed) ?
PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;
// lock source bitmap data
BitmapData srcData = srcImg.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, srcFmt);
Bitmap dstImg = (srcFmt == PixelFormat.Format8bppIndexed) ?
CreateGrayscaleImage(w, h) :
new Bitmap(w, h, srcFmt);
// lock destination bitmap data
BitmapData dstData = dstImg.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.ReadWrite, srcFmt);
int srcOffset = srcData.Stride - ((srcFmt == PixelFormat.Format8bppIndexed) ? width : width * 3);
int dstOffset = dstData.Stride - ((srcFmt == PixelFormat.Format8bppIndexed) ? w : w * 3);
// do the job
unsafe
{
byte* src = (byte*)srcData.Scan0.ToPointer();
byte* dst = (byte*)dstData.Scan0.ToPointer();
if (srcFmt == PixelFormat.Format8bppIndexed)
{
//positionner le curseur sur l'angle PointX, PointY
src += pointY * (width + srcOffset)+pointX;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++, src++, dst++)
{
*dst = *src;
}
//positionner le curseur sur la ligne suivante
src += srcOffset + width - w;
dst += dstOffset;
}
}
else
{
//positionner le curseur sur l'angle PointX, PointY
src += pointY * (3* width + srcOffset) + 3*pointX;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++, src += 3, dst += 3)
{
dst[B] = src[B];
dst[G] = src[G];
dst[R] = src[R];
}
//positionner le curseur sur la ligne suivante
src += srcOffset + 3 * (width - w);
dst += dstOffset;
}
}
}
// unlock both images
dstImg.UnlockBits(dstData);
srcImg.UnlockBits(srcData);
return dstImg;
}
public static Bitmap CreateGrayscaleImage(int width, int height)
{
// create new image
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
// set palette to grayscale
SetGrayscalePalette(bmp);
// return new image
return bmp;
}
public static void SetGrayscalePalette(Bitmap srcImg)
{
// check pixel format
if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
throw new ArgumentException();
// get palette
ColorPalette cp = srcImg.Palette;
// init palette
for (int i = 0; i < 256; i++)
{
cp.Entries[i] = Color.FromArgb(i, i, i);
}
// set palette back
srcImg.Palette = cp;
} |
Partager