1

ファイルから直接画像をロードする方法を見つけましたが、ロードする画像は青です(元の画像は緑です)。保存方法が悪いと思ったので、フォトショップで保存しましたが、何も変わりませんでした。私のプログラムはうまくいかないと思います。どうすれば変更できますか?ファイルから画像をロードするのに良い方法ですか?texture2dメソッドへのビットマップ:

    public static Texture2D GetTexture2DFromBitmap(GraphicsDevice device, Bitmap bitmap)
    {
        Texture2D tex = new Texture2D(device, bitmap.Width, bitmap.Height);
        System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
        int bufferSize = data.Height * data.Stride;
        byte[] bytes = new byte[bufferSize];
        System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
        tex.SetData(bytes);
        bitmap.UnlockBits(data);
        return tex;
    }

画像行の読み込み:

        backgroundTexture = Tools.GetTexture2DFromBitmap(device, (System.Drawing.Bitmap)System.Drawing.Image.FromFile(@"1.bmp", false));

テクスチャの描画方法:

        spriteBatch.Begin();
        Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
        spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
        spriteBatch.End();
4

1 に答える 1

2

うーん....Texture2D.FromStreamメソッドを使用して画像ファイルをロードする方が簡単だと思います...

texture = Texture2D.FromStream(Device、File.OpenRead(path));

はい、それはjpg、png、gif画像のみをロードしますが、それはどうですか?...bmpの変換は簡単です。

于 2012-04-08T23:43:49.157 に答える