天天看点

如何将一张彩色图片转换成黑白图片

如何将一张彩色图片转换成黑白图片 Delphi / Windows SDK/API

http://www.delphi2007.net/DelphiMultimedia/html/delphi_2006120713000976.html

如何将一张彩色图片转换成黑白图片

  procedure   TForm1.Button3Click(Sender:   TObject);  

  var  

      P:   PByteArray;  

      x,   y:   Integer;  

      Gray:   Integer;  

      Bmp:   TBitmap;  

  begin  

      Bmp   :=   TBitmap.Create;  

      Bmp.Assign(Image1.Picture.Bitmap);  

      Bmp.PixelFormat   :=   pf24bit;  

      for   y   :=   0   to   Bmp.Height   -   1   do  

      begin  

          P   :=   Bmp.ScanLine[y];  

          for   x   :=   0   to   Bmp.Width   -   1   do  

          begin  

              Gray   :=   (P[3*x+2]   +   P[3*x+1]   +   P[3*x])   div   3;  

              P[3*x+2]   :=   Byte(Gray);  

              P[3*x+1]   :=   Byte(Gray);  

              P[3*x]   :=   Byte(Gray);  

          end;  

      end;  

      Canvas.Draw(0,   0,   Bmp);  

      Bmp.Free;  

  end;  

或者:  

  procedure   TForm1.Button3Click(Sender:   TObject);  

  var  

      P:   PByteArray;  

      x,   y:   Integer;  

      Gray:   Integer;  

      Bmp:   TBitmap;  

  begin  

      Bmp   :=   TBitmap.Create;  

      Bmp.Assign(Image1.Picture.Bitmap);  

      Bmp.PixelFormat   :=   pf24bit;  

      for   y   :=   0   to   Bmp.Height   -   1   do  

      begin  

          P   :=   Bmp.ScanLine[y];  

          for   x   :=   0   to   Bmp.Width   -   1   do  

          begin  

              Gray   :=   Max(P[3*x+2],   P[3*x+1]);  

              Gray   :=   Max(Gray,   P[3*x]);  

              P[3*x+2]   :=   Byte(Gray);  

              P[3*x+1]   :=   Byte(Gray);  

              P[3*x]   :=   Byte(Gray);  

          end;  

      end;  

      Canvas.Draw(0,   0,   Bmp);  

      Bmp.Free;  

  end;  

或者:  

  procedure   TForm1.Button3Click(Sender:   TObject);  

  var  

      P:   PByteArray;  

      x,   y:   Integer;  

      Gray:   Integer;  

      Bmp:   TBitmap;  

  begin  

      Bmp   :=   TBitmap.Create;  

      Bmp.Assign(Image1.Picture.Bitmap);  

      Bmp.PixelFormat   :=   pf24bit;  

      for   y   :=   0   to   Bmp.Height   -   1   do  

      begin  

          P   :=   Bmp.ScanLine[y];  

          for   x   :=   0   to   Bmp.Width   -   1   do  

          begin  

              Gray   :=   Round(P[3*x+2]   *   0.3   +   P[3*x+1]   *   0.59   +   P[3*x]   *   0.11);  

              P[3*x+2]   :=   Byte(Gray);  

              P[3*x+1]   :=   Byte(Gray);  

              P[3*x]   :=   Byte(Gray);  

          end;  

      end;  

      Canvas.Draw(0,   0,   Bmp);  

      Bmp.Free;  

  end;  

mark  

  学习

转载于:https://www.cnblogs.com/delphi2007/archive/2008/08/31/1280461.html