Skip to content

Instantly share code, notes, and snippets.

@kjlape
Created April 3, 2013 04:45
Show Gist options
  • Save kjlape/5298514 to your computer and use it in GitHub Desktop.
Save kjlape/5298514 to your computer and use it in GitHub Desktop.
Some example snippets from around the web related to low-level XNA Texture2D manipulation.
//To convert a Texture2D to an Image :
public static System.Drawing.Image Texture2Image(Texture2D texture)
{
if (texture == null)
{
return null;
}
if (texture.IsDisposed)
{
return null;
}
//Memory stream to store the bitmap data.
MemoryStream ms = new MemoryStream();
//Save the texture to the stream.
texture.SaveAsPng(ms, texture.Width, texture.Height);
//Seek the beginning of the stream.
ms.Seek(0, SeekOrigin.Begin);
//Create an image from a stream.
System.Drawing.Image bmp2 = System.Drawing.Bitmap.FromStream(ms);
//Close the stream, we nolonger need it.
ms.Close();
ms = null;
return bmp2;
}
//To convert an Image to a Texture2D:
public static void Image2Texture(System.Drawing.Image image,
GraphicsDevice graphics,
ref Texture2D texture)
{
if (image == null)
{
return;
}
if (texture == null || texture.IsDisposed ||
texture.Width != image.Width ||
texture.Height != image.Height ||
texture.Format != SurfaceFormat.Color)
{
if (texture != null && !texture.IsDisposed)
{
texture.Dispose();
}
texture = new Texture2D(graphics,image.Width,image.Height,false,SurfaceFormat.Color);
}
else
{
for (int i = 0; i < 16; i++)
{
if (graphics.Textures[i] == texture)
{
graphics.Textures[i] = null;
break;
}
}
}
//Memory stream to store the bitmap data.
MemoryStream ms = new MemoryStream();
//Save to that memory stream.
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//Go to the beginning of the memory stream.
ms.Seek(0, SeekOrigin.Begin);
//Fill the texture.
texture = Texture2D.FromStream(graphics, ms, image.Width, image.Height, false);
//Close the stream.
ms.Close();
ms = null;
}
private void RemovePlanetChunk(Vector2 RemovePosition)
{
// Create a render target, which we will draw to instead of the screen
RenderTarget2D target = new RenderTarget2D(graphics.GraphicsDevice, _Planet.Width, _Planet.Height);
// set the RenderTarget2D as the target for all future Draw calls untill we say otherwise
graphics.GraphicsDevice.SetRenderTarget(target);
// start our batch as usual..
_SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
// start with a transparent canvas
graphics.GraphicsDevice.Clear(Color.Transparent);
// add in the previously drawn dots from the current alpha map.
_SpriteBatch.Draw(_AlphaMask, new Vector2(0,0), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
// add a new dot to the map.
_SpriteBatch.Draw(_CuttingDot,
RemovePosition,
null,
Color.White,
0f,
new Vector2(_CuttingDot.Width / 2f, _CuttingDot.Height / 2f),
_HoleSize,
SpriteEffects.None,
1f);
// end the draw call
_SpriteBatch.End();
// start drawing to the screen again
graphics.GraphicsDevice.SetRenderTarget(null);
// set our Texture2D Alpha Mask to equal the current render target (the new mask).
// RenderTarget2D can be cast to a Texture2D without a problem
_AlphaMask = target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment