# How to read pixels form a UTexture?

**URL:** <https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [May 21, 2022, 9:42pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009 "2022-05-21T21:42:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![somawheels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@somawheels](https://forums.unrealengine.com/u/somawheels)\
**Post date:** [May 21, 2022, 9:42pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/1 "2022-05-21T21:42:56Z")

</div>

My question is as the title says. I have seen examples of how to read pixels from a Utexture2D,  
but cant find a way to get the pixel data from a UTexture.

PS. The reason i want to do this, is because i want to convert a UTexture to UTexture2D.

---

<div class="post-metadata">

**Author:** ![RichmarIII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RichmarIII](https://forums.unrealengine.com/u/RichmarIII)\
**Post date:** [May 21, 2022, 10:53pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/2 "2022-05-21T22:53:07Z")

</div>

Well, _ **UTexture** _ is an abstract class that cannot be instantiated. It relies on it’s concrete subclasses _ **UTexture2D** _, _ **UTextureCube** _, _ **UTexture2DArray** _, etc to be instantiated. If you have a pointer to a _ **UTexture** _, then it actually _ **IS** _ a _ **UTexture2D** _, or _ **UTextureCube** _, or _ **UTexture2DArray** _ etc.

So what you can do i believe, is simply cast it like so.

```cpp
if(const auto Texture2D = Cast<UTexture2D>(Texture))
{
    //If This block is reached, the texture was a Texture2D. Lets do our work with it

	// Make Sure The Texture Is Up To Date.
	Texture2D->UpdateResource();

	// Get Platform Data.
	const auto PlatformData = Texture2D->GetPlatformData();
	
	// Get The Bulk Data
	const auto TextureData = PlatformData->Mips[0].BulkData;

	// Array Pixel Data Will Be Copied To
	TArray<FColor> DestPixels{};

	// Initialize The Array With The Correct Pixel Size;
	DestPixels.AddDefaulted(PlatformData->SizeX * PlatformData->SizeY);

	// Lock The Pixel Data For CPU Read Access.
	const auto PixelData = static_cast<const FColor*>(TextureData.LockReadOnly());

	// Copy The Pixel Data.
	FMemory::Memcpy(DestPixels.GetData(), &PixelData, sizeof(FColor) * DestPixels.Num());

	// Unlock So The GPU Can Access The Data Again.
	TextureData.Unlock();

	// We Now Have All Pixel Data For The Texture And We Can Do What We Want Without Holding The GPU Up.
	DestPixels
}

```

---

<div class="post-metadata">

**Author:** ![somawheels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@somawheels](https://forums.unrealengine.com/u/somawheels)\
**Post date:** [May 21, 2022, 11:08pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/3 "2022-05-21T23:08:21Z")

</div>

oh wow, thanks! I didnt know i could simply cast it.

---

<div class="post-metadata">

**Author:** ![RichmarIII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RichmarIII](https://forums.unrealengine.com/u/RichmarIII)\
**Post date:** [May 21, 2022, 11:10pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/4 "2022-05-21T23:10:21Z")

</div>

Yeah, UTexture is just an abstract base class of all UTextureXX classes

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b1fd2ba6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b1fd2ba6](https://forums.unrealengine.com/u/anonymous_user_b1fd2ba6)\
**Post date:** [October 13, 2022, 5:29pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/5 "2022-10-13T17:29:43Z")

</div>

I have a similar issue, and your solution makes sense, but for some reason, after unlocking the texture data, when trying to access the DestPixels I get the following error:  
Unhandled Exception: EXCEPTION\_ACCESS\_VIOLATION reading address 0x000000bcbb580000  
to my knowledge, the Memcpy creates a copy of the data, so why would it be affected by the unlocking of the GPU?

---

<div class="post-metadata">

**Author:** ![RichmarIII](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RichmarIII](https://forums.unrealengine.com/u/RichmarIII)\
**Post date:** [October 12, 2023, 6:25pm UTC](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009/6 "2023-10-12T18:25:17Z")

</div>

it’s possible you are calculating the memcpy addresses wrong and are actually copying protected memory, i would give it another hard look.
