0

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:

neeKo
  • 4,280
  • 23
  • 31
tversteeg
  • 4,717
  • 10
  • 42
  • 77
  • You're not creating a texture from it, you're just attempting to create an empty texture of the same width and height as the bitmap. Do you need to use Bitmap in your solution? Because if you only need to create a texture with custom data, I could suggest a solution. – neeKo Apr 22 '12 at 23:35

1 Answers1

1

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);
    }
}
Asik
  • 21,506
  • 6
  • 72
  • 131
  • Should the stream be rewinded? Just a thought – neeKo Apr 23 '12 at 08:05
  • Thank you for that, but I wan't to create a new Texture2D with only some of the Data, I want to create a webcam application which takes a snapshot and converts it into a whole new Texture2D with only the difference between black and white visible in 2 colors. – tversteeg Apr 23 '12 at 14:29
  • @ThomasVersteeg Then change your Bitmap as needed before calling this method. See example on MSDN: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx – Asik Apr 23 '12 at 16:37
  • I used the bitmap change tutorial, and then I try to change the Texture into a Texture2D with the code Dr_Asik supplied, but when I debug I get an InvalidOperationException was unhandled error on the line: return Texture2D.FromStream(GraphicsDevice, ms); – tversteeg Apr 23 '12 at 20:06
  • Try using ImageFormat.Png instead of ImageFormat.Bmp. I updated the sample code in my answer. – Asik Apr 23 '12 at 20:22