XpyXt

Hi again!!!

I have a picture in a buffer and i need put it in picturebox. The error give me is: Invalid parameter in the source code Image ^Ima = Drawing::Image::FromStream(asd); // asd is a stream.



Code Snippet

MemoryStream ^streamMem = gcnew MemoryStream;
StreamReader ^PrivateStreamW = gcnew StreamReader(sysWsk->DebugSR());

array<unsigned char>^ImaArray = gcnew array<unsigned char>(6866);
while( PrivateStreamW->Peek() >= 0)
{
ImaArray[Size] = /*(Char)*/PrivateStreamW->Read();
Size++;
}
Debug::WriteLine("Size Sector " + Size.ToString());

streamMem->Write(ImaArray,0,6866);
Stream ^asd = streamMem;
Image ^Ima = Drawing::Image::FromStream(asd); // <- CRASH
// or Image ^Ima = Drawing::Image::FromStream(streamMem); // <- CRASH
pictureBox1->Image = Ima;



Any idea


I Have other source and this work it, and i see image in picturebox.

Code Snippet

Form1 ^Ob = (Form1^) Obj;
Debug::WriteLine("Escuchando...");
Socket ^tcpListen = gcnew Socket(AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp);
IPEndPoint ^iped = gcnew IPEndPoint(IPAddress::Any,666);
tcpListen->Bind(iped);
tcpListen->Listen(0);
Socket ^AcceptSck = tcpListen->Accept();
array<unsigned char>^msg = gcnew array<unsigned char>(3932214);
int rcv = AcceptSck->Receive(msg,3932214,SocketFlags::None);
MemoryStream ^a = gcnew MemoryStream;
a->Write(msg,0,rcv);
Debug::WriteLine("Size: " + rcv.ToString());
Image ^ima = Drawing::Image::FromStream(a);
Ob->pictureBox1->Image = ima;



Thank!!!


Re: Windows Forms General Problem with FromStream in PictureBox

XpyXt

Up!!! i continue with the problem.




Re: Windows Forms General Problem with FromStream in PictureBox

nobugz

I told you before that StreamReader is only suitable for text, not binary data. If you insist on using an intermediary stream, use FileStream and array<Byte>^.





Re: Windows Forms General Problem with FromStream in PictureBox

XpyXt

umm... but FileStream only use for local files, i dont use for sockets...
I am using the TcpListener and TcpClient for create server.




Re: Windows Forms General Problem with FromStream in PictureBox

nobugz

You're right. Why do you want to use the intermediate stream The MemoryStream already acts as an intermediate. You shouldn't need that either, you should be able to pass the NetworkStream instance to Image:: FromStream(). All that FromStream() needs is a Stream instance and NetworkStream is a stream. If that doesn't work, there is probably something wrong with the data sent to the socket.





Re: Windows Forms General Problem with FromStream in PictureBox

XpyXt

Solved

Code Snippet

Image ^sysConnection::GetImg()
{
// NOTA mySckEx->GetStream()
try
{
MemoryStream ^streamMem = gcnew MemoryStream();
NetworkStream ^netStream = mySckEx->GetStream();

array<Byte>^Data = gcnew array<Byte>(6866);
int bytes = netStream->Read(Data,0,Data->Length);
Debug::WriteLine("SIZE " + bytes.ToString());
streamMem->Write(Data,0,Data->Length);
// Thread::Sleep(100);
Image ^Ima = Drawing::Image::FromStream(streamMem);
Debug::WriteLine("LISTO!!");
return Ima;

}
catch(System::Exception ^e)
{
Debug::WriteLine("ERROR in return IMA: " + e->Message);
}

}



Thank all.