Mapping Nhibernate System.Drawing.Image

Question: I get an exception that serializes this class to the nHibernate XML file ({"Could not determine the type for: System.Drawing.Image, System.Drawing for columns: NHibernate.Mapping.Column (Settings)"}).

How to map System.Drawing.Image to nHibernate? And what type of dbtype MS-SQL should be used?

using System; using System.Collections.Generic; using System.Text; namespace nhDBapi.Tables { [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsSettings, nhDBapi", Table = "lsSettings")] public class clsSettings { [NHibernate.Mapping.Attributes.Id(Name = "Settings", Column = "Settings", TypeType = typeof(System.Drawing.Image))] public System.Drawing.Image Settings; } // End partial class lsSettings } // End Namespace nhDBapi.Tables 
+4
source share
3 answers

I would not recommend displaying directly in System.Drawing.Image. It is not only disposable (NHibernate will have to dispose of it, and I'm not sure what it can), but if you get the clsSettings collection, you will create many instances of images, thereby losing processor and memory if you do not use all of them.

Instead, map to byte[] with the varbinary sql type, and if necessary, you process the conversion from and to the image. An example .

It’s also worth checking out this project about supporting a large repository of objects for NHibernate , it seems more efficient than comparing with byte[] and this is also a great article about all the options.

+9
source
+3
source

You protect the storage of images in a database, this is rarely a good idea because of the huge overhead. Do you have a good reason for this? Is this a web application or a client desktop application?

The best solution is to save the image in the file system and use a database to store metadata about the image, such as file name, signature, etc.

But, as Maurizio Scheffer says, if you should do this, you're probably better off using the byte [] array, or perhaps use some kind of custom type that encapsulates the byte [] array and provides helper methods such as public Image GetBitmapImage();

0
source

All Articles