Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Stream to display Image *.ico

Tags:

c#

stream

asp.net

I want to show image with extension *.ico and I use Stream to do it. But I have a problem that.

With Extension *.jpg, *.bmp... Image show ok but *.ico, it does not show

Here is my code:

 private void OutputStream(string fileName)
    {
        try
        {
            Stream dataStream = null;

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPFile spFile = web.GetFile(fileName);
                    dataStream = spFile.OpenBinaryStream();
                    this.HandleOutputStream(dataStream);
                });

        }
        catch (System.Threading.ThreadAbortException exTemp)
        {
            logger.Error("KBStreamPicture::OutputStream", exTemp);
        }
        catch (Exception ex)
        {
            //System.Diagnostics.Debug.Write("OutputStream::" + ex);
            logger.Error("KBStreamPicture::OutputStream", ex);
        }
    }

and

private void HandleOutputStream(Stream dataStream)
    {
        const int bufferSize = 16384; //16KB 

        using (Stream file = dataStream)
        {
            if (file != null)
            {
                this.Page.Response.Clear();
                this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
                this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20)); //only cache 20 minutes
                byte[] buffer = new byte[bufferSize];
                int count = file.Read(buffer, 0, bufferSize);

                while (count > 0)
                {
                    if (this.Page.Response.IsClientConnected)
                    {
                        this.Page.Response.OutputStream.Write(buffer, 0, count);
                        count = file.Read(buffer, 0, bufferSize);
                    }
                    else
                    {
                        count = -1;
                    }
                }
                this.Page.Response.End();
            }
        }
    }

Please help me to resolve that problem.

like image 644
user1186850 Avatar asked Oct 09 '22 12:10

user1186850


1 Answers

You're not setting the ContentType property on the Response. Try setting the ContentType to image/x-icon (please note that the "correct" content type may be image/vnd.microsoft.icon, but this post seems to indicate you may run into problems with that type).

The code should look something like:

this.Page.Response.Clear();
this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20));
this.Page.Response.ContentType = "image/x-icon";
like image 71
rsbarro Avatar answered Oct 12 '22 11:10

rsbarro