当前位置:   article > 正文

UE4中实现截图功能并保存到指定路径_ue如何修改截图保存路径

ue如何修改截图保存路径

为方便起见,都是用全局函数和变量,而不是在类中实现

代码如下:

  1. FDelegateHandle GCaptureHandle;
  2. static void OnCaptureScreenshotComplete(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
  3. {
  4. FString ScreenShotName = FPaths::GameSavedDir() + TEXT("ScreenShot.png");
  5. TArray<uint8> CompressedBitmap;
  6. FImageUtils::CompressImageArray(InWidth, InHeight, InColors, CompressedBitmap);
  7. FFileHelper::SaveArrayToFile(CompressedBitmap, *ScreenShotName);
  8. UGameViewportClient::OnScreenshotCaptured().Remove(GCaptureHandle);
  9. }
  10. void CaptureScreenshot()
  11. {
  12. if (GEngine && GEngine->GameViewport)
  13. {
  14. GCaptureHandle = UGameViewportClient::OnScreenshotCaptured().AddStatic(&OnCaptureScreenshotComplete);
  15. GEngine->GameViewport->Exec(nullptr, TEXT("HighResShot 1080x720"), *GLog);//分辨率可自己设定
  16. }
  17. }

 

图片保存为png格式,如果想保存为jpg格式,可改为如下代码:

  1. static void OnCaptureScreenshotComplete(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
  2. {
  3. FString ScreenShotName = FPaths::GameSavedDir() + TEXT("ScreenShot.jpg");
  4. IImageWrapperModule& ImageWrapperModule = FModuleManager::Get().LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
  5. IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
  6. if (ImageWrapper->SetRaw(InColors.GetData(), InColors.Num()* sizeof(FColor), InWidth, InHeight, ERGBFormat::BGRA, 8))
  7. {
  8. int32 Quality = 100;//压缩质量0~100
  9. FFileHelper::SaveArrayToFile(ImageWrapper->GetCompressed(Quality), *ScreenShotName);
  10. }
  11. UGameViewportClient::OnScreenshotCaptured().Remove(GCaptureHandle);
  12. }

XXX.build.cs文件需要包含"ImageWrapper"模块

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ImageWrapper" });

头文件包含

  1. #include"IImageWrapperModule.h"
  2. #include"ModuleManager.h"
  3. #include"IImageWrapper.h"
  4. #include"ImageUtils.h"

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/91234
推荐阅读
相关标签
  

闽ICP备14008679号