如何动态地将图像绑定到图像控件?

时间:2014-11-13 07:36:32

标签: c# asp.net image dynamic

是的,我知道......是的,我确实谷歌了。问题是我对ASP.NET很新,没有一个搜索结果符合我的要求。

我有这个:

<asp:Image ID="imgView" runat="server" />

在我的数据库中,我存储了图像,我可以通过以下方法检索它们:

public static List<Image> GetRezeptImages(int rezeptId)
{
    using (CookBookDataContext ctx = new CookBookDataContext(Resources.ResourceFile.DBConnection))
    {
        IEnumerable<RezeptBilder> bilder = from b in ctx.RezeptBilders where b.FKRezept == rezeptId select b;
        List<Image> imageList = new List<Image>();

        foreach (RezeptBilder b in bilder)
        {
            imageList.Add(Helper.ByteArrayToImage(b.Bild.ToArray()));
        }

        return imageList;
    }
}

非常重要,当我开始使用Winforms应用程序时,我现在想要转变为ASP网站,Image来自System.Drawing.Image而不是System.Web.UI.WebControls.Image。我也有可能将我的图像转换为字节数组,反之亦然:

public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
    using (MemoryStream ms = new MemoryStream())
    {
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }
}

public static Image ByteArrayToImage(byte[] byteArrayIn)
{
    using (MemoryStream ms = new MemoryStream(byteArrayIn))
    {
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
}

现在如果某个事件被触发,则从数据库中取出System.Drawing.Image并且我想在图像控件中显示它。在这里,我需要一个Image-Url,因此我必须将我的图像保存到服务器。这是我不能做的,因为它不是System.Web.UI.WebControls.Image。我可以将它转换为字节数组,但那么呢?

这样做最温文尔雅的方式是什么?

提前致谢!

4 个答案:

答案 0 :(得分:1)

希望这会有所帮助

System.IO.MemoryStream ms = new System.IO.MemoryStream();


image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  Response.ClearContent();
  Response.ContentType = "image/Gif";
  Response.BinaryWrite(ms.ToArray());


<asp:Image ID="Image1" runat="server" ImageUrl="~/pic.aspx"/>

答案 1 :(得分:1)

您可以使用base64字符串显示二进制图像。像 -

这样的东西
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;

您可以通过演示链接获取此链接的更多详细信息 -

http://www.aspsnippets.com/Articles/Display-image-from-database-in-Image-control-without-using-Generic-Handler-in-ASPNet.aspx

答案 2 :(得分:1)

做这样的事,我可能会工作,为我工作。

  public class ImageToSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
            System.Globalization.CultureInfo culture)
    {
        Image image = value as Image;
        if (image != null)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, image.RawFormat);
            ms.Seek(0, SeekOrigin.Begin);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

并创建Propertey

public Image UserImage
{
    get
        {
            return _model.UserImage;
        }
}   

答案 3 :(得分:1)

你是如何生成byte []的?它工作正常 -

string id = "1";
byte[] bytes = (byte[])GetData("SELECT Data FROM tblFiles WHERE Id =" + id).Rows[0]["Data"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;


private DataTable GetData(string query)
{
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }
            return dt;
        }
    }