I have a VertexBuffer object that I "manually" load with vertices. I then attempt to create a Mesh object by calling the Mesh.FromStream method. The first parameter of Mesh.FromStream takes a stream and I attempted to get the stream from the VertexBuffer object by calling VertexBuffer.Lock.
I need a Mesh because I want to make use of the Intersect method. Has anyone ever attempted create a Mesh object from a "raw" VertexBuffer If so and you were successful, I would be interested in knowing how you did it.
GraphicsStream gs = vtxBuffer.Lock(0, vtxBuffer.SizeInBytes, LockFlags.ReadOnly);
mesh =
Mesh.FromStream(gs, MeshFlags.Managed, e.Device);vtxBuffer.Unlock();

Mesh.FromStream method of C#
kunal shanishchara
The FromStream method wants an x file in this stream. If you want to create a mesh with a “custom” vertex buffer you have to go another way.
Create a Mesh object with an empty vertex buffer and then fill this vertex buffer. Look in the documentation for “How Do I : Create a Mesh Object“.
ArBeeBee
I am pulling a mesh across the network via a NetworkStream. I know that I'm receiving the mesh properly because I can save it to a file (on the receiving end), then use "Mesh.FromFile" to fetch the Mesh.
However I'd like to avoid the File I/O operations and generate the Mesh directly from the NetworkStream. This didn't work. "Mesh.FromStream" choked. I then tried copying the Mesh into a MemoryStream. It also choked. It seems that the "Mesh.FromStream" method only works for FileStreams. I've tried every possible FromStream overload.
Below is the code, and the resulting error. Any guidance would be very much appreciated.
//--------------------------CODE-------------------------------------------------
System.IO.Stream meshStream = new MemoryStream();
System.IO.StreamWriter meshStreamWriter = new StreamWriter(meshStream);
//Read the Mesh from the NetworkStream into a MemoryBuffer
clientStreamReader.ReadBlock(inBuffer,0,meshBufferSize);
meshStreamWriter.Write(inBuffer,0,meshBufferSize);
//Save the MemoryBuffer into the Mesh. This works for FileStreams
myMesh = Mesh.FromStream(meshStream, meshBufferSize,
MeshFlags.SystemMemory,
device, out adjacency, out exMaterials,
out effectsInstance);
//--------------------------- ERROR--------------------------------------------------
se.StackTrace " at Microsoft.DirectX.Direct3D.Mesh.FromStream(Stream stream, Int32 readBytes, MeshFlags options, Device device, GraphicsStream& adjacency, ExtendedMaterial[]& materials, EffectInstance[]& effects)\r\n at TCPSocketClient.Client.btnSendMessage_Click(Object sender, EventArgs e) in c:\\dev\\csharp\\network\\meshnetworkclient\\form1.cs:line 159" string
Leandro Ramos
I was having same problem. But found this works. Hope this help
MemoryStream stream = new MemoryStream();stream.Write(buffer, 0, buffer.Length);
stream.Flush();
stream.Position = 0;
mesh =
Mesh.FromStream(stream, MeshFlags.Managed, this.device);Tim Reid
Maybe you have got me wrong. This “How Do I “ shows you how you can transfer custom vertex buffer data to a mesh. It should be not that complicated to modify the code sample from that that it copies the data you already have in a vertex buffer to the vertex buffer of a mesh.
EvanBlack
You have to seek the buffer back to position 0 after you have written to it.
But I am recommending another solution. Create a MemoryStream with the right size and use GetBuffer to get access to the internal buffer. You can now use this buffer to read from the network stream. This will save you one copy.
SGMX