UE4截屏

该函数描述了一个在UnrealEngine中实现的屏幕截图功能,可选择性地使用CutBox裁剪区域,否则全屏截图,并将结果压缩保存为PNG。
摘要由CSDN通过智能技术生成

 如果指定了 CutBox,就截取屏幕上一小块,如果没有指定CutBox,就截取整屏。

// 截屏。
bool ScreenShots(UWorld* pWorld, const FBox2D* CutBox/*= nullptr*/)
{
	if (pWorld == NULL)
	{
		return false;
	}

	UGameViewportClient* pViewport = Cast<UGameViewportClient>(
		pWorld ? pWorld->GetGameViewport() : GEngine->GameViewport);
	if (pViewport == NULL)
	{
		return false;
	}

	FVector2D vSize = FVector2D::ZeroVector;
	pViewport->GetViewportSize(vSize);

	TArray<FColor> Bitmap;
	bool bScreenshotSuccessful = false;
	TSharedPtr<SWindow> WindowPtr = pViewport->GetWindow();
	if (FSlateApplication::IsInitialized())
	{
		TSharedRef<SWidget> WindowRef = WindowPtr.ToSharedRef();
		FIntVector IntVector = FIntVector(vSize.X, vSize.Y, 0);
		bScreenshotSuccessful = FSlateApplication::Get().TakeScreenshot(
			WindowRef, Bitmap, IntVector);
	}
	for (FColor& Color : Bitmap)
	{
		Color.A = 255;
	}

	TArray<FColor>* RealMap = &Bitmap;
	bool bNeedCut = (CutBox != NULL && CutBox->bIsValid);
	int32 nSizeX = vSize.X, nSizeY = vSize.Y;
	TArray<FColor> CutBitmap, CutBitmap2;
	if (bNeedCut)
	{
		nSizeX = CutBox->GetSize().X;
		nSizeY = CutBox->GetSize().Y;
		RealMap = &CutBitmap;
		for (int32 i = CutBox->Min.Y; i < CutBox->Max.Y; i++)
		{
			for (int32 j = CutBox->Min.X; j < CutBox->Max.X; j++)
			{
				CutBitmap.Add(Bitmap[i * vSize.X + j]);
			}
		}
	}

	FString sFileName = FPaths::ProjectSavedDir();
	sFileName.Append(TEXT("/TempImage.png"));
	sFileName.ReplaceInline(TEXT("\\"), TEXT("/"));
	sFileName.ReplaceInline(TEXT("//"), TEXT("/"));
	TArray<uint8> CompressedBitmap;
	FImageUtils::CompressImageArray(nSizeX, nSizeY, *RealMap, CompressedBitmap);
	if (!CompressedBitmap.IsEmpty())
	{
		FFileHelper::SaveArrayToFile(CompressedBitmap, *sFileName);
	}

	return true;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码实现在UE4中截取屏幕并保存为图片: ```cpp UTexture2D* UScreenshotFunctionLibrary::TakeScreenshot(UObject* WorldContextObject, int32 Width, int32 Height) { // Get the player controller APlayerController* PlayerController = UGameplayStatics::GetPlayerController(WorldContextObject, 0); if (!PlayerController) { return nullptr; } // Take the screenshot FViewport* Viewport = GEngine->GameViewport->Viewport; TArray<FColor> Bitmap; bool bScreenshotSuccessful = Viewport->ReadPixels(Bitmap, FReadSurfaceDataFlags(), FIntRect(0, 0, Width, Height)); if (!bScreenshotSuccessful) { return nullptr; } // Create the texture UTexture2D* Texture = UTexture2D::CreateTransient(Width, Height, PF_B8G8R8A8); Texture->AddToRoot(); // Make sure the texture isn't garbage collected Texture->UpdateResource(); // Copy the bitmap data to the texture uint8* MipData = static_cast<uint8*>(Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE)); for (int32 Y = 0; Y < Height; Y++) { for (int32 X = 0; X < Width; X++) { int32 Index = Y * Width + X; MipData[Index * 4 + 0] = Bitmap[Index].B; MipData[Index * 4 + 1] = Bitmap[Index].G; MipData[Index * 4 + 2] = Bitmap[Index].R; MipData[Index * 4 + 3] = Bitmap[Index].A; } } Texture->PlatformData->Mips[0].BulkData.Unlock(); // Save the texture to a file FString Filename = FPaths::ProjectDir() / TEXT("Screenshots") / FDateTime::Now().ToString(TEXT("yyyy-MM-dd_HH-mm-ss")) + TEXT(".png"); FFileHelper::SaveObjectAsPNG(Texture, *Filename); return Texture; } ``` 此代码通过读取屏幕像素并将其复制到一个临时纹理中,最后将纹理保存为 PNG 文件。你可以调用此函数来截取当前屏幕并保存为文件。 注意:你需要在项目设置中启用 "With Editor" 选项,以便在运行时允许截屏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值