使用字节数组在c#中绑定图像

时间:2013-12-25 09:54:02

标签: asp.net .net c#-4.0

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>");
        }
    }
}

5 个答案:

答案 0 :(得分:10)

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 ;

此代码运作良好..

答案 1 :(得分:2)

我假设您正在尝试使用System.Web.UI.WebControls.Image中的base64字符串显示图像。

首先,创建一个客户端JavaScript函数来设置src标记的<img />属性:

function setImageData(imageBase64) {
    document.getElementById("imageId").src = "data:image/png;base64," + imageBase64;
}

然后,通过

调用该方法
Response.Write("<script>setImageData("+ base64String +")</script>");

答案 2 :(得分:1)

我建议您创建ImageHandler(继承自IHttpHandler),将其注册到web.config文件中,然后修改Button2_Click函数:

public void Button2_Click(object sender, EventArgs e)
{
    ...
    Image1.ImageUrl = "URL_TO_IMAGE_HANDLER.jpg";
    ...
}

您可以在此处阅读有关http处理程序的信息:http://www.codeproject.com/Articles/34084/Generic-Image-Handler-Using-IHttpHandler

这个解决方案比Base64方法好得多,也可以在这里实现缓存支持

答案 3 :(得分:0)

请参阅this教程,您可以使用字节数组来获取图像

更新:顺便说一下,您应该使用此功能:

string imageDataParsed = imageData.Substring( imageData.IndexOf( ',' ) + 1 );
byte[] imageBytes = Convert.FromBase64String( imageDataParsed );
using ( var imageStream = new MemoryStream( imageBytes, false ) )
{
Bitmap image = new Bitmap( imageStream );
}

修改 您可以通过Image.FromStream将base64string转换为图像。您需要先将base64string转换为stream。

byte[] imageBytes = Convert.FromBase64String(imgBase64String);
MemoryStream ms1 = new MemoryStream(imageBytes);
Image img = Image.FromStream(ms1);

答案 4 :(得分:0)

我刚刚解决了这个问题..这是更新的代码..

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>");
            }
        }
    }