C#:WPD - 使用元标记下载图片

时间:2017-03-03 01:43:52

标签: jpeg meta-tags exif smartphone wpd

我正在运行便携式设备API以自动从连接的智能手机获取照片。我把它全部正确地转移了。我使用的代码是Standard DownloadFile()例程:

    public PortableDownloadInfo DownloadFile(PortableDeviceFile file, string saveToPath)
    {
        IPortableDeviceContent content;
        _device.Content(out content);
        IPortableDeviceResources resources;
        content.Transfer(out resources);
        PortableDeviceApiLib.IStream wpdStream;
        uint optimalTransferSize = 0;
        var property = new _tagpropertykey
        {
            fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42),
            pid = 0
        };
        resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

        System.Runtime.InteropServices.ComTypes.IStream sourceStream =
            // ReSharper disable once SuspiciousTypeConversion.Global
            (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

        var filename = Path.GetFileName(file.Name);
        if (string.IsNullOrEmpty(filename))
            return null;

        FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename),
            FileMode.Create, FileAccess.Write);

        try
        {
            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, 1024);
                } while (bytesRead > 0);

                targetStream.Close();
            }
        }
        finally
        {
            Marshal.ReleaseComObject(sourceStream);
            Marshal.ReleaseComObject(wpdStream);
        }
        return pdi;
    }
}

此标准代码存在两个问题: 1) - 当图像保存到Windows机器时,没有EXIF信息。这些信息是我需要的。我该如何保存? 2)保存的文件非常臃肿。例如,源jpeg是1,045,807字节,而下载的文件是3,942,840字节!它类似于所有其他文件。我会认为不安全的{}部分内的一些会按字节输出吗?有更好的方法来传输数据吗? (一种安全的方式?)

1 个答案:

答案 0 :(得分:0)

对此感到抱歉。它运作正常..这是造成这些问题的其他因素