gpt4 book ai didi

asp.net - 使用字节数组在c#中绑定(bind)图像

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

protected void Button2_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string ext = Path.GetExtension(filename);
if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".PNG" || ext == ".JPG" || ext == ".JPEG" || ext == ".gif" || ext == ".GIF")
{

Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;
}
else
{
Response.Write("<script>alert('unsupported format of photo file');</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
}

最佳答案

Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;

这段代码运行良好..

关于asp.net - 使用字节数组在c#中绑定(bind)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20771432/

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