2012-04-08 2 views
1

파일에서 직접 이미지를로드하는 방법을 찾았지만로드하는 이미지가 파란색 (원본은 초록색)입니다. 나는 그것이 나쁜 방식으로 저장되었다는 것을 알고 있었기 때문에 포토샵으로 저장했지만 아무것도 변경되지 않았습니다. 나는 내 프로그램이 나쁜 것 같아. 어떻게 변경하고 파일에서 이미지를로드하는 좋은 방법입니까? texture2d 방법에 비트 맵 :XNA 및 파일에서 이미지로드

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)); 

Drawning 질감 방법 :

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

답변

2

음, 어디 .... 내가 통해 이미지 파일을로드하는 것이 더 쉽습니다 생각 Texture2D.FromStream 메서드 ...

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

예, jpg, png 및 gif 이미지 만로드되지만 문제는 무엇입니까? ... 변환은 bmp입니다.