gpt4 book ai didi

image - Delphi组合阵列图像

转载 作者:行者123 更新时间:2023-12-03 18:30:57 30 4
gpt4 key购买 nike

如何将多张图像合并为一张?
我有一个带有图像路径的数组。这些图像要从右侧连接。我的图像是.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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com