Search code examples
delphidelphi-10.3-rioskia4delphi

How to fix "Cannot assign a TSkGraphic to a TBitmap"


I use Skia4Delphi in my VCL application. I have an Image1 (TImage) component. It supports loading webp image files thanks to Skia4Delphi. However I get "Cannot assign a TSkGraphic to a TBitmap" error when I try to convert a webp image to TBitmap. No error message for PNG or JPEG files.

Here is an example code:

uses System.Skia, VCL.Skia;

var
  Bitmap: TBitmap;
begin
  Image1.Picture.LoadFromFile('c:\image.webp'); // No error
  Bitmap := TBitmap.Create;
  try
    Bitmap.Assign(Image1.Picture.Graphic); // Error for webp files
  finally
    Bitmap.Free;
  end;
end;

How to fix the error and load the webp image into TBitmap?


Solution

  • The suggestion Robert wrote as a comment works.

    uses System.Skia, VCL.Skia;
    
    var
      Bitmap: TBitmap;
    begin
      Image1.Picture.LoadFromFile('c:\image.webp'); // No error
      Bitmap := TBitmap.Create;
      try
        //Bitmap.Assign(Image1.Picture.Graphic); // Error for webp files
        Bitmap.Width:=Image1.Picture.Width;
        Bitmap.Height:=Image1.Picture.Height;
        Bitmap.Canvas.Draw(0,0,Image1.Picture.Graphic);
      finally
        Bitmap.Free;
      end;
    end;