Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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# .NET Exif信息在生产服务器上不起作用_C#_Asp.net_Image Processing_Exif - Fatal编程技术网

C# .NET Exif信息在生产服务器上不起作用

C# .NET Exif信息在生产服务器上不起作用,c#,asp.net,image-processing,exif,C#,Asp.net,Image Processing,Exif,我正在读取JPEG的Exif信息以旋转图像。JPEG上传到ASP.NET,我读取上传流,旋转并保存。它在我的开发机器(Windows 10、IIS 10)上运行得很好,但当我在服务器(Windows server 2012 R2、IIS 8.5)上尝试时,它不工作,也不加载任何Exif信息 代码如下: void SavePhoto() { // PHOTO is the Html HttpPostedFile photo = Request.Files["ProfilePhoto

我正在读取JPEG的Exif信息以旋转图像。JPEG上传到ASP.NET,我读取上传流,旋转并保存。它在我的开发机器(Windows 10、IIS 10)上运行得很好,但当我在服务器(Windows server 2012 R2、IIS 8.5)上尝试时,它不工作,也不加载任何Exif信息

代码如下:

void SavePhoto()
{
    // PHOTO is the Html
    HttpPostedFile photo = Request.Files["ProfilePhoto_File"];
    using (var image = Image.FromStream(photo.InputStream, true, true))
    {
        SaveConvertingFormat(image, "output_path.jpg");
    }
}

public static void SaveConvertingFormat(Image image, string outputPath)
{
    int imageWidth = image.Width;
    int imageHeight = image.Height;

    using (var result = new Bitmap(imageWidth, imageHeight))
    {
        using (var g = Graphics.FromImage(result))
        {
            g.DrawImage(image, 0, 0, imageWidth, imageHeight);
        }

        var rotation = GetExifRotate(image, outputPath);
        // IN THE SERVER, rotation IS ALWAYS RotateNoneFlipNone
        if (rotation != RotateFlipType.RotateNoneFlipNone)
            result.RotateFlip(rotation);

        SaveJpeg(result, outputPath, 85);
    }
}

private static void SaveJpeg(this Image img, string filename, int quality)
{
    EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)quality);
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qualityParam;
    img.Save(filename, jpegCodec, encoderParams);
}

public static RotateFlipType GetExifRotate(Image img, string outputPath)
{
    // Source: https://stackoverflow.com/a/48347653/72350
    // ERROR:
    // IN THE PRODUCTION SERVER, PropertyIdList IS EMPTY!
    const int ExifOrientationId = 0x112;
    if (!img.PropertyIdList.Contains(ExifOrientationId))
        return RotateFlipType.RotateNoneFlipNone;

    var prop = img.GetPropertyItem(ExifOrientationId);
    int val = BitConverter.ToUInt16(prop.Value, 0);
    var rot = RotateFlipType.RotateNoneFlipNone;

    if (val == 3 || val == 4)
        rot = RotateFlipType.Rotate180FlipNone;
    else if (val == 5 || val == 6)
        rot = RotateFlipType.Rotate90FlipNone;
    else if (val == 7 || val == 8)
        rot = RotateFlipType.Rotate270FlipNone;

    if (val == 2 || val == 4 || val == 5 || val == 7)
        rot |= RotateFlipType.RotateNoneFlipX;

    return rot;
}
同样,上面的代码:

  • 有效:Windows 10、IIS 10
  • 不工作:Windows Server 2012 R2,IIS 8.5

有什么建议吗?

以防有人有同样的问题。我在使用WFP和GDI阅读产品介绍时遇到问题

使用WPF时,错误为:

System.Runtime.InteropServices.COMException (0x88982F8A): The component registration is invalid.
(Exception from HRESULT: 0x88982F8A)
at System.Windows.Media.Imaging.BitmapMetadata.GetQuery(String query)
解决方案:

唯一有效的方法是使用:

代码相当简单:

var img = new MagickImage(inputStream);
img.AutoOrient();   // Fix orientation
img.Strip();        // remove all EXIF information
img.Write(outputPath);

它还帮助我删除了10行。

没有必要删除EXIF数据。AutoOrient()自动将EXIF方向设置为左上角

您还需要使用using子句,因为MagickImage实现了IDisposable

using (var img = new MagickImage(inputStream))
{
    img.AutoOrient();   // Fix orientation
    img.Write(outputPath);
}

这和公认的答案是一样的。无论如何,谢谢你。