Delphi WebP Image Conversion — Using the delphi-webp Library (with Sample Code)
If you want to convert images to WebP format in Delphi, this guide will help you get started.
In this article, we explain how to use the open‑source delphi-webp library from GitHub to convert JPG and PNG images into WebP.
You will learn how to place the required DLLs, which Pascal units to include, and how to use the library through practical, working sample code.
WebP is known for its high compression and high image quality, making it an effective format for reducing application size and improving performance.
The delphi-webp library can be downloaded from the following URL:
https://github.com/Wykerd/delphi-webp
Download it via Code ⇒ Download ZIP.
After extracting the ZIP file, place the following DLLs in your project’s output folders:
bin\x64\libwebp.dll — for 64‑bit builds (place in Win64\Debug or Win64\Release)
bin\x86\libwebp.dll — for 32‑bit builds (place in Win32\Debug or Win32\Release)
Additionally, copy the following three Pascal files from the extracted ZIP into your project folder:
bin\libwebp.pas, bin\Vcl.WebpImage.pas, bin\WebpHelpers.pas
Converting a JPG File to WebP
The following source code shows how to convert a JPG file to WebP format.
Make sure to place the libwebp.dll file in the same directory as your executable.
You must also copy libwebp.pas, Vcl.WebpImage.pas, and WebpHelpers.pas into your project folder.
uses
Winapi.GDIPAPI, Winapi.GDIPOBJ, libwebp, WebpHelpers;
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TGPBitmap;
strm: TMemoryStream;
quality: Single;
begin
strm := TMemoryStream.Create;
bmp := TGPBitmap.Create('..\..\01.jpg');
try
quality := 80.0; // Quality (0–100)
WebpEncode(strm, bmp, quality);
strm.SaveToFile('..\..\01.webp');
finally
strm.Free;
bmp.Free;
end;
end;
Converting a PNG File to WebP
The following source code shows how to convert a PNG file to WebP format.
Make sure to place the libwebp.dll file in the same directory as your executable.
You must also copy libwebp.pas, Vcl.WebpImage.pas, and WebpHelpers.pas into your project folder.
uses
Winapi.GDIPAPI, Winapi.GDIPOBJ, libwebp, WebpHelpers;
procedure TForm1.Button2Click(Sender: TObject);
var
bmp: TGPBitmap;
strm: TMemoryStream;
quality: Single;
begin
strm := TMemoryStream.Create;
bmp := TGPBitmap.Create('..\..\02.png');
try
quality := 80.0; // Quality (0–100)
WebpEncode(strm, bmp, quality);
strm.SaveToFile('..\..\02.webp');
finally
strm.Free;
bmp.Free;
end;
end;
Converting a WebP File to PNG
The following source code shows how to convert a WebP file to PNG format.
Make sure to place the libwebp.dll file in the same directory as your executable.
You must also copy libwebp.pas, Vcl.WebpImage.pas, and WebpHelpers.pas into your project folder.
uses
Winapi.GDIPAPI, Winapi.GDIPOBJ, libwebp, WebpHelpers;
procedure TForm1.Button3Click(Sender: TObject);
var
bmp: TGPBitmap;
strm: TMemoryStream;
data: PByte;
begin
strm := TMemoryStream.Create();
try
strm.LoadFromFile('..\..\01.webp');
WebpDecode(strm, data, bmp);
bmp.Save('..\..\01.png', gPNG);
finally
bmp.Free;
strm.Free;
WebPFree(data);
end;
end;
TWebPImage
Using the TWebPImage component, you can display WebP images directly on a form.
uses
Winapi.GDIPAPI, Winapi.GDIPOBJ, libwebp, WebpHelpers, Vcl.WebpImage;
procedure TForm1.Button4Click(Sender: TObject);
var
webp: TWebPImage;
graphics: TGPGraphics;
bmp: TBitmap;
begin
webp := TWebPImage.Create(Self);
webp.Parent := Self;
webp.LoadFromFile('..\..\01.webp');
webp.Left := 0;
webp.Top := 0;
webp.Width := webp.Bitmap.GetWidth;
webp.Height := webp.Bitmap.GetHeight;
end;
