-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Getting images from the database.
PostPosted: Fri May 04, 2007 5:01 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I have a problem when getting a blob (which is a bitmap image) from a database table. The blob is saved in a Byte[] and it seems ok when I debug the applicaiton, the array has values (as weel as other properties), but how do I convert the array to a System.Drawing.Bitmap? I guess this is more of a C# question, but any help (example code) is appreciated.


Mapping documents (partial):
Code:
namespace HibTest
{
  public class TemplateMapping
  {
    public TemplateMapping(){
    }

    public virtual int TemplateId {
      set { templateId = value; }
      get { return templateId; }
    }

    public virtual Byte[] BitmapNormal {
      set {bitmapNormal = value;}
      get {return bitmapNormal;}
    }

    private int templateId;
    private Byte[] bitmapNormal;
  };
}


This is what I'm trying to do:
Code between sessionFactory.openSession() and session.close():
Code:
  TemplateMapping foo = (TemplateMapping)
      session.Load(typeof(TemplateMapping), 100);

  System.IO.Stream stream = new System.IO.MemoryStream(foo.BitmapNormal, true);
  stream.Write(foo.BitmapNormal, 0, foo.BitmapNormal.Length);
  Bitmap boo = new Bitmap(stream);


Full stack trace of any exception that occurs:(translated from Swedish)
Code:
System.Runtime.InteropServices.ExternalException: A common error in GDI+.
   at System.Drawing.Bitmap..ctor(Stream stream)
   at HibTest.Program.Main(String[] args) i C:\slask\HibTest\HibTest\Program.cs:rad 37


Name and version of the database you are using:
Firebird version 2.x

Best regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 5:08 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I missed the mapping file, here it is (partial):
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="HibTest.TemplateMapping, HibTest" table="template">
    <id name="TemplateId" column="templateid" type="int" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">templateid_generator</param>
      </generator>
    </id>
    <property name="BitmapNormal" type="BinaryBlob" column="bitmapnormal"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 6:47 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
Put the byte array in a memorystream, load the image from the stream.

Code:
byte[] logo;
// assign a value to logo
MemoryStream ms = new MemoryStream(logo);
Image image = new Bitmap(ms);


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 5:55 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I tested what you said, but I don't get it to work. The exception is (translated from Swedish):

Code:
System.Runtime.InteropServices.ExternalException: A common error raised in GDI+.
   vid System.Drawing.Bitmap..ctor(Stream stream)
   vid HibTest.Program.Main(String[] args) i C:\slask\HibTest\HibTest\Program.cs:row 43


The image that is stored in the database is also available in a normal bitmap-file (before it is loaded into the database, which occurs during installation of my application). I loaded the file into a memory stream with the following code:

Code:
  Bitmap boo2 = new Bitmap("c:\\tmp\\100.bmp");
  System.IO.MemoryStream stream2 = new System.IO.MemoryStream();
  boo2.Save(stream2, System.Drawing.Imaging.ImageFormat.Bmp);

When I debug the application, and look at the stream2 and stream variables (see my first posting) there is a difference in since of the byte arrays. The stream2 variable loaded from file is 4340 elements (_buffer dimension), and stream loaded from database via NHibernate is 7558. Why is it so? Is that the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 5:08 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
The code I showed you is the code I used in several applications, and it works.

I never mapped the data as binary though (didn't know you could :) ), my only guess now is that you've used binary as column type in your database (if ms sql) and not varbinary. In the assumption again that it works like i.e. char and varchar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 2:22 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
What did you map the data as? The column type in my databasetable is BLOB (mapped to BinaryBlob), we are using Firebird as database server.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 3:38 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
Forming image from the string:

Code:
Dim memStr As New System.IO.MemoryStream(Convert.FromBase64String(_logo))
Return System.Drawing.Image.FromStream(memStr)


Image to string:

Code:
Dim memStr As New System.IO.MemoryStream
img.Save(memStr, Drawing.Imaging.ImageFormat.Jpeg)
_logo = Convert.ToBase64String(memStr.ToArray())


Mapping:
Code:
<property access="field" name="_logo" column="ArticleLogo" type="StringClob" />


Table column is mapped as text, which basically is a pointer to an area.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 3:38 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
Thanks for your help, but I still don't get it to work. Same exception as earlier.

Has anyone out there loaded a bitmap from a database, using mapping type "BinaryBlob" in NHibernate?

This is the code I'm trying to use:
Code:
  TemplateMapping foo = (TemplateMapping)session.Load(typeof(TemplateMapping), 100);
  System.IO.MemoryStream stream = new System.IO.MemoryStream(foo.BitmapNormal);
  Bitmap boo = new Bitmap(stream);


BitmapNormal is a Byte[].

Please help me! I'm in urgent need of assistance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 6:24 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I found the problem, finally!

It seems like there is a difference between how Interbase and Firebird handle blob-images. I’m using a Firebird database server, but the actual database file was created with Interbase, normally this is not a problem, but apparently when dealing with images…

I created a new table with a blob column, and made a C#-program that loaded a bitmap file into a memory stream, and that stream was inserted into the table using the FirebirdSql-library (FirebirdSql.Data.FirebirdClient.dll), not NHibernate. I then fetched that blob with NHibernate without any problems!

Kinda tricky!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 9:37 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
I'm glad you finally got it working :).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.