赞
踩
为方便起见,都是用全局函数和变量,而不是在类中实现
代码如下:
- FDelegateHandle GCaptureHandle;
- static void OnCaptureScreenshotComplete(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
- {
- FString ScreenShotName = FPaths::GameSavedDir() + TEXT("ScreenShot.png");
- TArray<uint8> CompressedBitmap;
- FImageUtils::CompressImageArray(InWidth, InHeight, InColors, CompressedBitmap);
- FFileHelper::SaveArrayToFile(CompressedBitmap, *ScreenShotName);
- UGameViewportClient::OnScreenshotCaptured().Remove(GCaptureHandle);
- }
-
- void CaptureScreenshot()
- {
- if (GEngine && GEngine->GameViewport)
- {
- GCaptureHandle = UGameViewportClient::OnScreenshotCaptured().AddStatic(&OnCaptureScreenshotComplete);
- GEngine->GameViewport->Exec(nullptr, TEXT("HighResShot 1080x720"), *GLog);//分辨率可自己设定
- }
- }
图片保存为png格式,如果想保存为jpg格式,可改为如下代码:
-
- static void OnCaptureScreenshotComplete(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
- {
- FString ScreenShotName = FPaths::GameSavedDir() + TEXT("ScreenShot.jpg");
- IImageWrapperModule& ImageWrapperModule = FModuleManager::Get().LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
- IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
- if (ImageWrapper->SetRaw(InColors.GetData(), InColors.Num()* sizeof(FColor), InWidth, InHeight, ERGBFormat::BGRA, 8))
- {
- int32 Quality = 100;//压缩质量0~100
- FFileHelper::SaveArrayToFile(ImageWrapper->GetCompressed(Quality), *ScreenShotName);
- }
- UGameViewportClient::OnScreenshotCaptured().Remove(GCaptureHandle);
- }
XXX.build.cs文件需要包含"ImageWrapper"模块
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ImageWrapper" });
头文件包含
- #include"IImageWrapperModule.h"
- #include"ModuleManager.h"
- #include"IImageWrapper.h"
- #include"ImageUtils.h"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。