tags:

views:

74

answers:

2

I want to stores fotos use DB4o and a tried de following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace imagemsDb4o
{

class Person
public class Person
{
    public string Name { set; get; }
    public byte foto { set; get; }
}

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

           private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo("c:\\test.yap");
            if (fi.Exists)
                fi.Delete();
            pictureBox1.Load("c:\\sunset.jpg");

            Person pers = new Person();
            pers.Name = "Martin";
            pers.bild = (System.Drawing.Bitmap)(pictureBox1.Image);

            IObjectContainer db = Db4oFactory.OpenFile("c:\\test.yap");
            db.Store(pers);
            db.Commit();
            db.Close();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Person suchpers = new Person();
            suchpers.Name = "Martin";

            IObjectContainer db = Db4oFactory.OpenFile("c:\\test.yap");
            IObjectSet result = db.QueryByExample(suchpers);
            Person gefunden = (Person)result.Next();

            textBox2.Text = (string) gefunden.Name;

            pictureBox2.Image = (byte) gefunden.bild;
            db.Close();
        }

    }
}

It gives error when i click button2. What change must I make?

+1  A: 

I think you should convert the picture to a byte array before assigning to the foto property and convert the byte array back to a bitmap before assigning to the image property of the picturebox

Raj
Raj, Please can u send or show me a litle exemple? Thank you in advance
Salomao I. manjate
Hello SalamaoSorry Just picked up your note. I see that Gamlor has already responded to your request.
Raj
+3  A: 

Hi

You're trying to store a GUI- / Drawing-Object from WindowForms/WPF (The System.Drawing.Bitmap-Object). However this doesn't work. The reason is that GUI-class (Winforms, WPF, etc) often contains references to the underlying drawing-system, like native handles to OS-resources. And those cannot be serialized correctly. I such cases you create a class which represents the Image. This class contains a the representation of the picture. Typically this is a Blob which stores the image.

There are two basic ways to handle Blobs. Either you store a blob as byte-array in the database or you use the special db4o-Blob-Type. Both have their advantages.

Advantages/Disadvantages with byte array:

  • The blobs are in the db4o-database-file. So there's only a single file to copy around.
  • Byte-arrays are part of the normal db4o-transaction and behave as expected.
  • When storing large blobs, you might run into the database-size limitation of db4o. (256 GB)

Advantages/Disadvantaged with db4o-blobs

  • The blobs are stored as regular files outside the database. This keeps the database itself small. Furthermore you just can access all stored blobs with a regular application.
  • You always need to copy the blob-directory and the database.
  • The db4o-blobs works outside the db4o transaction. This means that a db4o-blob behaves different than any other stored object (and the API is a little strange). However this allows to retrieve a db4o-blob without blocking the current transaction.

For your case I would store a byte[] array with the picture in the Person class. Or you create a special Image-class. This image-class contains then a byte-array with the picture. And a few methods to convert this byte-array from and to a Winforms-bitmap.

Gamlor
pf. pode me enviar um pqueno exemplo?
Salomao I. manjate
Gamlor,Please can u send or show me a litle exemple?Thank you in advance
Salomao I. manjate
Thank u Gamlor. I get it!Many thank!
Salomao I. manjate