gpt4 book ai didi

asp.net - 如何将 'System.Drawing.Image' 添加到 'System.Web.UI.WebControls.Image'

转载 作者:行者123 更新时间:2023-12-04 15:04:59 25 4
gpt4 key购买 nike

我有两个功能:

函数1: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)分配给类型为()的“image1”控件System.Web.UI.WebControls.Image).

显然我不能只分配: image1 = ByteArrayToImage(byteArray);因为我会收到以下错误:无法将类型“System.Drawing.Image”隐式转换为“System.Web.UI.WebControls.Image”

有什么办法吗?

最佳答案

看起来你只是想要一个简单的方法来将图像字节数组转换为图片。没问题。我找到了一个 article这对我帮助很大。

    System.Web.UI.WebControls.Image image = (System.Web.UI.WebControls.Image)e.Item.FindControl("image");

if (!String.IsNullOrEmpty(currentAd.PictureFileName))
{
image.ImageUrl = GetImage(currentAd.PictureFileContents);
}
else
{
image.Visible = false;
}

//The actual converting function
public string GetImage(object img)
{
return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img);
}

PictureFileContents 是一个 Byte[],这就是函数 GetImage 所接受的对象。

关于asp.net - 如何将 'System.Drawing.Image' 添加到 'System.Web.UI.WebControls.Image',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com