A lightweight and simple native DLL wrapper for Google's libwebp, allowing basic WebP encoding from C# and other .NET environments.
Note
This project (including the wrapper code and C# examples) was automatically generated by AI. It is highly recommended to perform thorough testing and implement proper error handling before using it in a production environment.
- libwebp Wrapper: Statically links
libwebp1.6.0. - Self-Contained: No additional external runtime dependencies required.
- Designed for .NET: Supports raw BGRA/RGBA pixel data input (suitable for
WriteableBitmap, etc.). - Minimal API: Provides straightforward encoding functions without complex configurations.
Below is an example of P/Invoke definitions and usage for integrating this DLL into a C# project.
internal static partial class WebpEncoder
{
private const string DllName = "WebpEncoder.dll";
// WebP Encode (BGRA Input)
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int EncodeBGRAtoWebP(
IntPtr bgra, // Pointer to raw BGRA pixel data
int width,
int height,
int stride,
float quality,
int lossless,
out IntPtr output,
out nuint outputSize
);
// WebP Encode (RGBA Input)
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int EncodeRGBAtoWebP(
IntPtr rgba, // Pointer to raw RGBA pixel data
int width,
int height,
int stride,
float quality,
int lossless,
out IntPtr output,
out nuint outputSize
);
// Free allocated memory on the native side
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void FreeWebP(IntPtr data);
}
// Usage Example
// 1. Prepare your raw RGBA pixel data as a byte array (or pointer)
byte[] rgba = GetRawRgbaPixels();
int width = 1920;
int height = 1080;
int stride = width * 4;
float quality = 90.0f;
int lossless = 0; // 0 for lossy, 1 for lossless
// 2. Call the native WebP encoder
int ok = WebpEncoder.EncodeRGBAtoWebP(
rgba, width, height, stride, quality, lossless,
out IntPtr nativePtr, out nuint outputSize
);
if (ok == 1 && outputSize > 0)
{
// 3. Copy the encoded native bytes back to managed C# memory
byte[] webpData = new byte[(int)outputSize];
Marshal.Copy(nativePtr, webpData, 0, (int)outputSize);
// 4. CRITICAL: Always free the native memory to prevent memory leaks!
WebpEncoder.FreeWebP(nativePtr);
// 5. Save or use your WebP data
File.WriteAllBytes("output.webp", webpData);
}- The
outputparameter is a pointer-to-pointer (e.g.,out IntPtrin C#) used to receive the allocated memory address. - The
outputbuffer is allocated internally by thelibwebplibrary. - Ensure
FreeWebPis always executed after usage to properly release memory. - The
strideparameter represents the data width of a single row in bytes. - For 4-channel (RGBA/BGRA) images, the
strideis typically calculated aswidth * 4. - Providing an incorrect or mismatched pixel format will result in a corrupted output image.
- Instantly saving screenshots as WebP files.
- Speeding up basic image conversion tools.
- Calling lightweight native processing directly from .NET applications.
For more comprehensive details, please refer to the following documents:
As this is a very thin wrapper, advanced features such as metadata handling or detailed encoding configurations are currently unsupported.
This project statically links the official libwebp library provided by Google.
- Google libwebp Repository: webpmux / libwebp (chromium.googlesource.com)
- For detailed licensing information and credit details regarding
libwebpand other components, please refer to the accompanying ThirdPartyNotices.txt file included in this repository.
This project is licensed under the MIT License - see the LICENSE file for details. Copyright (c) 2026 htkz80s
JAPANESE README SECTION / 日本語ドキュメント
Googleの libwebp をC#などの.NET環境から呼び出すための、シンプルなネイティブDLLラッパー。
Note
このプロジェクト(ラッパー部分、およびC#のサンプルコード)は、AIによって自動生成されたものです。実際の利用環境に合わせた検証やエラーハンドリングの追加を推奨します。
- libwebpのラップ:
libwebp1.6.0 を静的リンク。 - 依存関係の集約: ランタイム時の追加外部依存は不要。
- .NET向けの設計: BGRA/RGBAピクセルデータ(
WriteableBitmapなど)の処理に対応。 - 最小限のAPI: 複雑な設定を省いた単純なエンコード機能。
このDLLをC#プロジェクトで利用するためのP/Invoke定義と実装例。
internal static partial class WebpEncoder
{
private const string DllName = "WebpEncoder.dll";
// WebP Encode (BGRA Input)
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int EncodeBGRAtoWebP(
IntPtr bgra, // Pointer to raw BGRA pixel data
int width,
int height,
int stride,
float quality,
int lossless,
out IntPtr output,
out nuint outputSize
);
// WebP Encode (RGBA Input)
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int EncodeRGBAtoWebP(
IntPtr rgba, // Pointer to raw RGBA pixel data
int width,
int height,
int stride,
float quality,
int lossless,
out IntPtr output,
out nuint outputSize
);
// Free allocated memory in native side
[LibraryImport(DllName)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void FreeWebP(IntPtr data);
}
// 実装例
// 1. 変換元のRGBAピクセルデータをバイト配列等で用意
byte[] rgba = GetRawRgbaPixels();
int width = 1920;
int height = 1080;
int stride = width * 4;
float quality = 90.0f;
int lossless = 0; // 0: ロッシー圧縮 / 1: ロスレス圧縮
// 2. ネイティブのWebPエンコーダを呼び出す
int ok = WebpEncoder.EncodeRGBAtoWebP(
rgba, width, height, stride, quality, lossless,
out IntPtr nativePtr, out nuint outputSize
);
if (ok == 1 && outputSize > 0)
{
// 3. エンコードされたネイティブ側のバイトデータをC#のメモリにコピー
byte[] webpData = new byte[(int)outputSize];
Marshal.Copy(nativePtr, webpData, 0, (int)outputSize);
// 4. 重要: メモリリークを防ぐため、必ずネイティブメモリを解放してください!
WebpEncoder.FreeWebP(nativePtr);
// 5. 取得したWebPデータを保存、または利用
File.WriteAllBytes("output.webp", webpData);
}outputはアドレスを受け取るためのポインタのポインタ(C#ではout IntPtr等)outputは libwebp 側で確保される- 使用後は必ず
FreeWebPで解放すること strideは「1行あたりのバイト数」- 4チャンネル(RGBA/BGRA)画像の場合、基本的には
stride = width * 4となる - ピクセルフォーマットが一致していないと壊れた画像になる
- スクリーンショットの即時WebP保存
- 画像変換ツールの高速化
- .NETアプリからの軽量ネイティブ処理呼び出し
詳細は下記ドキュメントを参照してください:
かなり薄いラッパーなので、高度な設定(メタデータ保持・詳細なエンコードパラメータチューニングなど)は未対応です。
本プロジェクトは、Googleが提供する公式の libwebp ライブラリを静的リンクしています。
- Google libwebp リポジトリ: webpmux / libwebp (chromium.googlesource.com)
libwebpを含む利用コンポーネントの著作権表記およびライセンス条項の詳細は、本リポジトリ内に同梱されている ThirdPartyNotices.txt をご確認ください。
本プロジェクトはMITライセンスのもとで公開されています。詳細は同梱の LICENSE ファイルをご確認ください。 Copyright (c) 2026 htkzr80s