Consultor Eletrônico



Kbase P145740: How to convert a BLOB field to a System.Drawing.Image object
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   5/5/2009
Status: Unverified

GOAL:

How to convert a BLOB field to aSystem.Drawing.Image object

GOAL:

Can I convert a BLOB to a System.Drawing.Image object without writing the BLOB to a file?

FACT(s) (Environment):

Windows
OpenEdge 10.2A

FIX:

The following sample code shows one possible way to take binary data (in either a file or a BLOB field) and create a .NET System.Drawing.Image object out of it:

define variable vMemPtr as memptr no-undo.
define variable vSize as integer no-undo.
define variable vLoop as integer no-undo.
define variable vMemStream as System.IO.MemoryStream no-undo.
define variable vImage as System.Drawing.Image no-undo.

/* this sample code pulls the binary data in from a file but you */
/* can just as easily use copy-lob to copy the blob field to the memptr */
/* this sample code is also slow when the binary data is large since we */
/* are doing a buyte by byte copy of the data. */
copy-lob from file "brian.jpg" to vMemPtr.
vSize = get-size(vMemPtr).
vMemStream = new System.IO.MemoryStream(vSize).


do vLoop = 1 to vSize:
vMemStream:WriteByte(get-byte(vMemPtr,vLoop)).
end.

set-size(vMemPtr) = 0.

do on error undo, leave:
vImage = System.Drawing.Image:FromStream(vMemStream).
catch e as System.ArgumentException:
vImage = ?.
end catch.
end.