我有两个功能:
功能一:ImageToByteArray:用于将图像转换为字节数组,然后存储到Oracle数据库中,在一个BLOB字段中。
public byte[] ImageToByteArray(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
函数2:ByteArrayToImage:用于将数据库中的字节数组转换为图像:
public System.Drawing.Image ByteArrayToImage(byte[] byteArray)
{
MemoryStream img = new MemoryStream(byteArray);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(img);
return returnImage;
}
在我的标记中,我有一个图像控件:
<asp:Image ID="Image1" runat="server" />
在后面的代码中,我想将函数 2 的返回值(类型为System.Drawing.Image )分配给类型为( System.Web.UI.WebControls.Image )的“image1”控件。
显然我不能只分配:
image1 = ByteArrayToImage(byteArray);
因为我会收到以下错误:无法将类型'System.Drawing.Image'隐式转换为'System.Web.UI.WebControls.Image'
有没有办法做到这一点?