Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在NETMVC中写入图像元数据会导致文件大小的更改_C#_Asp.net Mvc 3_Bitmap - Fatal编程技术网

C# 在NETMVC中写入图像元数据会导致文件大小的更改

C# 在NETMVC中写入图像元数据会导致文件大小的更改,c#,asp.net-mvc-3,bitmap,C#,Asp.net Mvc 3,Bitmap,我尝试在mvc3应用程序中修改少数上传图像文件的元数据信息,发现对于tiff图像,文件大小增加了90%[实际文件大小为4 MB,更新元数据后,我发现大小为9 MB],问题似乎仅限于tiff图像。我尝试了其他图像格式,如png、jpeg、bmp等,在更新元数据后没有大小问题。 下面是我用来保存元数据[修改元数据]的代码 BitmapDecoder decoder = null; BitmapFrame bitmapFrame = null; BitmapMetada

我尝试在mvc3应用程序中修改少数上传图像文件的元数据信息,发现对于tiff图像,文件大小增加了90%[实际文件大小为4 MB,更新元数据后,我发现大小为9 MB],问题似乎仅限于tiff图像。我尝试了其他图像格式,如png、jpeg、bmp等,在更新元数据后没有大小问题。 下面是我用来保存元数据[修改元数据]的代码

        BitmapDecoder decoder = null;
    BitmapFrame bitmapFrame = null;
    BitmapMetadata metadata = null;
    BitmapEncoder encoder = null;            
    using (Stream bitmapStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
    {
        //This combination seems to work. The BitmapCacheOption.onload argument to BitmapDecoder.Create() is the only combination that seems to work. Otherwise, we get a file in use if we try to accessit from another thread.                    
        decoder = BitmapDecoder.Create(bitmapStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        bitmapStream.Close();
    }
    if (decoder != null)
    {
        bitmapFrame = decoder.Frames[0];
        if (bitmapFrame != null && bitmapFrame.Metadata != null)
        {
            // clone the image file's metadata                        
            metadata = (BitmapMetadata)bitmapFrame.Metadata.Clone();
            metadata.Title = "";//value here
            metadata.Comment = "";//value here
            metadata.Author = "";//value here
            metadata.Copyright = "";//value here
            metadata.Keywords = "";//value here 
            switch (mymethods.getMimeType(filename))
            {
                case "image/jpeg":
                    encoder = new JpegBitmapEncoder();
                    break;
                case "image/png":
                    encoder = new PngBitmapEncoder();
                    break;
                case "image/tiff":
                    encoder = new TiffBitmapEncoder();
                    break;
                case "image/gif":
                    encoder = new GifBitmapEncoder();
                    break;
                case "image/bmp":
                    encoder = new BmpBitmapEncoder();
                    break;
                default:
                    encoder = new PngBitmapEncoder();
                    break;
            }
            encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts));
            using (Stream outStream = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                encoder.Save(outStream);
            }
        }
    }

此代码片段帮助我修改元数据,但唯一的问题是重新调整少数格式的已保存图像的大小

尝试使用编码器。压缩属性-在保存保存图像的最后一位代码后,不可能仍然获得双倍大小,只是将更新的元数据图像添加到现有图像中吗(因此是双倍大小)?打开图像并检查页数。DPI增加也可能是问题所在。你能帮助优化这个吗?如果我上面提到的事情真的发生了(我想是的),在“tiff c#中的overwite frames”上进行谷歌搜索。我以前从未这样做过,但这个概念似乎足够简单。或者您可以简单地创建一个新的tiff,然后在保存后删除原始tiff并更新新的tiff名称以匹配。