Home > 计算机与 Internet > RenderTarget2D.GetTexture() throws exception

RenderTarget2D.GetTexture() throws exception

We know that when a RenderTarget is set to device, the GetTexture() method can not be invoked or an InvalidOperationException arguing
"The render target must not be set on the device when calling GetTexture" will be thrown. But how if a newly constructed RenderTarget like this?
 
RenderTarget2D target = new RenderTarget2D( /*some params*/);
target.GetTextrure();
 
Yes, the answer is that, the very exception is thrown. Even if we add a line GraphicsDevice.SetRenderTarget(0, null /* or some other RT*/) just above the line of GetTexture(). After some painful googling, the solution was found: WE MUST SET A RENDERTARGET TO DEVICE FIRST, THEN SET IT OFF, THEN GETTEXTURE(), that is to say, a RenderTarget must have been a really "render target" before GetTexture(). This is reasonable, because the content of texture is meaningless if nothing was rendered on to the target. But what a fuc|< exception it throws!
This site is helpful with the problem : http://www.ziggyware.com/forum/viewthread.php?forum_id=12&thread_id=13568 , where Driklyn wrote " You never set the render target to LargeBackground so it gives the error. Definitely a misleading error message however. It should say something more along the lines of ‘texture is undefined.’ ". Also some other sites found to be related with the issue, but hardly to the point. We list them below:
 
Advertisements
Categories: 计算机与 Internet
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: