image - Delphi组合阵列图像

标签 image delphi

如何将多张图像合并为一张?
我有一个带有图像路径的数组。这些图像要从右侧连接。我的图像是.png格式。

谢谢

我的测试代码不起作用:

procedure TCreatorProject.MergeImageList(Images: TImagesList);
var
  Img,ImageOut: TGraphic;
  ImageTemp: TBitmap;
  I: Integer;
  FileOutput: String;
begin
  FileOutput := ExtractFilePath(Application.ExeName)+'temp\tempimage.png';

  for I := Low(Images) to High(Images) do
    if not FileExists(Images[I]) then
      raise Exception.Create('Chyba: obrázek nebyl nalezen !');

  ImageTemp := TBitmap.Create;
  try

    for I := Low(Images) to High(Images) do
    begin
      Img.LoadFromFile(Images[I]);

      ImageTemp.Width := Img.Width;
      ImageTemp.Height := Img.Height;

      if I = 0 then
        ImageTemp.Canvas.Draw(0, 0, Img)
      else
        ImageTemp.Canvas.Draw(ImageTemp.Width, ImageTemp.Height, Img);
    end;

    ImageOut := TPNGObject.Create;

    ImageOut.Assign(ImageTemp);

    ImageOut.SaveToFile(FileOutput);
  finally
    ImageTemp.Free;
    Img.Free;
    ImageOut.Free;
  end;

end;

最佳答案

您始终在同一位置绘制,并且不放大输出图像。
尝试以下方法(使用DelphiXE7编写并经过测试):

program SO32558735;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.IOUtils, System.Types, Vcl.Imaging.pngimage, Vcl.Graphics;

type
  TPngImages = array of TPngImage;

function LoadImages(Path : String) : TPngImages;

var
  Index   : Integer;
  Files   : TStringDynArray;

begin
 Files := TDirectory.GetFiles(Path, '*.png');
 SetLength(Result, Length(Files));
 for Index := Low(Files) to High(Files) do
  begin
   Result[Index] := TPngImage.Create;
   Result[Index].LoadFromFile(Files[Index]);
  end;
end;

function MergeImages(Images: TPngImages) : TPngImage;

var
  Image  : TPngImage;
  Bmp    : TBitmap;
  X      : Integer;

begin
 Result := TPngImage.Create;
 Bmp := TBitmap.Create;
 X := 0;
 try
  for Image in Images do
   begin
    // enlarge output image
    Bmp.Width := Bmp.Width+Image.Width;
    // adjust height if images are not equal
    if Image.Height > Bmp.Height then
     Bmp.Height := Image.Height;
    Bmp.Canvas.Draw(X, 0, Image);        
    X := Bmp.Width+1;
   end;
  Result.Assign(Bmp);
 finally
  Bmp.Free;
 end;
end;

var
 Images : TPngImages;
 Image  : TPngImage;

begin
  try
   try
    Images := LoadImages('<input path>');
    if Length(Images) < 1 then
     raise Exception.Create('No files found!');
    Image := MergeImages(Images);
    Image.SaveToFile('<output file>');
    Image.Free;
   finally
    for Image in Images do
     Image.Free;
   end;
  except
   on E: Exception do
    Writeln(E.ClassName, ': ', E.Message);
  end;
end.

关于image - Delphi组合阵列图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558735/

相关文章:

python - 更改 matplotlib 按钮小部件的图像

c# - 重绘图像 WPF

html - 将图像置于中心,左右边距最小

javascript - 清除特定图像的缓存

windows - 所见即所得与 Unicode

delphi - 如何使用 Delphi 中的 Indy 客户端从服务器读取所有字节?

用于操作图像的 C++ 库

delphi - Delphi FastMM4如何读取MemoryManager_EventLog.txt?

delphi - Delphi 中组件的定位提示

ios - Delphi XE10.2.3 iOS设备系统状态栏