如何将“为System.Drawing.Image'添加到'System.Web.UI.WebControls.Image' [英] How to add a 'System.Drawing.Image' to a 'System.Web.UI.WebControls.Image'
问题描述
我有两个功能:
功能1:ImageToByteArray:是用来将图像转换成一个字节数组,然后存储在Oracle数据库中BLOB字段
Function 1: ImageToByteArray: Is used to Convert an Image into a Byte Array and then Store in an Oracle Database, in a BLOB Field.
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:用于从数据库转换一个字节数组转换为图像:
Function 2: ByteArrayToImage: Is used to Convert a Byte Array from the Database into an Image:
public System.Drawing.Image ByteArrayToImage(byte[] byteArray)
{
MemoryStream img = new MemoryStream(byteArray);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(img);
return returnImage;
}
在我的标记我有一个Imgage控制:< ASP:图片ID =Image1的=服务器/>
In my Markup I have an Imgage Control:
<asp:Image ID="Image1" runat="server" />
在code背后,我想从功能2至返回值(这是类型的为System.Drawing.Image )分配给了此搜索控制它的类型的( System.Web.UI.WebControls.Image )。
In the Code Behind I want to Assign the Returned Value from Function 2 to (which is of type System.Drawing.Image) to the "image1" control which is of type (System.Web.UI.WebControls.Image).
显然,我不能就这么分配: =此搜索ByteArrayToImage(字节阵列);
因为我得到以下错误:无法隐式'为System.Drawing.Image'转换为'System.Web.UI.WebControls.Image
Obviously I can't just assign:
image1 = ByteArrayToImage(byteArray);
because I'd get the following error: Cannot implicitly convert type 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image'
反正有做到这一点?
推荐答案
您不能。 WebControls.Image只是一个图片网址HTML容器 - 图像数据直接在它不能存储,你只存储引用(URL)添加到图像文件
You can't. WebControls.Image is just a HTML container for an image url - you can't store the image data directly in it, you just store the reference (url) to an image file.
如果你需要动态检索图像数据,通常的方法是创建一个图像处理程序将处理该请求并返回图像作为流浏览器可以显示。
If you need to retrieve image data dynamically, the usual approach is to create an image handler that will handle the request and return the image as a stream that the browser can display.
请参阅此<一个href=\"http://stackoverflow.com/questions/3437740/how-to-convert-system-drawing-image-to-system-web-ui-webcontrols-image\">question
这篇关于如何将“为System.Drawing.Image'添加到'System.Web.UI.WebControls.Image'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!