Les Snippets

Connexion

Inverser les couleurs d'une image

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 27/03/2006 15:52:53 et initié par MorpionMx [Liste]
Date de mise à jour : 27/05/2006 12:55:28
Vue : 13956
Catégorie(s) : Graphique
Langages dispo pour ce code :
- C# 1.x, C# 2.x
- VB 2005, VB.NET 1.x
- PHP 3, PHP 4, PHP 5
- Delphi 5
- VB 2005, VB.NET 1.x
- C# 1.x, C# 2.x
- Delphi 5
- VB6, VBA
- Delphi 5



Langage : C# 1.x , C# 2.x
Date ajout : 27/03/2006
Posté par MorpionMx [Liste]
DateMAJ : 27/03/2006
public static void InvertColors(Bitmap bmp) 
{
    Color cSource; 
    Color cDest;
    for (int y = 0; y < bmp.Height; y++) 
    {
        for (int x = 0; x < bmp.Width; x++) 
        {
            cSource = bmp.GetPixel(x, y);
            cDest = Color.FromArgb(255 - cSource.R, 255 - cSource.G, 255 - cSource.B); 
            bmp.SetPixel(x, y, cDest);

        }
    }

}

Langage : VB.NET 1.x , VB 2005
Date ajout : 27/03/2006
Posté par MorpionMx [Liste]
DateMAJ : 27/03/2006
Public Shared Sub InvertColors(ByRef bmp As Bitmap) 
    Dim cSource As Color
    Dim cDest As Color 

    For y As Integer = 0 To bmp.Height - 1
        For x As Integer = 0 To bmp.Width - 1 
            cSource = bmp.GetPixel(x, y)
            cDest = Color.FromArgb(255 - cSource.R, 255 - cSource.G, 255 - cSource.B)
            bmp.SetPixel(x, y, cDest)
        Next

    Next

End Sub

Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 28/03/2006
Posté par nolii [Liste]
DateMAJ : 28/03/2006

<?php

function InvertColors($image) {
 $im = imagecreatefrompng($image);
 list($width, $height, $null,$null) = getimagesize($image);
 for($y=0;$y<=$height;$y++) {
  for($x=0;$x<=$width;$x++) {
   $color = imagecolorsforindex($im,imagecolorat($im,$x,$y));
   imagesetpixel($im,$x,$y,imagecolorallocate($im,255-$color['red'],255-$color['green'],255-$color['blue']));
  }
 }
 
 imagepng($im);
}

InvertColors('image.png');

Remarque :
Remplacer imagecreatefrompng() par imagecreatefrom[png|jpeg|gif...]() et imagepng() par image[png|jpeg,gif...]() suivant le format d'image
Langage : Delphi 5
Date ajout : 31/03/2006
Posté par florenth [Liste]
DateMAJ : 31/03/2006
{ Cette procédure utilise la propriété ScanLine[] qui permet
 d'avoir une vitesse presque trente foix supérieure que l'acces simple à 
 la propriété Pixels[] utilisant les méthodes API GetPixel() et SetPixel() }
procedure InvertBitmapColors(const Bmp: TBitmap);
type
  TLine = array[Word] of TRGBTriple;
  PLine = ^TLine;
var
  X, Y: Integer;
  OldPixelFmt: TPixelFormat;
  Line: PLine;
begin
  OldPixelFmt := Bmp.PixelFormat;
  { Mise du bitmap en 32 bits par pixel :
  pas de changement de l'octet alpha s'il existait. }
  Bmp.PixelFormat := pf24bit;
  for Y := 0 to Bmp.Height -1 do
  begin
    Line := Bmp.ScanLine[Y];
    for X := 0 to Bmp.Width -1 do
    begin
      { Inversion des couleurs. }
      Line[X].rgbtRed := not Line[X].rgbtRed;
      Line[X].rgbtGreen := not Line[X].rgbtGreen;
      Line[X].rgbtBlue := not Line[X].rgbtBlue;
    end;
  end;
  { Restauration du mode de pixel d'origine. }
  Bmp.PixelFormat := OldPixelFmt;
end;

Langage : VB.NET 1.x , VB 2005
Date ajout : 06/04/2006
Posté par Charles Racaud [Liste]
DateMAJ : 06/04/2006
Public Function InvertColors(ByVal Img As System.Drawing.Image) As System.Drawing.Image
  Dim InvertAttributes As System.Drawing.Imaging.ImageAttributes
  Dim InvertMatrix As New System.Drawing.Imaging.ColorMatrix
  InvertMatrix.Matrix00 = -1
  InvertMatrix.Matrix11 = -1
  InvertMatrix.Matrix22 = -1
  InvertMatrix.Matrix33 = 1
  InvertMatrix.Matrix40 = 1
  InvertMatrix.Matrix41 = 1
  InvertMatrix.Matrix42 = 1
  InvertMatrix.Matrix44 = 1
  InvertAttributes = New System.Drawing.Imaging.ImageAttributes()
  InvertAttributes.SetColorMatrix(InvertMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Default)
  Dim FinalImg As New System.Drawing.Bitmap(Img.Width, Img.Height)
  Dim Graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(FinalImg)
  Graphics.DrawImage(Img, New Rectangle(0, 0, FinalImg.Width, FinalImg.Height), 0, 0, FinalImg.Width, FinalImg.Height, System.Drawing.GraphicsUnit.Pixel, InvertAttributes)
  Return FinalImg
End Function
Langage : C# 1.x , C# 2.x
Date ajout : 06/04/2006
Posté par Charles Racaud [Liste]
DateMAJ : 06/04/2006
public System.Drawing.Image InvertColors(System.Drawing.Image Img)
{
  System.Drawing.Imaging.ImageAttributes InvertAttributes;
  System.Drawing.Imaging.ColorMatrix InvertMatrix = new System.Drawing.Imaging.ColorMatrix();
  InvertMatrix.Matrix00 = -1;
  InvertMatrix.Matrix11 = -1;
  InvertMatrix.Matrix22 = -1;
  InvertMatrix.Matrix33 = 1;
  InvertMatrix.Matrix40 = 1;
  InvertMatrix.Matrix41 = 1;
  InvertMatrix.Matrix42 = 1;
  InvertMatrix.Matrix44 = 1;
  InvertAttributes = new System.Drawing.Imaging.ImageAttributes();
  InvertAttributes.SetColorMatrix(InvertMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Default);
  System.Drawing.Bitmap FinalImg = new System.Drawing.Bitmap(Img.Width, Img.Height);
  System.Drawing.Graphics Graphics = System.Drawing.Graphics.FromImage(FinalImg);
  Graphics.DrawImage(Img, new Rectangle(0, 0, FinalImg.Width, FinalImg.Height), 0, 0, FinalImg.Width, FinalImg.Height, System.Drawing.GraphicsUnit.Pixel, InvertAttributes);
  return FinalImg;
}
Langage : Delphi 5
Date ajout : 11/04/2006
Posté par DRJEROME [Liste]

procedure InvertColorBmp  (bmp:TBitmap);                     begin                                                               InvertRect(bmp.canvas.handle,Rect                                      (0,0,bmp.width,bmp.height));                      bmp.Assign(bmp);                                     end;                                                         
PS: -je n'ai pas testé la vitesse de la procédure -on peut inverser des parties (de forme quelconque) du bmp  (càd par rognage) : Exemple de rognage avec ellipse :
procedure InvertEllipseColorBmp  (bmp:TBitmap); var                                             rgn1:HRgn;                                      begin                                           rgn1:=CreateEllipticRgn(                               0,0,bmp.width,bmp.height);                 InvertRgn(bmp.canvas.handle,rgn1);               bmp.Assign(bmp);                                 end;                                            
Langage : VB6 , VBA
Date ajout : 14/04/2006
Posté par PCPT [Liste]
' Picture1 va recevoir l'image normale
 Picture1.AutoSize = True 
 Picture1.Picture = LoadPicture("C:\tmp.jpg") 
 
 ' replacer, resizer...
 With Picture2 
     .Top = Picture1.Top + Picture1.Height 
     .Left = Picture1.Left 
     .Width = Picture1.Width 
     .Height = Picture1.Height 
     .AutoRedraw = True 
 End With 
 
 ' paint inversé
 Picture2.PaintPicture Picture1, 0, 0, , , , , , , vbSrcInvert
Langage : Delphi 5
Date ajout : 03/07/2008
Posté par f0xi [Liste]
procedure Negativize(src, dest : TBitmap);
var X : integer;
    pPix : ^Integer;
begin
  if Src.PixelFormat <> pf32bit then
  begin
    Src.PixelFormat  := pf32Bit;
    Dest.PixelFormat := pf32Bit;
  end;
  Dest.Assign(Src);
  pPix := Dest.ScanLine[Dest.Height-1];
  for X := 0 to (Dest.Width*Dest.Height)-1 do
  begin
    pPix^ := $00FFFFFF and (not pPix^);
    inc(pPix);
  end;
end;

Snippets en rapport avec : Image, Inverser, Couleur, Picture, Invert



Codes sources en rapport avec : Image, Inverser, Couleur, Picture, Invert

{JAVA / J2EE} FAIRE DEFILER UNE IMAGE
...

{Flash} NEGATION D'UNE IMAGE BITMAP (FLASH 8 BETA)
Voila un petit exemple suite à la source déposée concernant flash player 8 et sa gestion des pixels....

{PHP} CRÉE UNE IMAGE DE PILE DE MINIATURES EN UTILISANT LA BIBLIOTHÈQUE GD
Tout est dans le titre. Regarder la capture pour mieux comprendre. N'hésitez pas à me contacter ...

{Visual Basic, VB6, VB.NET, VB 2005} LOUPE PICTURE BOX
Une loupe agrandissant une image réduite aux dimensions de l’écran Ayant été confronté a afficher d...

{Visual Basic, VB6, VB.NET, VB 2005} CLASSER DES IMAGES EN FONCTION DE LEUR COULEUR DOMINANTE/CONTIENT UNE FONCTION DÉTECTANT LA COULEUR MOYENNE D'UNE IMAGE
Bonjour, Je poste ma première source : elle permet de classer des images en fonction de leur couleu...

{Delphi} MINIMISER LES COULEURS D' UNE IMAGE VENANT DU SCANNER OU AUTRE
Dans le même esprit que ma source : http://www.delphifr.com/codes/CHANGER-COULEUR-PIXEL-PIXELS-COUL...

{Visual Basic, VB6, VB.NET, VB 2005} PROGRESS BAR COULEURS DIFFERENTES
Bonjour, Je pense que le code est certainement deja disponible je ne veux pas faire une source de...

{Visual Basic, VB6, VB.NET, VB 2005} REDIMENSIONNER IMAGE + COMPRESSION EN JPG ( AVEC OPTION )
Bonjour, Ce controle utilisateur reprend pour plus de simplicité la methode de redimensionnement ...

{PHP} REDIMENSIONNEMENT D'UNE IMAGES, GIF, PNG, JPEG, JPG
J'éspère que ce code vous sera utile, je n'ai pas encore eu de problème avec ce code, je l'ai amélio...

{Visual Basic, VB6, VB.NET, VB 2005} TRANSFORMATION IMAGE COULEURS A IMAGE NOIR SUR BLANC
Tranformation d'une image couleurs a une image Noir sur blanc. Code bien commente pour les debutants...