1、TArray<FColor>或是FColor*可通过ImageWrapper直接保存成图片
1 void URenderCineCameraLibrary::SaveColorToFile(FColor* SourceColor, uint32 Width, uint32 Height) 2 { 3 check(SourceColor != nullptr); 4 5 /*// FColor* SurfaceColor ...png 6 for (FColor& Color : SurfaceColor) 7 { 8 Color.A = 255; 9 } 10 11 TArray<uint8> TextureData; 12 TextureData.AddUninitialized(Width * Height * sizeof(FColor)); 13 FMemory::Memcpy(TextureData.GetData(), (uint8*)SourceColor, Width * Height * sizeof(FColor)); 14 if (TextureData.Num() <= 0) 15 { 16 UE_LOG(LogTemp, Error, TEXT("TextureData is empty ...")); 17 return; 18 }*/ 19 20 // save png 21 IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")); 22 TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG); 23 ImageWrapper->SetRaw(SourceColor, Width * Height * sizeof(FColor), Width, Height, ERGBFormat::BGRA, 8); 24 25 26 const TArray64<uint8>& PNGData = ImageWrapper->GetCompressed(100); 27 28 FString FileName = TEXT("D:\\CineCapture1.jpg"); 29 FFileHelper::SaveArrayToFile(PNGData, *FileName); 30 ImageWrapper.Reset(); 31 }
2、将UTexture2D保存成图片
1 void URenderCineCameraLibrary::SaveTextureToFile(UTexture2D* SourceTex) 2 { 3 check(SourceTex); 4 int32 Width = SourceTex->GetSizeX(); 5 int32 Height = SourceTex->GetSizeY(); 6 7 FTexture2DMipMap& MipMap = SourceTex->GetPlatformData()->Mips[0]; 8 void* Data = MipMap.BulkData.Lock(LOCK_READ_ONLY); 9 10 TArray<uint8> TextureData; 11 TextureData.AddUninitialized(Width * Height * sizeof(FColor)); 12 FMemory::Memcpy(TextureData.GetData(), (uint8*)Data, Width * Height * sizeof(FColor)); 13 if (TextureData.Num() <= 0) 14 { 15 UE_LOG(LogTemp, Error, TEXT("TextureData is empty ...")); 16 return; 17 } 18 19 // save png 20 IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")); 21 TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG); 22 ImageWrapper->SetRaw(TextureData.GetData(), Width * Height * sizeof(FColor), Width, Height, ERGBFormat::BGRA, 8); 23 24 25 const TArray64<uint8>& PNGData = ImageWrapper->GetCompressed(100); 26 27 FString FileName = TEXT("D:\\CineTexture.jpg"); 28 FFileHelper::SaveArrayToFile(PNGData, *FileName); 29 ImageWrapper.Reset(); 30 31 32 MipMap.BulkData.Unlock(); 33 }
标签:FColor,ImageWrapper,TextureData,保存,Height,Width,UE4,sizeof From: https://www.cnblogs.com/-sev-/p/16931689.html