在Delphi中使用jpeg

时间:2012-08-01 19:53:01

标签: delphi delphi-7

我如何在此处使用此功能:Create an image and colored ir?表示jpeg?

因为Bmp格式有很大的尺寸。我想在图片中小尺寸学习。

1 个答案:

答案 0 :(得分:6)

procedure TForm1.GenerateImageWithRandomColors;
var
  I, J: Integer;
  ColorHEX: string;
  Bitmap: TBitmap;
  JpegImg: TJpegImage;
begin
  Randomize;

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

    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;

    JpegImg := TJpegImage.Create;
    try
      JpegImg.Assign(Bitmap);
      JpegImg.SaveToFile('test.jpg');
    finally
      JpegImg.Free;
    end;

  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;