Delphi 语言 图像处理库使用示例

Delphi阿木 发布于 2025-06-19 5 次阅读


Delphi 语言图像处理库使用示例详解

Delphi 是一种强大的编程语言,广泛应用于桌面应用程序的开发。它拥有丰富的组件库和工具,使得开发者可以轻松地创建功能丰富的应用程序。在图像处理领域,Delphi 也提供了相应的库来帮助开发者实现各种图像处理功能。本文将围绕 Delphi 语言图像处理库的使用,通过一系列示例,详细介绍如何利用这些库进行图像的读取、显示、编辑和保存等操作。

环境准备

在开始之前,请确保您的开发环境中已经安装了 Delphi,并且已经安装了相应的图像处理库。以下是一些常用的图像处理库:

- TMS ImageList

- VCL Graphics32

- FreeImage

以下示例将使用 TMS ImageList 库,该库提供了丰富的图像处理功能。

1. 图像读取

我们需要读取一个图像文件。以下是一个简单的示例,展示如何使用 TMS ImageList 库读取图像文件:

delphi

uses


TMS Imaging, TMS Imaging.BMP, TMS Imaging.JPEG, TMS Imaging.TIF;

procedure TForm1.ReadImage(const FileName: string);


var


Image: TBitmap;


begin


Image := TBitmap.Create;


try


// 根据文件扩展名选择合适的图像格式读取器


if LowerCase(ExtractFileExt(FileName)) = '.bmp' then


Image.LoadFromBMP(FileName)


else if LowerCase(ExtractFileExt(FileName)) = '.jpg' then


Image.LoadFromJPEG(FileName)


else if LowerCase(ExtractFileExt(FileName)) = '.tif' then


Image.LoadFromTIF(FileName)


else


raise Exception.Create('Unsupported image format.');

// 显示图像


Image.Picture.Graphic := Image;


finally


Image.Free;


end;


end;


在这个示例中,我们首先创建了一个 `TBitmap` 对象,然后根据文件扩展名选择合适的读取器来加载图像。加载完成后,我们将图像显示在窗体上。

2. 图像显示

在上面的示例中,我们已经将图像显示在窗体上。以下是一个更详细的示例,展示如何使用 `TImage` 控件来显示图像:

delphi

procedure TForm1.ShowImage(const FileName: string);


var


Image: TImage;


begin


Image := TImage.Create(Self);


try


Image.Parent := Self;


Image.Align := alClient;


Image.Picture.LoadFromFile(FileName);


finally


Image.Free;


end;


end;


在这个示例中,我们创建了一个 `TImage` 控件,并将其设置为窗体的客户端。然后,我们使用 `LoadFromFile` 方法加载图像。

3. 图像编辑

Delphi 的图像处理库提供了丰富的编辑功能,例如旋转、缩放、裁剪等。以下是一个示例,展示如何旋转图像:

delphi

procedure TForm1.RotateImage(var Image: TBitmap; Angle: Integer);


var


RotatedImage: TBitmap;


begin


RotatedImage := TBitmap.Create;


try


RotatedImage.Width := Image.Width;


RotatedImage.Height := Image.Height;


RotatedImage.Canvas.Pen.Color := clBlack;


RotatedImage.Canvas.Pen.Width := 1;


RotatedImage.Canvas.Brush.Color := clWhite;


RotatedImage.Canvas.FillRect(Rect(0, 0, RotatedImage.Width, RotatedImage.Height));

// 旋转图像


RotatedImage.Canvas.Rotate(Angle);


RotatedImage.Canvas.Draw(0, 0, Image);


finally


Image.Free;


Image := RotatedImage;


end;


end;


在这个示例中,我们创建了一个新的 `TBitmap` 对象来存储旋转后的图像。然后,我们使用 `Canvas.Rotate` 方法来旋转图像。

4. 图像保存

我们需要将编辑后的图像保存到文件中。以下是一个示例,展示如何保存图像:

delphi

procedure TForm1.SaveImage(const FileName: string);


var


Image: TBitmap;


begin


Image := TBitmap.Create;


try


Image.Assign(Self.Image1.Picture.Graphic);


// 根据文件扩展名选择合适的图像格式保存器


if LowerCase(ExtractFileExt(FileName)) = '.bmp' then


Image.SaveToBMP(FileName)


else if LowerCase(ExtractFileExt(FileName)) = '.jpg' then


Image.SaveToJPEG(FileName)


else if LowerCase(ExtractFileExt(FileName)) = '.tif' then


Image.SaveToTIF(FileName)


else


raise Exception.Create('Unsupported image format.');


finally


Image.Free;


end;


end;


在这个示例中,我们首先将图像赋值给一个新的 `TBitmap` 对象,然后根据文件扩展名选择合适的保存器来保存图像。

总结

本文通过一系列示例,详细介绍了 Delphi 语言图像处理库的使用。从图像的读取、显示、编辑到保存,我们展示了如何利用这些库来实现各种图像处理功能。通过学习和实践这些示例,开发者可以更好地掌握 Delphi 图像处理库,并将其应用于实际项目中。