这是通过套接字发送位图的正确方法吗? [英] Is this the correct way to send a Bitmap over sockets?
问题描述
我正在使用TCP / IP通过套接字发送Bitmap对象。这是我第一次使用C#的时间,所以我不知道这是不是正确的。做的方式
吧。我已经多次发现我做某事的方式
效率非常低,可以用2等减少10行代码。
为了阅读,我使用的是TcpClient,我打电话给
NetworkStream ns = client.GetStream();得到一个小溪
stream.Read(buffer,0,buffer.Length);
现在已经是我的问题了,我该怎么办制作缓冲区以便我没有
来任意使它成为1 MB?如果我的位图大于1 MB怎么办?请
我需要阅读多次,然后加入缓冲区?我怎么能这样做?$ / b
MemoryStream ms =新的MemoryStream(缓冲区);
Bitmap = new位图(ms);
这看起来像是通过我的Socket发送Bitmap的正确方法吗?
我还发现保存了在发送之前将位图作为PNG使其更小
。这是一个好主意,我可以压缩它吗?
bm.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
非常感谢从新手那里读一个问题。我知道这可能对你们中的一些人没有意义,或者看起来很琐碎。
I''m using TCP/IP to send a Bitmap object over Sockets. This is my first
time using C# at all so I don''t know if this is the "right" way to do
it. I''ve already found out several times the way I was doing something
was really inefficient and could reduce 10 lines of code with 2, etc.
For reading, I am using a TcpClient and I call
NetworkStream ns = client.GetStream(); to get a stream
stream.Read(buffer, 0, buffer.Length);
now already my question is, how do I make buffer so that I don''t have
to arbitrarily make it 1 MB? What if my Bitmap is larger than 1 MB? Do
I need to read multiple times and then "join" the buffers? How would I
do that?
MemoryStream ms = new MemoryStream(buffer);
Bitmap = new Bitmap(ms);
Does this seem like the correct way to send the Bitmap over my Socket?
I also found that saving the Bitmap as a PNG before sending makes it
smaller. Is this a good idea, and can I compress it even more?
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Thanks so much for reading a question from a newbie. I know this might
not make sense to some of you, or seem trivial.
推荐答案
可以你不能直接/从NetworkStream保存/加载?
bm.Save(ns,System.Drawing.Imaging.ImageFormat.Png);
Marc
Can you not save / load directly to / from the NetworkStream?
bm.Save(ns, System.Drawing.Imaging.ImageFormat.Png);
Marc
我刚尝试发送图像(将样本发布到如果你试图直接保存到NetworkStream中,那么
GDI +会抛出一个错误。
我不知道为什么。但是你可以直接从NetworkStream中读取图像
。
-Lenard
Marc Gravell写道:
Hi,
I just tried sending an image (will post a sample in a minute).
GDI+ throws an error if you try to save into the NetworkStream directly.
I don''t know why. You can however read the image from the NetworkStream
directly.
-Lenard
Marc Gravell wrote:
你能直接保存/加载到NetworkStream吗?
bm.Save(ns,System.Drawing。 Imaging.ImageFormat.Png);
Marc
Can you not save / load directly to / from the NetworkStream?
bm.Save(ns, System.Drawing.Imaging.ImageFormat.Png);
Marc
我对这个问题玩了一点,并提出了以下
解决方案。
发送时,你可以使用:
TcpListener listener = new TcpListener(9998);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
Image image = Image.FromFile(@" .. file ..");
使用(MemoryStream ms = new MemoryStream())
{
image.Save(ms,ImageFormat.Png);
byte [] imageBuffer = ms.GetBuffer();
stream.Write(imageBuffer,0,(int)ms.Length);
}
stream.Close(500 );
client.Close();
listener.Stop();
这里你没有必要担心缓冲区,因为MemoryStream会为你处理
。出于某种原因,您无法直接使用NetworkStream
(将图像保存到其中)。如果你试试这个,那么GDI +会抛出异常。我猜这与网络流无关可以搜索(但这只是一个猜测)。
在接收端,你可以直接从
NetworkStream中读取。所以你可以创建一个像这样的方法:
private Image ReceiveBitmap()
{
TcpClient client = new TcpClient();
client.Connect(" 127.0.0.1",9998);
NetworkStream stream = client.GetStream();
Image image = Image.FromStream(stream);
stream.Close();
client.Close();
返回图像;
}
这将读取您的图像并将其返回给调用者。你可以看到
你不需要担心缓冲区的长度,因为
图像加载器会读取正确的字节数。
但请注意,我上面没有进行任何错误检查。
解决问题的另一种方法是不将图像发送到
网络流,首先是大小(比如Int32),然后是缓冲区的
内容。在接收端,你将总是读取
一个Int32(长度),分配一个适合数据的缓冲区,然后
接收到该缓冲区。这样你就可以分配你需要的空间和
。
希望这会有所帮助
-Lenard <
el *********** @ gmail .com 写道:
我正在使用TCP / IP通过套接字发送Bitmap对象。这是我第一次使用C#的时间,所以我不知道这是不是正确的。做的方式
吧。我已经多次发现我做某事的方式
效率非常低,可以用2等减少10行代码。
为了阅读,我使用的是TcpClient,我打电话给
NetworkStream ns = client.GetStream();得到一个小溪
stream.Read(buffer,0,buffer.Length);
现在已经是我的问题了,我该怎么办制作缓冲区以便我没有
来任意使它成为1 MB?如果我的位图大于1 MB怎么办?请
我需要阅读多次,然后加入缓冲区?我怎么能这样做?$ / b
MemoryStream ms =新的MemoryStream(缓冲区);
Bitmap = new位图(ms);
这看起来像是通过我的Socket发送Bitmap的正确方法吗?
我还发现保存了在发送之前将位图作为PNG使其更小
。这是一个好主意,我可以压缩它吗?
bm.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
非常感谢你从新手那里读到一个问题。我知道这可能对你们中的一些人没有意义,或者看起来很琐碎。
I''m using TCP/IP to send a Bitmap object over Sockets. This is my first
time using C# at all so I don''t know if this is the "right" way to do
it. I''ve already found out several times the way I was doing something
was really inefficient and could reduce 10 lines of code with 2, etc.
For reading, I am using a TcpClient and I call
NetworkStream ns = client.GetStream(); to get a stream
stream.Read(buffer, 0, buffer.Length);
now already my question is, how do I make buffer so that I don''t have
to arbitrarily make it 1 MB? What if my Bitmap is larger than 1 MB? Do
I need to read multiple times and then "join" the buffers? How would I
do that?
MemoryStream ms = new MemoryStream(buffer);
Bitmap = new Bitmap(ms);
Does this seem like the correct way to send the Bitmap over my Socket?
I also found that saving the Bitmap as a PNG before sending makes it
smaller. Is this a good idea, and can I compress it even more?
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Thanks so much for reading a question from a newbie. I know this might
not make sense to some of you, or seem trivial.
这篇关于这是通过套接字发送位图的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!