Business object mapper and images


Author
Message
GRocchi
GRocchi
StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)StrataFrame Beginner (24 reputation)
Group: Forum Members
Posts: 18, Visits: 56
I have an image in my database, I setup the business object mapper with CustomObject Type = System.Drawing.Bitmap and the flag serialize to true, null value option= don't allow null. I rebuild and get the following working code:
Set(ByVal value As System.Drawing.Bitmap)
Dim loStream As New MemoryStream()
Me.BinaryFormatter.Serialize(loStream, value)
Me.CurrentRow.Item("Immagine") = loStream.ToArray()
End Set

When I change the option null value option= return alternate on null/Set null on alternate(reference type) the code generated is the following:

Set(ByVal value As System.Drawing.Bitmap)

Dim loStream As New MemoryStream()

if value IsNot Nothing Then

Me.CurrentRow.Item("Immagine") = loStream.ToArray()

Else

Me.CurrentRow.Item("Immagine") = DBNull.Value

End If

End Set

It seems that the row

Me.BinaryFormatter.Serialize(loStream, value)   

gets missing. In fact I added it manually and all works fine.

Is there a better way to achieve this? Is this by design?

Thanks in advance


Gianpaolo Rocchi

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Really, if you are going to store a bitmap in a database, I recomemnd against using serialization.  It is much larger than storing a byte array of the saved JPG or PNG.  Generally I will create a custom fiedl that accepts an Image and updates the underlying Byte() strong-typed property (database column).  I will also craete an empty or default image for a Get in the case that the image has not been set (Null could be taken into account as well).  Here is an example of one of my custom properties in our medical app:

<Browsable(False), _
BusinessFieldDisplayInEditor(), _
Description("Signature as an Image"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public ReadOnly Property cct_SignatureAsImage() As System.Drawing.Image
Get
    '-- Establish Locals
    Dim r As Bitmap

    '-- See if there is an image to return
    If cct_Signature.Length = 0 Then
        '-- Create a new image appropriatly sized
        r = New Bitmap(Preferences.POSPreferences.SignatureImageWidth, Preferences.POSPreferences.SignatureImageHeight)

        '-- Just clear the image to a white canvas
        Dim g As Graphics = Graphics.FromImage(r)
        g.Clear(Color.White)

        '-- Clean Up
        g.Dispose()
    Else
        '-- Create a memory stream to house the image
        Dim mem As New System.IO.MemoryStream

        '-- Write the signature bytes to a memory stream
        mem.Write(cct_Signature, 0, cct_Signature.Length)
        mem.Seek(0, SeekOrigin.Begin)
        r = New Bitmap(mem)

        '-- Clean Up
        mem.Dispose()
    End If

    '-- Return the results
    Return r
End Get
End Property


Ross L Rooker, Sr.(1)
Ross L Rooker, Sr.(1)
StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)
Group: StrataFrame Users
Posts: 50, Visits: 163



BusinessFieldDisplayInEditor below gives me: The type or namespace 'BusinessFieldDisplayInEditor' could not be found. Are you missing a using directive or a assembly reference?

Here are my using statements:

using MicroFour.StrataFrame.Business;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Text;
using System.Drawing;

What I am trying to do is:

I have a column in this table called GI_PICTURE which is an IMAGE type. I borrowed Trent's code above in this threaded (converted to C#) to do this.

Yet looking at this code I am not sure how this will read the image to display on a form and then save a jpg, etc selected by the user back to the database. Any ideas on how to do this?

What do I set in the BO Mapper on this column?




[Browsable(false), BusinessFieldDisplayInEditor(), Description("GIpicture"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Drawing.Image cct_SignatureAsImage
{
   
 get
    {
  //-- Establish Locals
  Bitmap r = default(Bitmap);
       
  //-- See if there is an image to return
  if (GI_PICTURE.Length == 0)
        {
   //-- Create a new image appropriatly sized

   //r = new Bitmap(Preferences.POSPreferences.SignatureImageWidth, Preferences.POSPreferences.SignatureImageHeight);
            r = new Bitmap(200,200);

   //-- Just clear the image to a white canvas
   Graphics g = Graphics.FromImage(r);
   g.Clear(Color.White);

   //-- Clean Up
   g.Dispose();
  }
        else
        {
   //-- Create a memory stream to house the image
   System.IO.MemoryStream mem = new System.IO.MemoryStream();

   //-- Write the signature bytes to a memory stream
            mem.Write(GI_PICTURE, 0, GI_PICTURE.Length);
   mem.Seek(0, System.IO.SeekOrigin.Begin);
   r = new Bitmap(mem);

   //-- Clean Up
   mem.Dispose();
  }

  //-- Return the results
  return r;
 }
}
Edited 10 Years Ago by Ross L Rooker, Sr.(1)
Santosh Patil
Santosh Patil
StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)StrataFrame Beginner (3 reputation)
Group: Forum Members
Posts: 1, Visits: 28
Column type is  'System.Drawing.Bitmap' you have to set.  And for null value   'System.Drawing.Bitmap(1, 1)'.

 public System.Drawing.Bitmap Col006
        {
            get
            {
                object loValue;
                loValue = this.CurrentRow["Col006"];
                if (loValue == DBNull.Value)
                {
                    return new System.Drawing.Bitmap(1, 1);
                }
                else
                {
                    try
                    {
                        return (System.Drawing.Bitmap)this.BinaryFormatter.Deserialize(new MemoryStream((Byte[])loValue));
                    }
                    catch
                    {
                        return new System.Drawing.Bitmap(1, 1);
                    }
                }
            }
            set
            {
                MemoryStream loStream = new MemoryStream();
                this.BinaryFormatter.Serialize(loStream, value);
                this.CurrentRow["Col006"] = loStream.ToArray();
            }
        }

Ross L Rooker, Sr.(1)
Ross L Rooker, Sr.(1)
StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)StrataFrame User (148 reputation)
Group: StrataFrame Users
Posts: 50, Visits: 163
Thanks. I have never used the Custom Code option in the BO Mapper. I assume this is where I use the code you mentioned above. This seems to have allowed me to get the BO to compile. When I reference this picture column in a form bound to a picturebox, when the form displays I get this error:

An exception of type 'MicroFour.StrataFrame.Business.BusinessLayerException' occurred in MicroFour StrataFrame Business.dll but was not handled in user code

Additional information: An error occurred while refreshing the data from field 'tbvo_Groupi.GI_PICTURE' to property 'Image' on control 'pictureBox1.'  Are you missing FieldPropertyDescriptor for a custom property?

Not sure what this may be looking for?



Edited 10 Years Ago by Ross L Rooker, Sr.(1)
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search