Frage

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. 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:

        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. 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.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:

War es hilfreich?

Lösung

You could use a MemoryStream. Example (untested):

Texture2D MakeTextureFromBitmap(Bitmap bmp) {
    using (var ms = new MemoryStream()) {
        bmp.Save(ms, ImageFormat.Png);
        return Texture2D.FromStream(GraphicsDevice, ms);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top