使用流显示图像*.ico

本文关键字:图像 ico 显示图 显示 | 更新日期: 2023-09-27 18:26:08

我想用扩展名*.ico显示图像,我用Stream来做。但我有一个问题。

带扩展*.jpg*.bmp。。。图像显示正常,但*.ico,不显示

这是我的代码:

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

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

请帮我解决那个问题。

使用流显示图像*.ico

您没有在Response上设置ContentType属性。尝试将ContentType设置为image/x-icon(请注意,"正确"的内容类型可能是image/vnd.microsoft.icon,但这篇文章似乎表明您可能会遇到该类型的问题)。

代码应该看起来像:

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