How to access Surface data?
here is my code
Surface surface = graphics.GraphicsDevice.GetBackBuffer(0, 0);
uint[] data = new uint[4];
surface.GetData<uint>(data,0,4);
"The size of the data passed in is too large or too small for this resource" exception will throw out when executeGetData function.
How draw a picture on an surface except to use GetData SetData function?Is there a way to use GDI+ function to draw something on XNA surface,or copy data from System.Drawing.Image?
I want to write some code to draw chinese string on screen.If GDI+ function is available in XNA,that will easy to complete.
[753 byte] By [
ledwinka] at [2007-12-25]
No GDI+ for XNA on the XBox 360.
It’s accessible on the PC but you should never ever try to manipulate the back buffer this way.
The Font class should be able to handle Chinese string as long as you have the necessary fonts on your system. If you only need a limited number of symbols you can store them in a texture and use sprites.
Font class?I can't find any font class in the beta version.Do you mean there is a font class in the furture final version ?
If so ,I don't want to waste my time to write my own font class!
Sorry my fault. Still having trouble to separate MDX2 and XNA.
There was a Font class in MDX 2 but it didn’t make the cut. Maybe it will reintroduce but as it uses the GDI font system as base I am not sure.
The “old” Font system was based in the sprite system.
I believe there is a font class in the SpaceWars starter kit.
Yes , it's a tiny font class there.But I need a font class to display chinese charater.You know there are more than 1000 chinese character for daily use.A-Z is not enough.
To get data (for example text) from a System.Drawing.Image, you can do something like the following (but probly not recommended):
System.Drawing.Bitmap _BITMAP = new System.Drawing.Bitmap(64, 64);System.Drawing.Graphics _GRAPHICS = System.Drawing.Graphics.FromImage(_BITMAP);System.Drawing.Font _FONT = new System.Drawing.Font("Arial", 10.0f);_GRAPHICS.DrawString("text", _FONT, System.Drawing.Brushes.Black, new System.Drawing.PointF(0.0f, 0.0f));System.IO.MemoryStream _STREAM = new System.IO.MemoryStream();_BITMAP.Save(_STREAM, System.Drawing.Imaging.ImageFormat.Png);Texture2D _TEXTURE = Texture2D.FromFile(_GraphicsComponent.GraphicsDevice, _STREAM);To draw to the back buffer, I think that's what the SpriteBatch does:
SpriteBatch _SPRITEBATCH = new SpriteBatch(_GraphicsComponent.GraphicsDevice);
_SPRITEBATCH.Begin(SpriteBlendMode.AlphaBlend);
_SPRITEBATCH.Draw(_TEXTURE, new Vector2(0, 0), Color.White);
_SPRITEBATCH.End();
(Haven't tested the above code, but it should all work with some fine-tuning.)
Here's the code for a working sample of this method of drawing text.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace texttest
{
/// <summary>
/// This is the main type for your game
/// </summary>
partial class Game1 : Microsoft.Xna.Framework.Game
{
public Game1()
{
InitializeComponent();
System.Drawing.Font _FONT = new System.Drawing.Font("Arial", 24.0f);
System.Drawing.Graphics _GRAPHICS = System.Drawing.Graphics.FromHwnd(Window.Handle);
System.Drawing.SizeF _SIZE = _GRAPHICS.MeasureString("Text to Image to Texture2D test", _FONT);
_GRAPHICS.Dispose();
System.Drawing.Bitmap _BITMAP = new System.Drawing.Bitmap((int)_SIZE.Width, (int)_SIZE.Height);
_GRAPHICS = System.Drawing.Graphics.FromImage(_BITMAP);
_GRAPHICS.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
_GRAPHICS.DrawString("Text to Image to Texture2D test", _FONT, System.Drawing.Brushes.White, new System.Drawing.PointF(0, 0));
_GRAPHICS.Dispose();
System.IO.MemoryStream _STREAM = new System.IO.MemoryStream();
_BITMAP.Save(_STREAM, System.Drawing.Imaging.ImageFormat.Png);
_BITMAP.Dispose();
_STREAM.Position = 0;
_Texture = (Texture2D)Texture2D.FromFile(graphics.GraphicsDevice, _STREAM);
_STREAM.Dispose();
_SpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
_Position = new Vector2(10, 10);
}
private Texture2D _Texture;
private SpriteBatch _SpriteBatch;
private Vector2 _Position;
protected override void Update()
{
// The time since Update was called last
float elapsed = (float)ElapsedTime.TotalSeconds;
// TODO: Add your game logic here
// Let the GameComponents update
UpdateComponents();
}
protected override void Draw()
{
// Make sure we have a valid device
if (!graphics.EnsureDevice())
return;
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.BeginScene();
// TODO: Add your drawing code here
_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
_SpriteBatch.Draw(_Texture, _Position, Color.Black);
_SpriteBatch.End();
// Let the GameComponents draw
DrawComponents();
graphics.GraphicsDevice.EndScene();
graphics.GraphicsDevice.Present();
}
}
}
And a screenshot here.
I'm getting the same error, but I'm not trying to access the back buffer, I'm just trying to access a texture I allocated myself so I can build the texture procedurally on the CPU. Guess I'll do it on the GPU instead, but if there's away to do it with the CPU I'd like to know - I only need to do it once. Do some combination of render settings, resource usage, whatever, make it possible?