[英]Converting AxaptaContainer to either Byte[] or Base64
Background: I am trying to display an image from an Ax2012 DB on an ASP.Net page. 背景:我试图在ASP.Net页上显示Ax2012数据库中的图像。
Problem: The image is stored in the Ax2012 DB as a BLOB (Or well, an Ax Container). 问题:图像作为BLOB(或者说是Ax容器)存储在Ax2012 DB中。 I must to convert this in my C#.Net webservice (Connected via the BusinessConnector) to a Byte array, or even directly to a Base64 string. 我必须将其在C#.Net Web服务(通过BusinessConnector连接)中转换为Byte数组,甚至直接转换为Base64字符串。
What have I done: With the code as below, I get an exception that the imageObject is not serializable. 我已完成的工作:使用下面的代码,我得到了imageObject不可序列化的异常。 Which I understand to a certain extent, but how else would I do this then? 我在某种程度上了解它,但是那我还能怎么做呢?
Code Behind: 背后的代码:
while (axRecord.Found)
{
string workerRecId = axRecord.get_Field("WorkerRecId").ToString();
string name = axRecord.get_Field("Name").ToString();
string image;
using (MemoryStream ms = new MemoryStream())
{
AxaptaContainer imageObject = (AxaptaContainer)axRecord.get_Field("Image");
new BinaryFormatter().Serialize(ms, imageObject);
image = Convert.ToBase64String(ms.ToArray());
}
string wppServiceWarehouse = axRecord.get_Field("WPPServiceWarehouse").ToString();
dataTable.Rows.Add(new object[] { workerRecId, name, image, wppServiceWarehouse });
axRecord.Next();
}
ASP page: ASP页面:
<asp:Image id="employee_ProfilePhoto" runat="server" imageUrl='<%# "data:image/png;base64," + Eval("Image") %>'/>
I also tried casting it to a string, in which case I get no exception thrown, but also no image ;) 我还尝试将其强制转换为字符串,在这种情况下,不会引发异常,也不会产生图像;)
Any advice? 有什么建议吗?
Thanks 谢谢
You have to convert container to base64 encoded PNG. 您必须将容器转换为base64编码的PNG。
You can add this method to HcmPersonImage
(or any orher) table: 您可以将此方法添加到HcmPersonImage
(或任何其他)表中:
public str getImageAsBase64png()
{
Image imgObj;
BinData bd;
str result;
if (this.Image)
{
imgObj = new Image(this.Image);
imgObj.saveType(ImageSaveType::PNG);
bd = new BinData();
bd.setData(imgObj.getData());
result = bd.base64Encode();
}
else
{
result = "";
}
return result;
}
And then call the getImageAsBase64png
in .cs
code behind: 然后在.cs
代码后面调用getImageAsBase64png
:
axRecord.Call('getImageAsBase64png');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.