【发布时间】:2026-01-22 23:10:01
【问题描述】:
我已经找了好几天了,所有关于如何转换填充了 imageData 的 byte[] 的当前答案都让我回到了这段代码:
using (var ms = new MemoryStream(byte[]))
{
Bitmap bit = new Bitmap(ms);
}
至少是这样的(我现在已经制作了很多版本..我不记得原版了) 但无论我如何使用它,当我尝试将流放入我的位图中时,我总是得到参数的“参数无效”。
这是我目前保存它的方式:
private static byte[] ConvertToByteArr(Bitmap bitmap)
{
byte[] result;
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
result = stream.ToArray();
return result;
}
并在我将字典保存到 .txt 文件时调用它
这就是我试图再次阅读它的方式。
TextReader reader = new StreamReader(trainingPath + "\\" + filename + ".txt");
Dictionary<string, string> tempDict = new Dictionary<string, string>();
String line;
String[] parts;
List<string> names = new List<string>();
while ((line = reader.ReadLine()) != null)
{
parts = line.Split('=');
string key = parts[0];
string value = parts[1];
tempDict.Add(key, value);
Console.WriteLine("added " + key + " to temp Dict");
byte[] byteArr = Encoding.ASCII.GetBytes(tempDict[key]);
MemoryStream ms = new MemoryStream(byteArr);
Bitmap bit = new Bitmap(Image.FromStream(ms));
parts[0] = null;
parts[1] = null;
}
在 Bitmap bit = new Bitmap(Image.Fromstream(ms)) 行之前一切正常。
【问题讨论】:
-
I always get a 'parameter invalid'抛出了什么exact异常?通过哪一行代码? -
发生异常时
tempDict[key]的准确值是多少? -
不要将二进制数据转换为字符串并返回,这是一种无法正常工作的方法
-
mjwills 我得到一个 'System.ArgumentException' tempDict[key] 的值是一个字节数组,其中包含来自我转换的位图的数据,以便我可以将大量数据保存到分类的 .txt 文件中。
-
saruman- 我这样做只是因为我有太多数据。我正在做一个机器学习项目,我需要向其中输入大量数据。但这些被保存为字典
以确保我有正确的信息。我认为将其保存到 .txt 文件比保存和搜索数据库所需的计算机处理能力更少。
标签: c# bitmap console memorystream