How to use C# to do the Lvgl Image Converter like web version?

I want to implement this conversion process in C#.
The environment is LVGL v8, with the following input and output requirements:

  • Input: PNG images (with or without background).
  • Color format: CF_TRUE_COLOR
  • Output format: Binary RGB565

How can I achieve this conversion in C#?

I wrote the following procedure, but it seems the public RGB565.bin format is slightly different from the RGB565.bin format of LVGL v8, resulting in display issues.

 public static void ConvertPngToRgb565(string inputFilePath, string outputFilePath)
 {
     using (Bitmap bitmap = new Bitmap(inputFilePath))
     {
         int width = bitmap.Width;
         int height = bitmap.Height;
         using (FileStream fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
         using (BinaryWriter writer = new BinaryWriter(fs))
         {
             for (int y = 0; y < height; y++)
             {
                 for (int x = 0; x < width; x++)
                 {
                     Color pixelColor = bitmap.GetPixel(x, y);

                     // 如果Alpha通道小於某個閾值,設定為黑色
                     if (pixelColor.A < 128)
                     {
                         pixelColor = Color.Black;
                     }

                     // 將像素轉換為RGB565格式
                     ushort rgb565 = ConvertToRgb565(pixelColor);
                     writer.Write(rgb565);
                 }
             }
         }
     }
 }

 private static ushort ConvertToRgb565(Color color)
 {
     // 將RGB值壓縮到RGB565格式
     int r = color.R >> 3; // 5位紅色
     int g = color.G >> 2; // 6位綠色
     int b = color.B >> 3; // 5位藍色

     ushort rgb565 = (ushort)((r << 11) | (g << 5) | b);
     return rgb565;
 }