简体   繁体   English

如何在C#XNA中访问位图的宽度和高度?

[英]How do I acces the width and height of an Bitmap in C# XNA?

I am trying to create a application where I create a Bitmap, and then take some variables out of it and make a Texture2D from it. 我试图创建一个应用程序,在其中创建一个位图,然后从中取出一些变量,并从中创建Texture2D。 This is what I've got: 这就是我得到的:

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

Then I try to create a Texture from it: 然后,我尝试从中创建一个Texture:

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

This is where I get the error, I get this: 这是我得到错误的地方,我得到了:

System.ArgumentException was unhandled Message=Parameter is not valid. 未处理System.ArgumentException消息=参数无效。 Source=System.Drawing StackTrace: at 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: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. Source = System.Drawing StackTrace:位于GPUParticles.VelocityTexture.createVelocityMapBitmap(GraphicsDevice gDevice,位图位,单精度)的System.Drawing.Image.get_Width()处,位于D:\\ Dropbox \\ School \\ Project FUN \\ Code \\ XNA \\ GPUParticles \\ GPUParticles \\ GPUParticles \\ VelocityTexture.cs:第16行,位于GPUParticles.Game1.camInterval_Tick(Object myObject,EventArgs myEventArgs),位于D:\\ Dropbox \\ School \\ Project FUN \\ Code \\ XNA \\ GPUParticles \\ GPUParticles \\ GPUParticles \\ Game1.cs:第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, 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() at GPUParticles.Program.Main(String[] args) in D:\\Dropbox\\School\\Project FUN\\Code\\XNA\\GPUParticles\\GPUParticles\\GPUParticles\\Program.cs:line 15 InnerException: System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)在Microsoft.Xna.Framework.WindowsGameHost,System.Windows.Forms.Application.Run(表单mainForm)。 D:\\ Dropbox \\ School \\ Project中GPUParticles.Program.Main(String [] args)处Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)处的Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) FUN \\ Code \\ XNA \\ GPUParticles \\ GPUParticles \\ GPUParticles \\ Program.cs:第15行InnerException:

You could use a MemoryStream. 您可以使用MemoryStream。 Example (untested): 示例(未试用):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM