c++截取屏幕图片并保存(函数代码实现)

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <strong>  //获取桌面窗口的CDC  
  2.     CDC *pdeskdc = GetDesktopWindow()->GetDC();    
  3.     CRect re;  
  4.     //获取窗口的大小  
  5.     GetDesktopWindow()->GetClientRect(&re);  
  6.     CBitmap bmp;  
  7.     bmp.CreateCompatibleBitmap(pdeskdc , re.Width() , re.Height());  
  8.     //创建一个兼容的内存画板  
  9.     CDC memorydc;  
  10.     memorydc.CreateCompatibleDC(pdeskdc);  
  11.   
  12.     //选中画笔  
  13.     CBitmap *pold = memorydc.SelectObject(&bmp);  
  14.   
  15.     //绘制图像  
  16.     memorydc.BitBlt(0,0,re.Width() ,re.Height(), pdeskdc , 0 ,0 ,SRCCOPY) ;  
  17.   
  18.     //获取鼠标位置,然后添加鼠标图像  
  19.     CPoint po;  
  20.     GetCursorPos(&po);  
  21.     HICON hinco = (HICON)GetCursor();  
  22.     memorydc.DrawIcon(po.x-10 , po.y - 10 , hinco);  
  23.     //选中原来的画笔  
  24.     memorydc.SelectObject(pold);  
  25.     BITMAP bit;  
  26.     bmp.GetBitmap(&bit);  
  27. //  DWORD size = bit.bmWidth * bit.bmHeight ;  
  28.   
  29.     //定义 图像大小(单位:byte)  
  30.     DWORD size = bit.bmWidthBytes * bit.bmHeight ;  
  31.     LPSTR lpdata = (LPSTR)GlobalAlloc(GPTR , size) ;  
  32.   
  33.     //后面是创建一个bmp文件的必须文件头,想要了解可以参考msdn  
  34.       
  35.     BITMAPINFOHEADER pbitinfo;  
  36.     pbitinfo.biBitCount = 24 ;   
  37.     pbitinfo.biClrImportant = 0;  
  38.     pbitinfo.biCompression = BI_RGB ;  
  39.     pbitinfo.biHeight = bit.bmHeight ;   
  40.     pbitinfo.biPlanes = 1 ;  
  41.     pbitinfo.biSize = sizeof(BITMAPINFOHEADER);  
  42.     pbitinfo.biSizeImage =size;  
  43.     pbitinfo.biWidth = bit.bmWidth;  
  44.     pbitinfo.biXPelsPerMeter = 0;  
  45.     pbitinfo.biYPelsPerMeter = 0 ;  
  46.   
  47.     GetDIBits(pdeskdc->m_hDC , bmp , 0 , pbitinfo.biHeight , lpdata ,   
  48.         (BITMAPINFO*)&pbitinfo,DIB_RGB_COLORS);  
  49.   
  50.     BITMAPFILEHEADER bfh;  
  51.     bfh.bfReserved1 = bfh.bfReserved2 = 0 ;  
  52.     bfh.bfType = ((WORD)('M'<< 8)|'B');  
  53.     bfh.bfSize = 54 + size ;   
  54.     bfh.bfOffBits = 54 ;  
  55.       
  56.     //写入文件  
  57.   
  58.     CFile file;  
  59.     if ( file.Open("1.bmp" , CFile::modeCreate|CFile::modeWrite) )  
  60.     {  
  61.         file.WriteHuge( &bfh , sizeof(BITMAPFILEHEADER) );  
  62.         file.WriteHuge(&pbitinfo , sizeof(BITMAPINFOHEADER));  
  63.         file.WriteHuge(lpdata , size);  
  64.         file.Close();  
  65.     }  
  66.     GlobalFree(lpdata);</strong>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在UE4中,你可以使用C++代码实现截取场景中的图片保存到本地。具体步骤如下: 1. 创建一个C++类,并继承自AActor类。命名为"ScreenshotActor"。 2. 在"ScreenshotActor.h"文件中添加以下头文件: ```cpp #include "Engine/Engine.h" #include "Kismet/GameplayStatics.h" #include "Engine/GameViewportClient.h" #include "Misc/FileHelper.h" #include "Misc/Paths.h" ``` 3. 在"ScreenshotActor.h"文件中添加以下声明: ```cpp UCLASS() class YOURPROJECT_API AScreenshotActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AScreenshotActor(); // Take screenshot and save to file UFUNCTION(BlueprintCallable, Category = "Screenshot") void TakeScreenshot(); private: // The resolution multiplier for the screenshot UPROPERTY(EditAnywhere, Category = "Screenshot") int32 ResolutionMultiplier = 1; }; ``` 4. 在"ScreenshotActor.cpp"文件中添加以下代码: ```cpp #include "ScreenshotActor.h" AScreenshotActor::AScreenshotActor() { PrimaryActorTick.bCanEverTick = false; } void AScreenshotActor::TakeScreenshot() { // Get the game viewport client UGameViewportClient* ViewportClient = GEngine->GameViewport; if (ViewportClient) { // Get the viewport size FVector2D Size; ViewportClient->GetViewportSize(Size); // Calculate the screenshot resolution int32 ResolutionX = Size.X * ResolutionMultiplier; int32 ResolutionY = Size.Y * ResolutionMultiplier; // Take the screenshot TArray<FColor> Bitmap; Bitmap.AddUninitialized(ResolutionX * ResolutionY); ViewportClient->ReadPixels(Bitmap); // Create the screenshot texture UTexture2D* Screenshot = UTexture2D::CreateTransient(ResolutionX, ResolutionY); Screenshot->CompressionSettings = TC_HDR; Screenshot->SRGB = false; Screenshot->AddToRoot(); Screenshot->UpdateResource(); // Copy the pixel data to the texture uint8* MipData = static_cast<uint8*>(Screenshot->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE)); FMemory::Memcpy(MipData, Bitmap.GetData(), Bitmap.Num() * sizeof(FColor)); Screenshot->PlatformData->Mips[0].BulkData.Unlock(); // Save the screenshot to file FDateTime Now = FDateTime::Now(); FString Filename = FString::Printf(TEXT("Screenshot_%04d%02d%02d_%02d%02d%02d.png"), Now.GetYear(), Now.GetMonth(), Now.GetDay(), Now.GetHour(), Now.GetMinute(), Now.GetSecond()); FString SaveDirectory = FPaths::GameSavedDir() + TEXT("Screenshots/"); if (!FPaths::DirectoryExists(SaveDirectory)) { FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*SaveDirectory); } FString FilePath = SaveDirectory + Filename; FFileHelper::SaveArrayToFile(Screenshot->GetRawImageData(), Screenshot->GetAllocatedSize(), *FilePath); // Remove the screenshot texture Screenshot->RemoveFromRoot(); } } ``` 5. 在UE4编辑器中创建一个蓝图,并选择"ScreenshotActor"作为其父类。 6. 在蓝图中添加一个"Take Screenshot"函数,并调用AScreenshotActor::TakeScreenshot()函数。 7. 在场景中放置该蓝图,并运行游戏。当需要截取场景中的图片时,调用蓝图中的"Take Screenshot"函数即可。 注意:在截图之前需要确保场景中的所有元素都已经加载完毕,否则可能会导致截图不完整或者出现其他问题。另外,该实现只适用于PC平台,如果需要支持其他平台,请参考UE4官方文档或者相关论坛。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值