首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在MongoDB中插入C#图像对象

如何在MongoDB中插入C#图像对象
EN

Stack Overflow用户
提问于 2016-08-03 08:50:57
回答 1查看 1K关注 0票数 1

我是mongoDB新手,我想转换一个C#/SQL Server Windows应用程序来使用MongoDB作为其DB,我的程序是从控制台捕获屏幕截图并将其转换为C#图像对象并将其插入到Sql Image类型中,我的问题是如何在MongoDB中做到这一点,我的图像很小,我编写了一个小程序来读取sql并将其存储在MongoDB中(在MongoDB中,SQL Image类型变成了一个字符串)。然后我从MongoDB中读取并尝试将其显示在图片框中,在内存流中得到一个错误,这是错误:,后面跟着我的程序代码。“参数无效。”

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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);

    }
}

有人能帮上忙吗?关于如何正确地插入图像数据,使其成为二进制数据?

EN

回答 1

Stack Overflow用户

发布于 2016-08-03 14:14:44

我终于让它工作了,代码是这样的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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中的类,现在它插入了我可以获取并显示为图像的二进制数据

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38738807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文