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
| OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "charger l'image";
if (ofd.ShowDialog() == DialogResult.OK)
{ Bitmap bmp = new Bitmap(ofd.FileName);
int cx = bmp.Width;
int cy = bmp.Height;
Bitmap bmMono = new Bitmap(cx, cy, PixelFormat.Format8bppIndexed);
bmMono.SetResolution(bmp.HorizontalResolution,
bmp.VerticalResolution);
ColorPalette pal = bmMono.Palette;
for (int i = 0; i < pal.Entries.Length; i++)
pal.Entries[i] = Color.FromArgb(i, i, i);
bmMono.Palette = pal;
BitmapData bmd = bmMono.LockBits(
new Rectangle(Point.Empty, bmMono.Size),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Scan through the pixels, converting color to B&W
unsafe
{
Byte* pPixel = (Byte*)bmd.Scan0;
for (int y = 0; y < cy; y++)
{
for (int x = 0; x < cx; x++)
{
Color clr = bmp.GetPixel(x, y);
Byte byPixel = (byte)(( clr.R ));
pPixel[x] = byPixel;
}
pPixel += bmd.Stride;
}
}
}
bmMono.UnlockBits(bmd);
image_originale.Image = bmp;
} |