Advertisement
Guest User

Bmpファイル、Jpegファイルの指定した座標のTColor値を取得、RGBに分解して表示

a guest
Jan 19th, 2013
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.99 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Memo1: TMemo;
  12.     Button1: TButton;
  13.     Button2: TButton;
  14.     procedure Button1Click(Sender: TObject);
  15.     procedure Button2Click(Sender: TObject);
  16.   private
  17.     { Private 宣言 }
  18.     procedure OutputRGB(Col: TColor);
  19.   public
  20.     { Public 宣言 }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.dfm}
  29.  
  30. uses
  31.   Jpeg;
  32.  
  33. procedure TForm1.OutputRGB(Col: TColor);
  34. type
  35.   TRGB = packed record
  36.     case Integer of
  37.       0: (Col: TColor;);
  38.       1: (R:   Byte;
  39.           G:   Byte;
  40.           B:   Byte;
  41.           I:   Byte;);
  42.   end;
  43. var
  44.   RGB:     TRGB;
  45.   R, G, B: Byte;
  46. begin
  47.   // レコード型を使った、TColor値 → R,G,Bの取り出し
  48.   RGB.Col:=Col;
  49.   Memo1.Lines.Add(Format('BGR=$%.8x',[RGB.Col]));
  50.   Memo1.Lines.Add(Format('R=$%.2x, G=$%.2x, B=$%.2x',[RGB.R, RGB.G, RGB.B]));
  51.  
  52.   // 関数(マクロ扱い)を使った、TColor値 → R,G,Bの取り出し
  53.   R:=GetRValue(Col);
  54.   G:=GetGValue(Col);
  55.   B:=GetBValue(Col);
  56.   Memo1.Lines.Add(Format('R=$%.2x, G=$%.2x, B=$%.2x',[R, G, B]));
  57. end;
  58.  
  59. procedure TForm1.Button1Click(Sender: TObject);
  60. const
  61.   // Bmpファイルを指定する
  62.   BMPFile='C:\~\Test.bmp';
  63. var
  64.   BMP: TBitmap;
  65. begin
  66.   BMP:=TBitmap.Create;
  67.   try
  68.     BMP.LoadFromFile(BMPFile);
  69.     Memo1.Lines.Add('----------'+ExtractFileName(BMPFile));
  70.     OutputRGB(BMP.Canvas.Pixels[100, 100]);
  71.   finally
  72.     BMP.Free;
  73.   end;
  74. end;
  75.  
  76. procedure TForm1.Button2Click(Sender: TObject);
  77. const
  78.   // Jpegファイルを指定する
  79.   JPGFile='C:\~\Test.jpg';
  80. var
  81.   JPG: TJPEGImage;
  82.   BMP: TBitmap;
  83. begin
  84.   JPG:=TJPEGImage.Create;
  85.   BMP:=TBitmap.Create;
  86.   try
  87.     JPG.LoadFromFile(JPGFile);
  88.     BMP.Assign(JPG);
  89.     Memo1.Lines.Add('----------'+ExtractFileName(JPGFile));
  90.     OutputRGB(BMP.Canvas.Pixels[100, 100]);
  91.   finally
  92.     BMP.Free;
  93.     JPG.Free;
  94.   end;
  95. end;
  96.  
  97. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement