C# 从流中读取位图

发布于 2025-01-17 23:11:53 字数 690 浏览 3 评论 0原文

我有一个网络流,它发送名称和图片。名称和图片以一个特定的单词开始和结尾 例子: beginnamexxxxxxxxendnamebeginpyyyyyyyyyendpic 我将在块中收集流,并检查最后一个块是否包含图片的停止单词。

byte[] data = new byte[2048];
int numBytesRead = stream.Read(data, 0, 2048);
if (numBytesRead > 0)
{
code += Encoding.ASCII.GetString(data, 0, numBytesRead);
}

如果是,我将停止收集并将字符串分为名称部分和图片部分。 原则上,我现在应该只有一个包含图片的字符串。 之后,我尝试将其保存为映像,但我只在最后一个代码步骤

byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(code);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Position = 0;
var image = Image.FromStream(ms, true);

异常中得到一个例外:system.argumentException'system.drawing.dll

有什么想法可能是什么问题?

I've a network stream which sends a name and a picture. Name and picture start and end with a specific word
Example:
BeginNameXXXXXXXEndNameBeginPicYYYYYYYYYEndPic
I'll collect the stream in blocks and check if the last block contains the stop word of the picture.

byte[] data = new byte[2048];
int numBytesRead = stream.Read(data, 0, 2048);
if (numBytesRead > 0)
{
code += Encoding.ASCII.GetString(data, 0, numBytesRead);
}

If yes I'll stop collecting and divide the String into the name part and the picture part.
In principle I should have now a string only containing the picture.
Afterwards I try to save it as image but I only get an exception on the last code step

byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(code);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Position = 0;
var image = Image.FromStream(ms, true);

Exception thrown: 'System.ArgumentException' in System.Drawing.dll

Any ideas what could be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文