-->

为什么我在图像的开始不需要额外的字节?(Why do I have unwanted extra b

2019-07-19 04:17发布

题:

我做了我的兄弟,在这里访问的网页:
http://www.daniel-steiger.ch

它采用微软ASP.NET MVC3在Linux上用的nginx(加上我自己的DNS服务器)单3,过度的FastCGI。

现在,我知道这是一个不寻常的星座,但到目前为止,这一切工作正常。
但是,我遇到了一个小的非常微妙的错误。

当在库一个点击的缩略图图像上,我想要经由库控制器的一个新标签的FullImage方法显示全尺寸图像。
例如,这种直接的网址:
http://www.daniel-steiger.ch/gallery/FullImage/001.jpg


在Internet Explorer中,我得到的图像文字。
在所有其他浏览器显示“无效图像”的消息。
我通过调用图像通过它的直接的文件URL,它工作得很好解决了这个问题:
http://www.daniel-steiger.ch/Content/images/gallery/001.jpg?LastWriteTimeUTC=1358694795000

随后,我报告的错误的单声道邮件列表
http://mono.1490590.n4.nabble.com/Bug-in-mono-3-0-1-MVC3-File-FileResult-td4658382.html

现在,我得到的回应,这是我的错,因为我设置了错误的图像MIME类型,这是/是真实的。
不过,我觉得很奇怪,如果是这样的话,同样的代码工作正常在Windows上,再加上Chrome等浏览器的智能检测,通常一个错误的MIME设置,并使用正确的。

所以我改变了MIME类型的“图像/ JPG”到“图像/ JPEG”,并重新部署该项目到服务器。
我还检查与文件工具如果图像实际上是一个JPEG图像,它是。

奇怪的是,它仍然不显示图像。
在Internet Explorer,我现在得到“不可用”。
在所有其他浏览器,我得到:无法显示图像,因为它包含错误。

我现在都从那里图像包含错误的URL wget的图像。
wget的http://www.daniel-steiger.ch/gallery/fullimage/001.jpg

然后我跑了无效和原始文件之间的二进制比较:

cmp -l 001.jpg 001a.jpg   | awk '{printf "%08X %02X %02X\n", $1, strtonum(0$2), strtonum(0$3)}' >> comparison.txt

这是比较结果:

什么弹入我的眼睛,是的Internet Explorer说,他找不到图像的大小实际上1.7 MB,并包含额外的字节:

31 39 36 62 36 38 0D 0A 

在开始...
任何人有一个想法,这是怎么回事错在这里/那里这些字节可能(从事实,这是最有可能从一个bug在单/ FastCGI的间隔)从何而来?

这是新的控制器代码BTW:

namespace Homepage.Controllers
{


    public class GalleryController : Controller
    {


        protected static string GetImageDirectory()
        {
            string bd = AppDomain.CurrentDomain.BaseDirectory;
            string strImageDirectory = System.IO.Path.Combine(bd,
            "Content");
            strImageDirectory =
            System.IO.Path.Combine(strImageDirectory, "images");
            strImageDirectory =
            System.IO.Path.Combine(strImageDirectory, "gallery");

            return strImageDirectory;
        } // End Function GetImageDirectory


        protected static string strImageDirectory = GetImageDirectory();


        public FileResult FullImage(string id)
        {
            string strFileName =
            System.IO.Path.Combine(strImageDirectory, id);

            //return new FilePathResult("CorrectFullPathAndFileName", "CorrectMime");
            //return File(strFileName, "image/jpg"); // Old
            return File(strFileName, "image/jpeg"); // New
        } // End Action FullImage



        public FileResult Thumb(string id)
        {
            //return Redirect(id);

            string strFileName =
            System.IO.Path.Combine(strImageDirectory, id);

            System.IO.Stream ms =
            Tools.Imaging.GetThumbnailStream(strFileName,
            System.Drawing.Imaging.ImageFormat.Png);
            return File(ms, "image/png");
            /*
            using (System.IO.Stream ms =
            Tools.Imaging.GetThumbnailStream(strFileName,
            System.Drawing.Imaging.ImageFormat.Png))
            {
                return File(ms, "image/png");
            }*/
        } // End Action Thumb


    } // End Class GalleryController : Controller


} // End Namespace Homepage.Controllers

编辑:
它得到的陌生人:
还有一个额外的序列

0d 0a 0d 0a 30 0d 0a 0d  0a 

在末尾...

我只是做了hexdump都可以,这才发现,原来的文件有文件大小(屏住呼吸):

00196b68

这里的hexdumps(8 MB的每个):

规范格式(hexdump都-C 001.JPG> 001.txt):
原始文件: http://www.daniel-steiger.ch/001.txt
拙劣的文件: http://www.daniel-steiger.ch/001a.txt

纯转储(hexdump都001.JPG> 001_1.txt):
原始文件: http://www.daniel-steiger.ch/001_1.txt
拙劣的文件: http://www.daniel-steiger.ch/001a_1.txt

嗯,对于拙劣文件十六进制转储为5 MB,一个为原来8.2 MB ...

我用的是最新的稳定nginx的:

sudo -s
nginx=stable # use nginx=development for latest development version
add-apt-repository ppa:nginx/$nginx
apt-get update 
apt-get install nginx

对于Tools.Imaging完整的代码(在此期间改名工具MvcTools,以配合大会的命名空间)

using System;
using System.Text;


namespace MvcTools
{


    public class Imaging
    {

        //public static System.Drawing.Size m_sMaxThumbNailDimensions = new System.Drawing.Size(200, 200);
        public static System.Drawing.Size m_sMaxThumbNailDimensions = new System.Drawing.Size(300, 300);

        public static System.IO.Stream GetImageAsStream(
                                                 string strOrgFileName
                                                , System.Drawing.Imaging.ImageFormat ifOutputFormat
                                           )
        {
            return GetImageAsStream(strOrgFileName, ifOutputFormat, 1024);
        } // End Function GetImage


        public static System.IO.Stream GetImageAsStream(
                                                 string strOrgFileName
                                                ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                                ,int rez
                                           )
        {
            System.IO.MemoryStream ms = null;

            if (!System.IO.File.Exists(strOrgFileName))
                throw new System.IO.FileNotFoundException(strOrgFileName);

            try
            {

                using (System.Drawing.Image imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {

                    ms = new System.IO.MemoryStream();
                    imgSourceImage.Save(ms, ifOutputFormat);
                    ms.Position = 0;
                } // End Using imgSourceImage

            } // End Try
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.GetType().ToString());
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } // End Catch

            return ms;
        } // End Function GetImageAsStream


        public static System.Drawing.Size GetThumbnailSize(string strOrgFileName)
        {
            System.Drawing.Size sThumbNailSizeToUse = new System.Drawing.Size();

            try
            {

                using (System.Drawing.Image imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    decimal decPixToSubstract = 0;
                    decimal decPercentage;

                    if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    {
                        if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Width - (decimal)m_sMaxThumbNailDimensions.Width) / (decimal)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (int)decPixToSubstract;
                        } // End if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        else
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Height - (decimal)m_sMaxThumbNailDimensions.Height) / (decimal)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage * (decimal)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (int)decPixToSubstract;
                        } // End else of if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)

                    } // End if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    else
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } // End else of if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)

                } // End Using imgSourceImage

            } // End Try
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } // End Catch

            return sThumbNailSizeToUse;
        } // End Sub GetThumbnailSize(string strOrgFileName)


        // http://stackoverflow.com/questions/7319842/mvc3-razor-thumbnail-resize-image-ideas
        // http://stackoverflow.com/questions/1528525/alternatives-to-system-drawing-for-use-with-asp-net/1528908#1528908
        public static void GenerateThumbnailFile(
                                        string strPhysicalPath,
                                        string strOrgFileName, string strThumbnailFileName,
                                        System.Drawing.Imaging.ImageFormat ifOutputFormat, int rez
                                     )
        {

            try
            {

                using (System.Drawing.Image imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    //System.Drawing.Image oImg = System.Drawing.Image.FromStream(fil.InputStream);

                    decimal decPixToSubstract = 0;
                    decimal decPercentage;

                    //default
                    System.Drawing.Size sThumbNailSizeToUse = new System.Drawing.Size();
                    if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    {
                        if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Width - (decimal)m_sMaxThumbNailDimensions.Width) / (decimal)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (int)decPixToSubstract;
                        } // End if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        else
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Height - (decimal)m_sMaxThumbNailDimensions.Height) / (decimal)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage * (decimal)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (int)decPixToSubstract;
                        } // End else of if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)

                    } // End if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    else
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } // End else of if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)

                    using (System.Drawing.Bitmap bmpThumbnail = new System.Drawing.Bitmap(sThumbNailSizeToUse.Width, sThumbNailSizeToUse.Height))
                    {
                        bmpThumbnail.SetResolution(rez, rez);
                        using (System.Drawing.Image imgThumbNail = bmpThumbnail)
                        {

                            using (System.Drawing.Graphics gGraphicsContext = System.Drawing.Graphics.FromImage(imgThumbNail))
                            {
                                gGraphicsContext.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                gGraphicsContext.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                gGraphicsContext.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                                System.Drawing.Rectangle rThumbnailDimension = new System.Drawing.Rectangle(0, 0, sThumbNailSizeToUse.Width, sThumbNailSizeToUse.Height);
                                gGraphicsContext.DrawImage(imgSourceImage, rThumbnailDimension);
                            } // End Using gGraphicsContext

                            imgThumbNail.Save(System.IO.Path.Combine(strPhysicalPath, strThumbnailFileName), ifOutputFormat);
                        } // End Using imgThumbNail

                    } // End Using bmpThumbnail

                } // End Using imgSourceImage

            } // End Try
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } // End Catch

        } // End Function GenerateThumbNail


        public static System.IO.Stream GetThumbnailStream(
                                                               string strOrgFileName
                                                             , System.Drawing.Imaging.ImageFormat ifOutputFormat
                                                         )
        {
            return GetThumbnailStream(strOrgFileName, ifOutputFormat, 1024);
        } // End Function GetThumbnailStream


        public static System.IO.Stream GetThumbnailStream(
                                      string strOrgFileName
                                     ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                     ,int rez
                                  )
        {

            System.IO.MemoryStream ms = null;

            try
            {

                using (System.Drawing.Image imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    decimal decPixToSubstract = 0;
                    decimal decPercentage;

                    System.Drawing.Size sThumbNailSizeToUse = new System.Drawing.Size();
                    if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    {
                        if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Width - (decimal)m_sMaxThumbNailDimensions.Width) / (decimal)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (int)decPixToSubstract;
                        } // End if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)
                        else
                        {
                            decPercentage = (((decimal)imgSourceImage.Size.Height - (decimal)m_sMaxThumbNailDimensions.Height) / (decimal)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage * (decimal)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (int)decPixToSubstract;
                        } // End else of if (imgSourceImage.Size.Width > imgSourceImage.Size.Height)

                    } // End if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)
                    else
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } // End else of if (m_sMaxThumbNailDimensions.Width < imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height < imgSourceImage.Size.Height)


                    using (System.Drawing.Bitmap bmpThumbnail = new System.Drawing.Bitmap(sThumbNailSizeToUse.Width, sThumbNailSizeToUse.Height))
                    {
                        bmpThumbnail.SetResolution(rez, rez);
                        using (System.Drawing.Image imgThumbNail = bmpThumbnail)
                        {

                            using (System.Drawing.Graphics gGraphicsContext = System.Drawing.Graphics.FromImage(imgThumbNail))
                            {
                                gGraphicsContext.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                gGraphicsContext.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                gGraphicsContext.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                                System.Drawing.Rectangle rThumbnailDimension = new System.Drawing.Rectangle(0, 0, sThumbNailSizeToUse.Width, sThumbNailSizeToUse.Height);
                                gGraphicsContext.DrawImage(imgSourceImage, rThumbnailDimension);

                                ms = new System.IO.MemoryStream();
                                imgThumbNail.Save(ms, ifOutputFormat);
                                ms.Position = 0;
                            } // End Using gGraphicsContext

                        } // End Using imgThumbNail

                    } // End Using bmpThumbnail


                    /*
                    byte[] buffer = null;
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        imgThumbNail.Save(ms, ifOutputFormat);
                        buffer = ms.ToArray();
                    }
                    */

                    // Exerts from Page_Load method 
                    //Response.ContentType = "image/" + extension;
                    //Response.OutputStream.Write(pBuffer, 0, pBuffer.Length);
                    //Response.End();

                    //imgThumbNail.Save(System.IO.Path.Combine(strPhysicalPath, strThumbnailFileName), ifOutputFormat);
                } // End Using imgSourceImage

            } // End Try
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } // End Catch

            //System.Windows.Forms.MessageBox.Show("image/" + ifOutputFormat.ToString().ToLower());
            return ms;
        } // End Function GenerateThumbNail


    } // End Class Imaging


} // End Namespace Tools

Answer 1:

这似乎是涉及错误Chuncked传输编码在

Class: System.Web.HttpResponse (or one of its dependencies)
Method: TransmitFile(string filename)

编辑:
有了这个代码在构造函数:

if (worker_request != null)
      use_chunked = (worker_request.GetHttpVersion () == "HTTP/1.1");

修补它来检查CGI(如果CGI,服务器处理的文件传输,所以有可能是从给定的FastCGI的服务器返回按不chuncked编码RFC 3875 。

        internal HttpResponse (HttpWorkerRequest worker_request, HttpContext context) : this ()
        {
            WorkerRequest = worker_request;
            this.context = context;

#if !TARGET_J2EE
            if (worker_request != null)
            {

                if(worker_request.GetHttpVersion () == "HTTP/1.1")
                {
                    string GatewayIface = context.Request.ServerVariables["GATEWAY_INTERFACE"];
                    use_chunked = (GatewayIface == null || !GatewayIface.StartsWith("CGI"));
                }
                else
                    use_chunked = false;

            }
#endif
            writer = new HttpWriter (this);
        }

添加补丁https://bugzilla.xamarin.com/show_bug.cgi?id=10001
修正了在单3.2.3



Answer 2:

31 39 36 62 36 38 0D 0A

196b68/r/n以ASCII。

  • 没有这种颜色一些如何与您的代码?
  • 不串196b68/r/n说一些关系到你的平台?


Answer 3:

我不承认Tools.Imaging.GetThumbnailStream()函数,而且也没有谷歌。 假设这是你自己的代码或第三方库,我想看看那里的怪字节流的来源。



文章来源: Why do I have unwanted extra bytes at the beginning of image?