Converting Image to base64 string.

Posted by Steve Croff on 04-Nov-2008 22:58

I am attempting to take a .NET image object and convert the object to a base64 string. This will ultimately go into an XML file.

My first attempt at this was to use Image.Save(filename) to write the image to a bitmap file, then I read that file into a memptr and then convert the memory with BASE64-ENCODE(). This works fine, except that I cannot delete the bitmap file created by Image.Save(). The bitmap file has a lock that is not released until my ABL session ends. I also tried doing Image.Dispose() but this doesn't release the locked file.)

My second attempt was to somehow save a .NET imageObject to a Progress MEMPTR variable directly without an intermediate bitmap file. The Image.Save() method supports writing to a System.IO.MemoryStream, but I don't see how to put that data into a Progress MEMPTR directly.

At the same time, I thought about just doing the Base64-Encode directly in .NET with System.Convert.ToBase64String(). I was able to get this working in VS2005 like so:

myMemory = NEW System.IO.MemoryStream();

myImage.Save(myMemory, System.Drawing.Imaging.ImageFormat.Bmp);

myBase64String = System.Convert.ToBase64String(myMemory.ToArray()).ToString();

In ABL, I am try to reproduce this functionality, but I can't get past the Image.Save() syntax in ABL. I get 'Unable to understand after "imageObject" ' on the MyImage:Save() statement:

DEFINE VARIABLE myMemory AS System.IO.MemoryStream NO-UNDO.

myMemory = NEW System.IO.MemoryStream().

myImage:Save(myMemoryStream,System.Drawing.Imaging.ImageFormat.Bmp).

In addition to the syntax error, another problem is that the auto-complete in OEA only works up to ImageFormat object... it isn't able to see the various ImageFormat types (Bmp,Icon, etc).

Thank you for any insight on why this fails, or alternative ways of doing this.

All Replies

Posted by jmls on 05-Nov-2008 00:58

try

myImage:Save(myMemoryStream,System.Drawing.Imaging.ImageFormat:Bmp).

Posted by Steve Croff on 05-Nov-2008 10:49

Thanks for that, Julian.

The whole "." vs. ":" thing is biting me.

Posted by jmls on 05-Nov-2008 11:04

heh. I think that its Object (.) vs property (

Posted by Roger Blanchard on 10-Feb-2009 15:11

Steve,

How did you make out with this? We do something similar as we read in a bitmap into a memptr, convert to BASE-64 and then store in a CLOB field in the DB. This is then sent to SonicMQ as XML and distributed as a message.

The issue we are having in 10.2A (NEW GUI) is there is a bug with the probinding source where if you have any LOB defined in the prodataset the bindingsource:ASSIGN will fail IF there is data in the LOB field. This is really the same issue with the prodataset:SAVE-ROW-CHANGES().

Progress TS says I am the only one who has reported this and was wondering if you are storing your BASE-64 string in the database.

Is anyone else experiencing this problem?

Progress has told me it will not be fixed until 10.2C if then.

Posted by Steve Croff on 10-Feb-2009 17:40

The conversion itself is working great, but at this point the XML we are working with remains on the client only, but we may eventually need to also store in our database.

We are not using a bindingsource or dataset anywhere for this function... just manually getting/setting .Image properties directly on .net controls.

Posted by Roger Blanchard on 11-Feb-2009 07:35

Steve,

Thanks for the info. We will have to manage the LOB's ourselves as well. Progress is to get us some sort of hot fix to allow the bindingsource:ASSIGN NOT to fail and ignore the LOB fields.

We shall see.

Posted by Roger Blanchard on 30-Mar-2009 09:10

Steve,

Do you have a sample snippet of code that illustrates the reading in of the image and storing as base64?

Posted by Steve Croff on 30-Mar-2009 10:44

Hi Roger,

Convert.cls contains methods for converting image to text and text to image.

ConvertTestForm.cls is a test application I wrote to extract the image of a desktop shortcut and convert it to base64 text, then back to image again.  If you look in the "OnDragDrop" event handler, you can see how Convert.ImageToChar is called to convert the image of the shortcut to base64.  The "OnButtonClick" event handler goes the other direction (text back to image) and displays the reconstructed image in the test form.

Hope this helps!

[View:~/cfs-file.ashx/__key/communityserver-discussions-components-files/19/Convert.cls:550:0]

[View:~/cfs-file.ashx/__key/communityserver-discussions-components-files/19/ConvertTestForm.cls:550:0]

Steve

Posted by Roger Blanchard on 30-Mar-2009 10:57

Steve,

thanks a bunch!!

Posted by Roger Blanchard on 30-Mar-2009 18:45

Steve,

Great sample...thanks. Unfortunately, I am unable to get the LONGCHAR override working. When the TextBox exceeds 32K I get the error "System.NullReferenceException: Object Reference Not Set to an instance of an object". The line of code generating the error is:

pictureBox1:

Image = Convert:Base64ToImage(textBox1:Text).

Posted by Steve Croff on 31-Mar-2009 09:20

I haven't actually used that override yet myself.  I pretty much cloned it from the char version... I will try and get some time soon to test and see if i can find the problem.

Posted by Roger Blanchard on 31-Mar-2009 09:44

Steve,

There may be an issue with the Progress .NET CLR bridge as this appears to work in VS. I am doing a bit more research right now and will let you know if I find out anything.

Thanks.

This thread is closed