public partial class _Default : Page
{
public class DBValue
{
public string Color { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
FormView1.DataSource = new List<DBValue> { new DBValue { Color="Red" } };
FormView1.DataBind();
}
}
Output
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
You can not. Background image is depending on element size. If you want the element to depend on the image size than you will have to use image inside that element or some javascripting or create more methods in HttpHandler so they return the size
and width of the image.
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
mmhbk
Member
51 Points
245 Posts
Background image from DB
Aug 18, 2012 12:37 PM|LINK
Hello,
I'm trying to give my div a background image from database, but it doesn't work.
<div id="imagediv" class="centered_div" style="background-image: url('<%# "GetImage.aspx?id=" + Eval("value.ID") %>');" >
controls
</div>
Please help.
Thanks.
mitja.GTI
Star
11157 Points
2094 Posts
Re: Background image from DB
Aug 18, 2012 04:28 PM|LINK
Please take a look at my blog post (Saving Images to SQL Server as Image Type with GridView) where you will see how to get the image from the DB and use HttpHandler to serve it to the user.
mitja.gti | www.mitjagti.com
mmhbk
Member
51 Points
245 Posts
Re: Background image from DB
Aug 18, 2012 06:31 PM|LINK
That's not what I meant.
I know how to retrieve images from DB and show them on a web page.
I''m having a problem with doing the same thing as div's background picture.
mitja.GTI
Star
11157 Points
2094 Posts
Re: Background image from DB
Aug 18, 2012 07:05 PM|LINK
Generic HttpHandler in root directory
public class GetImage : IHttpHandler { private static Random randonGen = new Random(); public void ProcessRequest(HttpContext context) { Bitmap myBitmap = new Bitmap(100, 100); Graphics g = Graphics.FromImage(myBitmap); if (context.Request.QueryString["color"] == null) g.Clear(CreateRandomColor()); else g.Clear(Color.FromName(context.Request.QueryString["color"].ToString())); MemoryStream ms = new MemoryStream(); myBitmap.Save(ms, ImageFormat.Jpeg); byte[] bmpBytes = ms.GetBuffer(); context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(bmpBytes); myBitmap.Dispose(); ms.Close(); context.Response.End(); } private static Color CreateRandomColor() { Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255)); return randomColor; } public bool IsReusable { get { return false; } } }Default.aspx
<asp:FormView ID="FormView1" runat="server"> <ItemTemplate> <div style="width:100px; height:100px; background-image:url('<%# "../GetImage.ashx?color=" + Eval("Color") %>');"></div> </ItemTemplate> </asp:FormView>Code behind
public partial class _Default : Page { public class DBValue { public string Color { get; set; } } protected void Page_Load(object sender, EventArgs e) { FormView1.DataSource = new List<DBValue> { new DBValue { Color="Red" } }; FormView1.DataBind(); } }Output
mitja.gti | www.mitjagti.com
mmhbk
Member
51 Points
245 Posts
Re: Background image from DB
Aug 19, 2012 06:41 AM|LINK
Thanks. I was missing the FormView enclosing.
How can I make the div's height and width change according to the size of the image?
mmhbk
Member
51 Points
245 Posts
Re: Background image from DB
Aug 19, 2012 06:45 AM|LINK
Thanks. I was missing the FormView enclosing.
How can I make the div's height and width change according to the size of the image?
mitja.GTI
Star
11157 Points
2094 Posts
Re: Background image from DB
Aug 19, 2012 12:12 PM|LINK
You can not. Background image is depending on element size. If you want the element to depend on the image size than you will have to use image inside that element or some javascripting or create more methods in HttpHandler so they return the size and width of the image.
mitja.gti | www.mitjagti.com