0

我正在尝试创建一个应用程序,在其中创建一个位图,然后从中取出一些变量并从中制作一个 Texture2D。这就是我所拥有的:

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=Parameter 无效。Source=System.Drawing StackTrace: 在 System.Drawing.Image.get_Width() at GPUParticles.VelocityTexture.createVelocityMapBitmap(GraphicsDevice gDevice, Bitmap bit, Single Accuracy) in D:\Dropbox\School\Project FUN\Code\XNA\GPUParticles\ GPUParticles\GPUParticles\VelocityTexture.cs:D:\Dropbox\School\Project FUN\Code\XNA\GPUParticles\GPUParticles\GPUParticles\Game1.cs 中 GPUParticles.Game1.camInterval_Tick(Object myObject, EventArgs myEventArgs) 的第 16 行:第 302 行在 System.Windows.Forms.Timer.OnTick(EventArgs e) 在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在系统。

4

1 回答 1

1

您可以使用 MemoryStream。示例(未经测试):

Texture2D MakeTextureFromBitmap(Bitmap bmp) {
    using (var ms = new MemoryStream()) {
        bmp.Save(ms, ImageFormat.Png);
        return Texture2D.FromStream(GraphicsDevice, ms);
    }
}
于 2012-04-23T04:33:39.850 回答