当前位置: 动力学知识库 > 问答 > 编程问答 >

sql server - How to Insert C# Image Object into MongoDB

问题描述:

I'm New to mongoDB, and i want to convert a C#/SQL Server Windows App to use MongoDB as its DB, my program is capturing screen shots from the console and converting it into C# Image object and the inserting it into Sql Image type,

my question is how do i do that in MongoDB, my Images are small,

i wort a small program that read from sql and store it in MongoDB, (in MongoDB the SQL Image type become a string) , then i read from MongoDB and try to display it in picture box and i get an error on the memory stream this is the error: followed by the code of my program.

"Parameter is not valid."

 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);

}

}

Can Any one help?on how to insert the Image data the right way so it will go in as Binary Data?

网友答案:

I finally got it to work, this is the code:

    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);

    }
}

Changes are Between ** ** just changed the type in the get collection to the class i needed to insert to MongoDb and now its inserting Binary data that i can get and show as image

分享给朋友:
您可能感兴趣的文章:
随机阅读: