<p>What I want to achieve is this. I want to give the user the ability to upload an image file, store the image in BLOB in SQL Server, and then use this image as a logo in other pages of the site. </p>

<p>I have done this by using  </p>

<pre><code>   Response.Clear();
   Response.ContentType = "image/pjpeg";
   Response.BinaryWrite(imageConents);
   Response.End();
</code></pre>

<p>but to do this, I use a User control in the place where I want to show the image. I want to do it if possible using an asp:Image control, or even a pure old html image control. Is this possible?</p>

[{'answer_id': 21883, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 0, 'selected': False, 'text': '<p>We actually just released some classes that help with exactly this kind of thing:</p>\n\n<p><a href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449" rel="nofollow noreferrer">http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449</a></p>\n\n<p>Specifically, check out the DatabaseImage sample.</p>\n'}, {'answer_id': 21885, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': '<p>You can BASE64 encode the content of the image directly into the SRC attribute, however, I believe only Firefox will parse this back into an image.</p>\n\n<p>What I typically do is a create a very lightweight HTTPHandler to serve the images:</p>\n\n<pre><code>using System;\nusing System.Web;\n\nnamespace Example\n{  \n    public class GetImage : IHttpHandler\n    {\n\n        public void ProcessRequest(HttpContext context)\n        {\n            if (context.Request.QueryString("id") != null)\n            {\n                Blob = GetBlobFromDataBase(id);\n                context.Response.Clear();\n                context.Response.ContentType = "image/pjpeg";\n                context.Response.BinaryWrite(Blob);\n                context.Response.End();\n            }\n        }\n\n        public bool IsReusable\n        {\n            get\n            {\n                return false;\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>You can reference this directly in your img tag:</p>\n\n<pre><code>&lt;img src="GetImage.ashx?id=111"/&gt;\n</code></pre>\n\n<p>Or, you could even create a server control that does it for you:</p>\n\n<pre><code>using System;\nusing System.Web;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\n\nnamespace Example.WebControl\n{\n\n    [ToolboxData("&lt;{0}:DatabaseImage runat=server&gt;&lt;/{0}:DatabaseImage&gt;")]\n    public class DatabaseImage : Control\n    {\n\n        public int DatabaseId\n        {\n            get\n            {\n                if (ViewState["DatabaseId" + this.ID] == null)\n                    return 0;\n                else\n                    return ViewState["DataBaseId"];\n            }\n            set\n            {\n                ViewState["DatabaseId" + this.ID] = value;\n            }\n        }\n\n        protected override void RenderContents(HtmlTextWriter output)\n        {\n            output.Write("&lt;img src=\'getImage.ashx?id=" + this.DatabaseId + "\'/&gt;");\n            base.RenderContents(output);\n        }\n    }\n}\n</code></pre>\n\n<p>This could be used like</p>\n\n<pre><code>&lt;cc:DatabaseImage id="db1" DatabaseId="123" runat="server/&gt;\n</code></pre>\n\n<p>And of course, you could set the databaseId in the codebehind as needed.</p>\n'}, {'answer_id': 21887, 'author': 'Fredrik Kalseth', 'author_id': 1710, 'author_profile': 'https://Stackoverflow.com/users/1710', 'pm_score': 5, 'selected': True, 'text': '<p>Add a \'Generic Handler\' to your web project, name it something like Image.ashx. Implement it like this:</p>\n\n<pre><code>public class ImageHandler : IHttpHandler\n{\n\n    public void ProcessRequest(HttpContext context)\n    {\n        using(Image image = GetImage(context.Request.QueryString["ID"]))\n        {    \n            context.Response.ContentType = "image/jpeg";\n            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);\n        }\n    }\n\n    public bool IsReusable\n    {\n        get\n        {\n            return true;\n        }\n    }\n}\n</code></pre>\n\n<p>Now just implement the GetImage method to load the image with the given ID, and you can use</p>\n\n<pre><code>&lt;asp:Image runat="server" ImageUrl="~/Image.ashx?ID=myImageId" /&gt; \n</code></pre>\n\n<p>to display it. You might want to think about implementing some form of caching in the handler too. And remember if you want to change the image format to PNG, you need to use an intermediate MemoryStream (because PNGs require a seekable stream to be saved).</p>\n'}, {'answer_id': 21889, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 0, 'selected': False, 'text': '<p>Add the code to a handler to return the image bytes with the appropriate mime-type. Then you can just add the url to your handler like it is an image. For example:  </p>\n\n<pre><code>&lt;img src="myhandler.ashx?imageid=5"&gt;  \n</code></pre>\n\n<p>Make sense?</p>\n'}, {'answer_id': 2310342, 'author': 'Taliesin', 'author_id': 124968, 'author_profile': 'https://Stackoverflow.com/users/124968', 'pm_score': 3, 'selected': False, 'text': '<p>You don\'t want to be serving blobs from a database without implementing client side caching. </p>\n\n<p>You will need to handle the following headers to support client side caching:</p>\n\n<ul>\n<li>ETag</li>\n<li>Expires</li>\n<li>Last-Modified</li>\n<li>If-Match</li>\n<li>If-None-Match</li>\n<li>If-Modified-Since</li>\n<li>If-Unmodified-Since</li>\n<li>Unless-Modified-Since </li>\n</ul>\n\n<p>For an http handler that does this, check out:\n<a href="http://code.google.com/p/talifun-web/wiki/StaticFileHandler" rel="nofollow noreferrer">http://code.google.com/p/talifun-web/wiki/StaticFileHandler</a></p>\n\n<p>It has a nice helper to serve the content. It should be easy to pass in database stream to it. It also does server side caching which should help alleviate some of the pressure on the database. </p>\n\n<p>If you ever decide to serve streaming content from the database, pdfs or large files the handler also supports 206 partial requests. </p>\n\n<p>It also supports gzip and deflate compression.</p>\n\n<p>These file types will benefit from further compression:</p>\n\n<ul>\n<li>css, js, htm, html, swf, xml, xslt, txt</li>\n<li>doc, xls, ppt </li>\n</ul>\n\n<p>There are some file types that will not benefit from further compression:</p>\n\n<ul>\n<li>pdf (causes problems with certain versions in IE and it is usually well compressed)</li>\n<li>png, jpg, jpeg, gif, ico</li>\n<li>wav, mp3, m4a, aac (wav is often compressed)</li>\n<li>3gp, 3g2, asf, avi, dv, flv, mov, mp4, mpg, mpeg, wmv</li>\n<li>zip, rar, 7z, arj </li>\n</ul>\n'}, {'answer_id': 19257098, 'author': 'Fidel Orozco', 'author_id': 1260174, 'author_profile': 'https://Stackoverflow.com/users/1260174', 'pm_score': 2, 'selected': False, 'text': '<p>Using ASP.Net with MVC this is pretty  forward easy. You code a controller with a method like this:</p>\n\n<pre><code>public FileContentResult Image(int id)\n{\n    //Get data from database. The Image BLOB is return like byte[]\n    SomeLogic ItemsDB= new SomeLogic("[ImageId]=" + id.ToString());\n    FileContentResult MyImage = null;\n    if (ItemsDB.Count &gt; 0)\n    {\n        MyImage= new FileContentResult(ItemsDB.Image, "image/jpg");\n    }\n\n    return MyImage;\n}\n</code></pre>\n\n<p>In your ASP.NET Web View or in this example, in your ASP.NET Web Form you can fill an Image Control with the URL to your method like this:</p>\n\n<pre><code>            this.imgExample.ImageUrl = "~/Items/Image/" + MyItem.Id.ToString();\n            this.imgExample.Height = new Unit(120);\n            this.imgExample.Width = new Unit(120);\n</code></pre>\n\n<p>Voilá. Not HttpModules hassle was needed.</p>\n'}]
['https://Stackoverflow.com/questions/21877', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/932/']