• Advertisement
Sign in to follow this  

Texture can't be locked, so I can't convert to bitmap

This topic is 2502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's what I'm trying to accomplish:
I got 2 textures, and I want to check the pixels to see the differences, and create a new texture out of that.

The problem:
The first problem is that I'm using C# (not my decision)
Those textures have been used as render targets, and for that, when they're created, usage is rendertarget, and pool is default.
Then when I try to make the textures into bitmaps, I simply can't lockrectangle on them because you can only do that if they have been created with usage.dynamic
but then with usage.dynamic, I can't use them as render targets!

Texture one = render_difference.texture_;
Texture two = render_target_.texture_;

Bitmap bitmap1 = new Bitmap((int)render_difference_.Width, (int)render_difference_.Height);
Bitmap bitmap2 = new Bitmap((int)render_target_.Width, (int)render_target_.Height);
bitmap1 = TextureToBitmap(ref one, new Rectangle(0, 0, (int)render_difference.Width, (int)render_difference.Height), bitmap1.Size, new Point(0, 0));

and then texturetobitmap is where the texture gets locked, and gives an error.

public Bitmap TextureToBitmap(ref Texture tex, Rectangle texRect, Size bmpSize, Point bmpPoint)
Rectangle bmpRect = new Rectangle(bmpPoint, new Size(texRect.Width, texRect.Height));

Bitmap bmp = new Bitmap(bmpSize.Width, bmpSize.Height);
//Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);

BitmapData data = bmp.LockBits(bmpRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

SurfaceDescription sd = tex.GetLevelDescription(0);
int texSize = sd.Width;

int texX = texRect.X;
int texY = texRect.Y;
int texWidth = texRect.Width;
int texHeight = texRect.Height;

IntPtr p = data.Scan0;

int* bp = (int*)p.ToPointer();

GraphicsStream s = tex.LockRectangle(0, texRect, LockFlags.ReadOnly);

int* gsp = (int*)s.InternalDataPointer;

for (int i = 0; i < texWidth; i++)
for (int j = 0; j < texHeight; j++)
*(bp + texWidth * j + i) = *(gsp + texSize * j + i);


return bmp;

I read that I could use device.UpdateSurface, but I don't know how... since what I got arent surfaces but textures...
- Is there a way to change the usage of a texture after it's been created?
- Is there a way to make a new texture using the old texture, and change the usage?
- Is there another way to make a bitmap out of a texture that doesn't involve locking it?
- Is there any other way to accomplish what I want?

I'm a noob and I dont get surfaces/textures/bitmaps too much yet... sorry ;_;

Share this post

Link to post
Share on other sites
In the end I could just copy the surface to another surface, and this new one was set with usage.dynamic.
And I found out that surfaces are just the layer 0 of a texture. BUT THEN trying to lock a file to go through it and make a copy apparently takes waaaaay too long.
So in the end I solved it by changing the blending operation before drawing the second picture

device.RenderState.BlendOperation = BlendOperation.Subtract;
device.RenderState.SourceBlend = Microsoft.DirectX.Direct3D.Blend.SourceColor;
device.RenderState.DestinationBlend = Microsoft.DirectX.Direct3D.Blend.DestinationColor;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement