2012-04-22 44 views
0

我想創建一個應用程序,在其中創建一個位圖,然後從中取出一些變量並從中創建一個Texture2D。這是我有:如何訪問C#XNA中位圖的寬度和高度?

public Bitmap getBitmap() 
     { 
      if (!panelVideoPreview.IsDisposed) 
      { 
       Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height, PixelFormat.Format32bppRgb); 
       Graphics g = Graphics.FromImage(b); 
       Rectangle videoRect = panelVideoPreview.Bounds; 
       panelVideoPreview.DrawToBitmap(b, videoRect); 
       b.Dispose(); 
       return b; 
      } 
      else 
      { 
       Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height); 
       return b; 
      } 
     } 

然後我嘗試從它創建一個紋理:

 Texture2D tex = new Texture2D(gDevice, (int)bit.Width, (int)bit.Height); 

這是我的錯誤,我得到這個:

System.ArgumentException未處理 Message =參數無效。 D:\ Dropbox \ School \ Project中的GPUParticlesTexture.createVelocityMapBitmap(GraphicsDevice gDevice,位圖位,單精度)的System.Drawing.Image.get_Width() FUN \ Code \XNA \ GPUParticles \ GPUParticles \ GPUParticles \ VelocityTexture.cs:第16行 GPUParticles.Game1.camInterval_Tick的.cs:線302 在System.Windows.Forms.Timer.OnTick(EventArgs的) 在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback( IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的dwComponentID,的Int32原因,的Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) at System.Windows.Forms.Application.Run (Form mainForm) at Microsoft.Xna.Framework.WindowsGameHost.Run() at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run() 在GPUParticles.Program.Main(字串[] args)在d:\ Dropbox的\學校\項目FUN \代碼\ XNA \ GPUParticles \ GPUParticles \ GPUParticles \的Program.cs:行15 的InnerException:

+0

你不是從創建它的紋理,你只是試圖創建相同的寬度和高度的位圖的一個空的紋理。你需要在你的解決方案中使用位圖嗎?因爲如果你只需要用自定義數據創建紋理,我可以提出一個解決方案。 – neeKo 2012-04-22 23:35:54

回答

1

你可以使用MemoryStream。示例(未經測試):

Texture2D MakeTextureFromBitmap(Bitmap bmp) { 
    using (var ms = new MemoryStream()) { 
     bmp.Save(ms, ImageFormat.Png); 
     return Texture2D.FromStream(GraphicsDevice, ms); 
    } 
} 
+0

該流應該重繞嗎?只是想一想 – neeKo 2012-04-23 08:05:35

+0

謝謝你,但我不想創建一個只有一些數據的新的Texture2D,我想創建一個攝像頭應用程序,它將快照轉換成一個全新的Texture2D,只是區別黑色和白色兩種顏色可見。 – tversteeg 2012-04-23 14:29:53

+0

@ThomasVersteeg然後在調用此方法之前根據需要更改您的位圖。請參閱MSDN上的示例:http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx – Asik 2012-04-23 16:37:13