我是mongoDB新手,我想转换一个C#/SQL Server Windows应用程序来使用MongoDB作为其DB,我的程序是从控制台捕获屏幕截图并将其转换为C#图像对象并将其插入到Sql Image类型中,我的问题是如何在MongoDB中做到这一点,我的图像很小,我编写了一个小程序来读取sql并将其存储在MongoDB中(在MongoDB中,SQL Image类型变成了一个字符串)。然后我从MongoDB中读取并尝试将其显示在图片框中,在内存流中得到一个错误,这是错误:,后面跟着我的程序代码。“参数无效。”
public partial class Form1 : Form
{
VideoEntities vid = new VideoEntities();
public Form1()
{
InitializeComponent();
connectToMongo();
}
public void connectToMongo()
{
Utilitys util = new Utilitys();
var con = "mongodb://127.0.0.1";
MongoClient client = new MongoClient(con);
var db = client.GetDatabase("Video");
ObjectResult<getFrame_Result> frame;
List<getFrame_Result> frameList;
frame = vid.getFrame(50604803);
frameList = frame.ToList();
string jsonStr = jsonStr = util.deserialze(frameList[0]);
// jsonStr = "{Frame:" + jsonStr + "}";
jsonStr = jsonStr.Replace("[", "{");
jsonStr = jsonStr.Replace("]", "}");
var frameCollection = db.GetCollection<BsonDocument>("Frame");
var oneFrame = BsonDocument.Parse(jsonStr);
frameCollection.InsertOne(oneFrame);
Byte[] data = new Byte[0];
var collection = db.GetCollection<BsonDocument>("Frame");
var filter = Builders<BsonDocument>.Filter.Eq("id", 50604803);
var result = collection.Find(filter).ToList();
string img = (string)result[0]["Frame"];
data = Encoding.ASCII.GetBytes(img);
MemoryStream mem = new MemoryStream(data);
pictureBox.Image = Image.FromStream(mem);
}
}
有人能帮上忙吗?关于如何正确地插入图像数据,使其成为二进制数据?
发布于 2016-08-03 14:14:44
我终于让它工作了,代码是这样的:
public partial class Form1 : Form
{
VideoEntities vid = new VideoEntities();
public Form1()
{
InitializeComponent();
connectToMongo();
}
public void connectToMongo()
{
Utilitys util = new Utilitys();
var con = "mongodb://127.0.0.1";
MongoClient client = new MongoClient(con);
var db = client.GetDatabase("Video");
ObjectResult<getFrame_Result> frame;
List<getFrame_Result> frameList;
frame = vid.getFrame(50604803);
frameList = frame.ToList();
**var frameCollection = db.GetCollection<getFrame_Result>("Frame");**
frameCollection.InsertOne(frameList[0]);
Byte[] data = new Byte[0];
var collection = db.GetCollection<BsonDocument>("Frame");
var filter = Builders<BsonDocument>.Filter.Eq("_id", 50604803);
var result = collection.Find(filter).ToList();
data = (Byte[]) result[0]["Frame"];
MemoryStream mem = new MemoryStream(data);
pictureBox.Image = Image.FromStream(mem);
}
}
更改是*刚刚将get集合中的类型更改为我需要插入到MongoDb中的类,现在它插入了我可以获取并显示为图像的二进制数据
https://stackoverflow.com/questions/38738807
复制相似问题