XNA中文显示学习

学习XNA一段时间了,但是中文还是不能显示,感觉上XNA就象个婴儿一样脆弱,然后就仔细的去找了一遍关于中文显示的范例,开始说的是即时编译的方法,就是用xml编译需要的字库,然后显示,费时费力,看来没多少选择,所以我也没看这种方法,偶然翻看论坛,发现一个帖子,是中文显示类,我一开始认为可能和那个方法差不多,其实自己也在想用点阵这种低效率方法了,一看好像没进行什么编译,采用的是把字写到图片缓冲上,我一下就意识到这种方法才是真正好的解决方案,但是复制代码运行中途遇到了一个问题,提示:

The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)

这可把我难住了,我以前一直没用C#,也没用过2005 C#,不知道出了什么错,后来到国外网站上查,仔细一看上面说是没装DX SDK,但是我装了的啊。。。只好先放弃,然后我又下载一个精灵生成器的C#代码,仔细一看,ReRefences文件夹下怎么有个System.Drawing,我照着给刚才中文显示的那个项目添加了,一运行成了,心中真是十分愉快啊!!

代码如下

//StringFormatImg类

using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace Angel.XNA.StringFormatImg
{
    public class StringFormatImg
    {
        private System.Drawing.Bitmap GetTmpBitMap;
        private System.Drawing.Font GetFont;
        private System.Drawing.Graphics GetGraphics;
        private System.Drawing.SizeF GetSizeF;
        private System.Drawing.Bitmap GetBmp;
        public StringFormatImg(string Text,string Fonts,float FontsSize,Color FontsColor)
        {
            GetTmpBitMap = new System.Drawing.Bitmap(1, 1);
            GetFont = new System.Drawing.Font(Fonts, FontsSize);
            GetGraphics = System.Drawing.Graphics.FromImage(GetTmpBitMap);
            GetSizeF = GetGraphics.MeasureString(Text, GetFont);
            GetBmp = new System.Drawing.Bitmap((int)GetSizeF.Width,(int)GetSizeF.Height);
            GetGraphics = System.Drawing.Graphics.FromImage(GetBmp);
            GetGraphics.DrawString(Text, GetFont, new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(FontsColor.A, FontsColor.R, FontsColor.G, FontsColor.B)), new System.Drawing.PointF());
            GetGraphics.Dispose();
        }

        public Stream Out2D()
        {
            MemoryStream GetStream = new MemoryStream();
            GetBmp.Save(GetStream, System.Drawing.Imaging.ImageFormat.Png);
            GetStream.Seek(0, SeekOrigin.Begin);
            Stream outstream = GetStream;
            return outstream;
        }

    }
}


//使用代码

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Angel.XNA.StringFormatImg;
namespace MousePosition
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch ForegroundBatch;
        private Texture2D SpriteTexture;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            graphics.ToggleFullScreen();
            ForegroundBatch = new SpriteBatch(graphics.GraphicsDevice);
            StringFormatImg outitext = new StringFormatImg("我爱世界", "宋体",9,Color.Pink);
            SpriteTexture = Texture2D.FromFile(graphics.GraphicsDevice, outitext.Out2D());
            base.Initialize();
            // Set this to true to make the mouse cursor visible.
            // Use the default (false) if you are drawing your own
            // cursor or don't want a cursor.
            this.IsMouseVisible = true;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
         
         

        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
          
            base.Update(gameTime);
        }


 
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            ForegroundBatch.Begin();
            Vector2 pos = new Vector2(100, 100);
            ForegroundBatch.Draw(SpriteTexture, pos, Color.White);
            ForegroundBatch.End();
            base.Draw(gameTime);
        }
    }
}

  评论这张
转发至微博
转发至微博
0   分享到:         
阅读(1010) | 评论(0) | 转载 (0) | 举报
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值