创建一个图像和彩色红色?

时间:2012-07-30 19:24:03

标签: delphi delphi-7

如何创建图像?如何使用十六进制颜色代码逐个像素地对其进行着色?

对于前。我想创建一个100x100像素的图像,我想要1x1区域的颜色是'$ 002125',2x2区域的颜色是'$ 125487'....我该怎么办?

感谢您的回答..

1 个答案:

答案 0 :(得分:4)

为您制作一个简单的样品。使用Canvas.Pixels而不是Scanline。 Scanline虽然速度更快,但一开始我觉得它很合适。颜色是随机生成的,因此您只需要替换这部分代码。

    procedure TForm1.GenerateImageWithRandomColors;
    var
      Bitmap: TBitmap;
      I, J: Integer;
      ColorHEX: string;

    begin
      Bitmap := TBitmap.Create;
      Randomize;

      try
        Bitmap.PixelFormat := pf24bit;
        Bitmap.Width := 100;
        Bitmap.Height := 100;

        for I := 0 to Pred(Bitmap.Width) do
        begin
          for J := 0 to Pred(Bitmap.Height) do
          begin
            Bitmap.Canvas.Pixels[I, J] := RGB(Random(256),
               Random(256),
               Random(256));

            // get the HEX value of color and do something with it
            ColorHEX := ColorToHex(Bitmap.Canvas.Pixels[I, J]);
          end;
        end;

        Bitmap.SaveToFile('test.bmp');
      finally
        Bitmap.Free;
      end;
    end;

function TForm1.ColorToHex(Color : TColor): string;
begin
  Result :=
     IntToHex(GetRValue(Color), 2) +
     IntToHex(GetGValue(Color), 2) +
     IntToHex(GetBValue(Color), 2);
end;