如何在 C# XNA 中访问位图的宽度和高度
本文关键字:高度 位图 访问 XNA | 更新日期: 2023-09-27 18:31:53
我正在尝试创建一个应用程序,在其中创建一个位图,然后从中取出一些变量并从中制作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 未处理 消息=参数无效。 源=系统.绘图 堆栈跟踪: 在 System.Drawing.Image.get_Width() at GPUParticles.VelocityTexture.createVelocityMapBitmap(GraphicsDevice gDevice, Bitmap bit, Single Precision) in D:''Dropbox''School''Project FUN''Code''XNA''GPUParticles''GPUParticles''GPUParticles''VelocityTexture.cs:line 16 at GPUParticles.Game1.camInterval_Tick(Object myObject, EventArgs myEventArgs) in D:''Dropbox''School''Project FUN''Code''XNA''GPUParticles''GPUParticles''GPUParticles''Game1.cs:line 302 at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at 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() at GPUParticles.Program.Main(String[] args) in D:''Dropbox''School''Project FUN''Code''XNA''GPUParticles''GPUParticles''GPUParticles''Program.cs:line 15 内部异常:
你可以使用 MemoryStream。示例(未经测试):
Texture2D MakeTextureFromBitmap(Bitmap bmp) {
using (var ms = new MemoryStream()) {
bmp.Save(ms, ImageFormat.Png);
return Texture2D.FromStream(GraphicsDevice, ms);
}
}