6

Is it possible to get something drawn with default .net paint methods (System.Drawing methods) to a SharpDX Texture2D object so that i can display it as a texture? Preferably with the SharpDX Toolkit.

If yes, how?

edit: what i am trying so far:

Bitmap b = new Bitmap(100,100);
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);    
Texture2D tex = Texture2D.Load(g.device, ms); // crashing here
ms.Close();
clamp
  • 33,000
  • 75
  • 203
  • 299
  • Just use the FromStream() method. Which lets you create the texture from a Bitmap that you saved to a MemoryStream. – Hans Passant Dec 06 '13 at 19:26
  • good idea, so i try writing it to a memorystream like this: `bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)` but then sharpdx thinks its a DDS and fails when loading. – clamp Dec 09 '13 at 09:11
  • You forgot ms.Position = 0; before the Load() call. – Hans Passant Dec 09 '13 at 11:36
  • thanks! if you put that as an answer i can accept it. – clamp Dec 09 '13 at 12:02
  • For future reference, "crashing here" is not a good problem description. When you get an exception, **always** post the complete exception details. – Lasse V. Karlsen Dec 09 '13 at 12:24

1 Answers1

6
  b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);    
  Texture2D tex = Texture2D.Load(g.device, ms); 

The Save() call leaves the memory stream positioned at the end of the stream. Which will confuzzle the Load() method, it can't read any data from the stream. You'll have to rewind the stream explicitly. Insert this statement between the two lines of code:

  ms.Position = 0;
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536