Modifier les valeurs de pixels d'une image

Hadjer91 - 20 avril 2013 à 21:54
yann_lo_san Messages postés 1137 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 23 janvier 2016 - 21 avril 2013 à 12:37
salut à tous

j'ai une question comment modifier les valeur des pixels dans
une images indexée en niveau de gris de 8 bits par exemple le pixel (4,4), la fonction SetPixel ne fonctionne pas avec ce genre d'image ??????
voila mon code j'ai chargé l'image mais je ne peux plus modifier ces valeurs

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);

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;



}

1 réponse

yann_lo_san Messages postés 1137 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 23 janvier 2016 26
21 avril 2013 à 12:37
Salut,

Voici une fonction avec quelques corrections pour que ça marche.

private void NiveauDeGris()
{
    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); 

        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;//<------------- NON !
                    *pPixel = byPixel;

                    pPixel++;//<------ PIXEL SUIVANT ICI (PTR) !
                }

                //////bmd.Stride; //<------------ NON !!!
            } 
        } 

        bmMono.UnlockBits(bmd);
        image_originale = bmMono;
    } 
} 


bye...
0
Rejoignez-nous