为什么我在图像的开头不需要额外的字节? [英] Why do I have unwanted extra bytes at the beginning of image?

查看:183
本文介绍了为什么我在图像的开头不需要额外的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:

我做了一个主页,我的兄弟,这里访问:

http://www.daniel-steiger.ch

它采用微软ASP.NET MVC3在Linux上单3,过nginx的(加上我自己的DNS服务器)的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类型,它是/是真实的。

不过,我觉得很奇怪,如果是这样的话,同样的code正常工作在Windows上,再加上Chrome等浏览器的智能检测,通常一个错误的哑剧设置,并使用正确的。

所以我改变了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

在开始......

任何人有一个想法是怎么回事错在这里/那里这些字节可以(从事实,这是最有可能从单声道/ FastCGI的错误除外)来自哪里?

这是新的控制器code顺便说一句:

 命名空间Homepage.Controllers
{
    公共类GalleryController:控制器
    {
        保护静态字符串GetImageDirectory()
        {
            字符串BD = AppDomain.CurrentDomain.BaseDirectory;
            字符串strImageDirectory = System.IO.Path.Combine(BD,
            内容);
            strImageDirectory =
            System.IO.Path.Combine(strImageDirectory,图像);
            strImageDirectory =
            System.IO.Path.Combine(strImageDirectory,画廊);            返回strImageDirectory;
        } //端功能GetImageDirectory
        保护静态字符串strImageDirectory = GetImageDirectory();
        公共FileResult FullImage(字符串ID)
        {
            串strFileName =
            System.IO.Path.Combine(strImageDirectory,身份证);            //返回新FilePathResult(CorrectFullPathAndFileName,CorrectMime);
            //返回文件(strFileName,图像/ JPG); //老
            返回文件(strFileName,图像/ JPEG); //新
        } //结束操作FullImage        公共FileResult拇指(字符串ID)
        {
            //返回重定向(ID);            串strFileName =
            System.IO.Path.Combine(strImageDirectory,身份证);            的System.IO.Stream毫秒​​=
            Tools.Imaging.GetThumbnailStream(strFileName,
            System.Drawing.Imaging.ImageFormat.Png);
            返回文件(MS,图像/ PNG);
            / *
            使用(的System.IO.Stream毫秒​​=
            Tools.Imaging.GetThumbnailStream(strFileName,
            System.Drawing.Imaging.ImageFormat.Png))
            {
                返回文件(MS,图像/ PNG);
            } * /
        } //结束操作拇指
    } //末级GalleryController:控制器
} //结束名称空间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的:

 须藤-s
nginx的稳定=#使用nginx的=发展的最新开发版
附加的apt-库PPA:nginx的/ $ nginx的
apt-get的更新
易于得到安装的nginx

满code为Tools.Imaging(重命名工具在此期间MvcTools,以配合大会命名空间)

 使用系统;
使用System.Text;
命名空间MvcTools
{
    公共类成像
    {        //公共静态System.Drawing.Size m_sMaxThumbNailDimensions =新System.Drawing.Size(200,200);
        公共静态System.Drawing.Size m_sMaxThumbNailDimensions =新System.Drawing.Size(300,300);        公共静态的System.IO.Stream GetImageAsStream(
                                                 串strOrgFileName
                                                ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                           )
        {
            返回GetImageAsStream(strOrgFileName,ifOutputFormat,1024);
        } //端功能的getImage
        公共静态的System.IO.Stream GetImageAsStream(
                                                 串strOrgFileName
                                                ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                                ,INT REZ
                                           )
        {
            System.IO.MemoryStream毫秒​​=无效;            如果(!System.IO.File.Exists(strOrgFileName))
                抛出新System.IO.FileNotFoundException(strOrgFileName);            尝试
            {                使用(System.Drawing.Image对象imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {                    MS =新System.IO.MemoryStream();
                    imgSourceImage.Save(MS,ifOutputFormat);
                    ms.Position = 0;
                } //结束使用imgSourceImage            } //结束Try
            赶上(异常前)
            {
                System.Windows.Forms.MessageBox.Show(ex.GetType()的ToString());
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } //到底抓            返回毫秒;
        } //端功能GetImageAsStream
        公共静态System.Drawing.Size GetThumbnailSize(字符串strOrgFileName)
        {
            System.Drawing.Size sThumbNailSizeToUse =新System.Drawing.Size();            尝试
            {                使用(System.Drawing.Image对象imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    小数decPixToSubstract = 0;
                    十进制decPercentage;                    如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    {
                        如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Width - (十进制)m_sMaxThumbNailDimensions.Width)/(十进制)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (INT)decPixToSubstract;
                        } // end如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        其他
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Height - (十进制)m_sMaxThumbNailDimensions.Height)/(十进制)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage *(十进制)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (INT)decPixToSubstract;
                        } //如果其他结束(imgSourceImage.Size.Width> imgSourceImage.Size.Height)                    } // end如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    其他
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } //如果其他结束(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)                } //结束使用imgSourceImage            } //结束Try
            赶上(异常前)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } //到底抓            返回sThumbNailSizeToUse;
        } //结束小组GetThumbnailSize(字符串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
        公共静态无效GenerateThumbnailFile(
                                        串strPhysicalPath,
                                        串strOrgFileName,串strThumbnailFileName,
                                        System.Drawing.Imaging.ImageFormat ifOutputFormat,INT REZ
                                     )
        {            尝试
            {                使用(System.Drawing.Image对象imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    //System.Drawing.Image oImg = System.Drawing.Image.FromStream(fil.InputStream);                    小数decPixToSubstract = 0;
                    十进制decPercentage;                    //默认
                    System.Drawing.Size sThumbNailSizeToUse =新System.Drawing.Size();
                    如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    {
                        如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Width - (十进制)m_sMaxThumbNailDimensions.Width)/(十进制)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (INT)decPixToSubstract;
                        } // end如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        其他
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Height - (十进制)m_sMaxThumbNailDimensions.Height)/(十进制)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage *(十进制)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (INT)decPixToSubstract;
                        } //如果其他结束(imgSourceImage.Size.Width> imgSourceImage.Size.Height)                    } // end如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    其他
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } //如果其他结束(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)                    使用(System.Drawing.Bitmap bmpThumbnail =新System.Drawing.Bitmap(sThumbNailSizeToUse.Width,sThumbNailSizeToUse.Height))
                    {
                        bmpThumbnail.SetResolution(苏亚雷斯,苏亚雷斯);
                        使用(System.Drawing.Image对象imgThumbNail = bmpThumbnail)
                        {                            使用(System.Drawing.Graphics gGraphicsContext = System.Drawing.Graphics.FromImage(imgThumbNail))
                            {
                                gGraphicsContext.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                gGraphicsContext.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.HighQuality;
                                gGraphicsContext.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                                System.Drawing.Rectangle rThumbnailDimension =新System.Drawing.Rectangle(0,0,sThumbNailSizeToUse.Width,sThumbNailSizeToUse.Height);
                                gGraphicsContext.DrawImage(imgSourceImage,rThumbnailDimension);
                            } //结束使用gGraphicsContext                            imgThumbNail.Save(System.IO.Path.Combine(strPhysicalPath,strThumbnailFileName),ifOutputFormat);
                        } //结束使用imgThumbNail                    } //结束使用bmpThumbnail                } //结束使用imgSourceImage            } //结束Try
            赶上(异常前)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } //到底抓        } //端功能GenerateThumbNail
        公共静态的System.IO.Stream GetThumbnailStream(
                                                               串strOrgFileName
                                                             ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                                         )
        {
            返回GetThumbnailStream(strOrgFileName,ifOutputFormat,1024);
        } //端功能GetThumbnailStream
        公共静态的System.IO.Stream GetThumbnailStream(
                                      串strOrgFileName
                                     ,System.Drawing.Imaging.ImageFormat ifOutputFormat
                                     ,INT REZ
                                  )
        {            System.IO.MemoryStream毫秒​​=无效;            尝试
            {                使用(System.Drawing.Image对象imgSourceImage = System.Drawing.Image.FromFile(strOrgFileName))
                {
                    小数decPixToSubstract = 0;
                    十进制decPercentage;                    System.Drawing.Size sThumbNailSizeToUse =新System.Drawing.Size();
                    如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    {
                        如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Width - (十进制)m_sMaxThumbNailDimensions.Width)/(十进制)imgSourceImage.Size.Width);
                            decPixToSubstract = decPercentage * imgSourceImage.Size.Height;
                            sThumbNailSizeToUse.Width = m_sMaxThumbNailDimensions.Width;
                            sThumbNailSizeToUse.Height = imgSourceImage.Size.Height - (INT)decPixToSubstract;
                        } // end如果(imgSourceImage.Size.Width> imgSourceImage.Size.Height)
                        其他
                        {
                            decPercentage =(((十进制)imgSourceImage.Size.Height - (十进制)m_sMaxThumbNailDimensions.Height)/(十进制)imgSourceImage.Size.Height);
                            decPixToSubstract = decPercentage *(十进制)imgSourceImage.Size.Width;
                            sThumbNailSizeToUse.Height = m_sMaxThumbNailDimensions.Height;
                            sThumbNailSizeToUse.Width = imgSourceImage.Size.Width - (INT)decPixToSubstract;
                        } //如果其他结束(imgSourceImage.Size.Width> imgSourceImage.Size.Height)                    } // end如果(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    其他
                    {
                        sThumbNailSizeToUse.Width = imgSourceImage.Size.Width;
                        sThumbNailSizeToUse.Height = imgSourceImage.Size.Height;
                    } //如果其他结束(m_sMaxThumbNailDimensions.Width< imgSourceImage.Size.Width || m_sMaxThumbNailDimensions.Height< imgSourceImage.Size.Height)
                    使用(System.Drawing.Bitmap bmpThumbnail =新System.Drawing.Bitmap(sThumbNailSizeToUse.Width,sThumbNailSizeToUse.Height))
                    {
                        bmpThumbnail.SetResolution(苏亚雷斯,苏亚雷斯);
                        使用(System.Drawing.Image对象imgThumbNail = bmpThumbnail)
                        {                            使用(System.Drawing.Graphics gGraphicsContext = System.Drawing.Graphics.FromImage(imgThumbNail))
                            {
                                gGraphicsContext.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                gGraphicsContext.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.HighQuality;
                                gGraphicsContext.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                                System.Drawing.Rectangle rThumbnailDimension =新System.Drawing.Rectangle(0,0,sThumbNailSizeToUse.Width,sThumbNailSizeToUse.Height);
                                gGraphicsContext.DrawImage(imgSourceImage,rThumbnailDimension);                                MS =新System.IO.MemoryStream();
                                imgThumbNail.Save(MS,ifOutputFormat);
                                ms.Position = 0;
                            } //结束使用gGraphicsContext                        } //结束使用imgThumbNail                    } //结束使用bmpThumbnail
                    / *
                    字节[]缓冲= NULL;
                    使用(System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream())
                    {
                        imgThumbNail.Save(MS,ifOutputFormat);
                        缓冲= ms.ToArray();
                    }
                    * /                    //从Page_Load方法作用Astragalus
                    //Response.ContentType =图像/+扩展名;
                    //Response.OutputStream.Write(pBuffer,0,pBuffer.Length);
                    //Response.End();                    //imgThumbNail.Save(System.IO.Path.Combine(strPhysicalPath,strThumbnailFileName),ifOutputFormat);
                } //结束使用imgSourceImage            } //结束Try
            赶上(异常前)
            {
                //Console.WriteLine(ex.Message);
                System.Windows.Forms.MessageBox.Show(ex.Message);
                //Response.Write(ex.Message);
            } //到底抓            //System.Windows.Forms.MessageBox.Show(\"image/+ ifOutputFormat.ToString()ToLower将())。
            返回毫秒;
        } //端功能GenerateThumbNail
    } //末级成像
} //结束名称空间工具


解决方案

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

 类:System.Web.Htt presponse(或它的一个依赖)
方法:的TransmitFile(字符串文件名)

编辑:

有一个在构造该code:

 如果(worker_request!= NULL)
      use_chunked =(worker_request.GetHttpVersion()==HTTP / 1.1);

修补它来检查CGI(CGI如果,服务器处理文件传输,因此可给予从FastCGI的服务器返回按的 RFC 3875

 内部的Htt presponse(的HttpWorkerRequest worker_request,HttpContext的背景下):这个()
        {
            WorkerRequest = worker_request;
            this.context =背景;#如果!TARGET_J2EE
            如果(worker_request!= NULL)
            {                如果(worker_request.GetHttpVersion()==HTTP / 1.1)
                {
                    字符串GatewayIface = context.Request.ServerVariables [GATEWAY_INTERFACE];
                    use_chunked =(GatewayIface == NULL || GatewayIface.StartsWith(CGI)!);
                }
                其他
                    use_chunked = FALSE;            }
#万一
            作家=新HttpWriter(本);
        }

添加补丁
https://bugzilla.xamarin.com/show_bug.cgi?id=10001

修正了单3.2.3

Question:

I've made a homepage for my brother, accessible here:
http://www.daniel-steiger.ch

It uses Microsoft ASP.NET MVC3 on Linux with mono 3, over fastcgi with nginx (plus my own DNS server).

Now I know this is an unusual constellation, but so far, it all works fine.
However, I ran into a minor very subtle bug.

When in the gallery one clicks on a thumbnail image, I wanted to display the full-sized image via the FullImage method of the gallery controller in a new tab.
For example, this direct url:
http://www.daniel-steiger.ch/gallery/FullImage/001.jpg


In internet Explorer, I got the image as text instead.
In all other browsers displayed an "invalid image" message.
I solved the problem by calling the image via it's direct file URL, which works fine:
http://www.daniel-steiger.ch/Content/images/gallery/001.jpg?LastWriteTimeUTC=1358694795000

Subsequently, I reported the bug to the mono mailing list
http://mono.1490590.n4.nabble.com/Bug-in-mono-3-0-1-MVC3-File-FileResult-td4658382.html

Now I got as a response, that this is all my fault, because I set the wrong image mime type, which is/was true.
However, I found it strange that if that was the case, the same code works fine on Windows, plus smart browsers like Chrome usually detect a wrong mime setting and use the correct one.

So I changed the mime-type from "image/jpg" to "image/jpeg", and redeployed the project to the server.
I also checked with the file utility if the image is actually a jpeg image, and it is.

What is strange is that it still doesn't show the image.
On internet explorer, I now get "not available".
On all other browsers, I get: the image cannot be displayed because it contains errors.

I did now wget the image from the URL where the image contains errors.
wget http://www.daniel-steiger.ch/gallery/fullimage/001.jpg

Then I ran a binary compare between the invalid and the original file:

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

And this is the comparison result:

What springs into my eye, is that the image that internet explorer says he cannot find is actually 1.7 MB in size, and contains the extra bytes:

31 39 36 62 36 38 0D 0A 

at the beginning...
Anybody has an idea what's going wrong here/ where these bytes could come from (apart from the fact that it's most likely from a bug in mono/fastcgi) ?

This is the new controller code 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

Edit:
it gets stranger:
There is also an extra sequence with

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

at the end...

I just did the hexdump, and found the original file has filesize (hold your breath):

00196b68

Here the hexdumps (8 MB each):

Canonical format (hexdump -C 001.jpg > 001.txt):
Original file: http://www.daniel-steiger.ch/001.txt
Botched file: http://www.daniel-steiger.ch/001a.txt

Pure dump (hexdump 001.jpg > 001_1.txt):
Original file: http://www.daniel-steiger.ch/001_1.txt
Botched file: http://www.daniel-steiger.ch/001a_1.txt

Hmmm, the hex dump for the botched files is 5 MB, the one for the original is 8.2 MB...

I use the latest stable 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

The full code for Tools.Imaging (renamed Tools to MvcTools in the meantime, to match the assembly namespace)

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

解决方案

This seems to be a bug involving Chuncked transfer encoding in

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

Edit:
There is this code in the constructor:

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

Patched it to check for CGI (if CGI, the server handles the file transfer, so there may be no chuncked encoding given back from the FastCGI server as per 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);
        }

Added patch to https://bugzilla.xamarin.com/show_bug.cgi?id=10001
Fixed in mono 3.2.3

这篇关于为什么我在图像的开头不需要额外的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆