unreal RenderTarget存本地PNG

bool APIPCamera::SaveRenderTargetToFile(UTextureRenderTarget2D* rt, const FString& fileDestination)
{
    {
        FTextureRenderTargetResource* rtResource = rt->GameThread_GetRenderTargetResource();
        FReadSurfaceDataFlags readPixelFlags(RCM_UNorm);
 
        TArray<FColor> outBMP;
        outBMP.AddUninitialized(rt->GetSurfaceWidth() * rt->GetSurfaceHeight());
        rtResource->ReadPixels(outBMP, readPixelFlags);
 
        for (FColor& color : outBMP)
            color.A = 255;
 
        FIntPoint destSize(rt->GetSurfaceWidth(), rt->GetSurfaceHeight());
        TArray<uint8> CompressedBitmap;
        FImageUtils::CompressImageArray(destSize.X, destSize.Y, outBMP, CompressedBitmap);
        bool imageSavedOk = FFileHelper::SaveArrayToFile(CompressedBitmap, *fileDestination);
        return imageSavedOk;
    }
}

  

posted @ 2024-05-30 18:47  阿华小青蛙  阅读(28)  评论(0编辑  收藏  举报