Kbase P170324: How to load image into .NET byte array ?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  11/18/2010 |
|
Status: Unverified
GOAL:
How to load image into .NET byte array ?
GOAL:
How to load a file into a .NET System.Byte[] ?
FACT(s) (Environment):
OpenEdge 10.2x
Windows
FIX:
A .NET control may expect to be provided with binary data in a byte-array, for example an image to display or a sound fragment to play.
While the .NET byte array is similar to an ABL MEMPTR type, there are differences and the two do not map directly to eachother.
Following is an example on now to instantiate a .NET byte array and load a file into it from ABL code:
DEFINE VARIABLE clt AS System.Net.WebClient.
DEFINE VARIABLE strm AS System.IO.Stream.
DEFINE VARIABLE myByteArray AS "System.Byte[]".
DEFINE VARIABLE myReader AS System.IO.BinaryReader.
/* added code so gif file is loaded from current working dir */
DEFINE VARIABLE cTempfilename AS CHARACTER NO-UNDO.
FILE-INFO:FILE-NAME = "myimage.gif".
cTempfilename = REPLACE(FILE-INFO:FULL-PATHNAME,"~\","/").
clt = new System.Net.WebClient().
strm = clt:OpenRead("file:///" + cTempfilename ).
MESSAGE strm:Length /*strm:ReadByte()*/ VIEW-AS ALERT-BOX.
/* inlined variations */
myByteArray = CAST( System.Array:CreateInstance(Progress.Util.TypeHelper:GetType( "System.Byte" ) , INTEGER(strm:Length) ) , "System.Byte[]" ).
myByteArray:Initialize().
myReader = NEW System.IO.BinaryReader(strm).
myByteArray = myReader:ReadBytes(INTEGER(strm:Length)).
MESSAGE myByteArray:GetValue(0) myByteArray:GetValue(1) VIEW-AS ALERT-BOX INFO BUTTONS OK.