Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# 将ImageUniqueID插入JPEG';s EXIF数据,而不使用.NET修改图像_C#_Exif_Imagesharp - Fatal编程技术网

C# 将ImageUniqueID插入JPEG';s EXIF数据,而不使用.NET修改图像

C# 将ImageUniqueID插入JPEG';s EXIF数据,而不使用.NET修改图像,c#,exif,imagesharp,C#,Exif,Imagesharp,我需要在不修改图像内容的情况下将唯一ID插入到图像文件中,即我要修改的只是元数据。我从JPEG文件开始,因为有一个合适的EXIF属性可用:ImageUniqueID 我正在使用C#with.NETCore3.1进行此练习。 我可以使用以下代码使用ImageSharp更改EXIF数据(显示简化,无需现有记录检查等): 我确实在JPEG编码器中使用了质量设置,但仍然得到了不可接受的质量下降或文件大小增加 有没有一种方法可以在不影响图像的情况下读取元数据,修改它,然后将其写回 我也看过,但这没有写功能

我需要在不修改图像内容的情况下将唯一ID插入到图像文件中,即我要修改的只是元数据。我从JPEG文件开始,因为有一个合适的EXIF属性可用:ImageUniqueID

我正在使用C#with.NETCore3.1进行此练习。 我可以使用以下代码使用ImageSharp更改EXIF数据(显示简化,无需现有记录检查等):

我确实在JPEG编码器中使用了质量设置,但仍然得到了不可接受的质量下降或文件大小增加

有没有一种方法可以在不影响图像的情况下读取元数据,修改它,然后将其写回


我也看过,但这没有写功能,我很乐意看看其他.NET核心方法或库。

经过一些研究,我发现有一些方法只允许修改图像元数据。 (包括示例) 如何为jpg文件添加唯一图像id的示例:

var file = ImageFile.FromFile("path_to_jpg_file");
var imageUniqueIdentifier = Guid.NewGuid().ToString().ToLower().Replace("-", "");
file.Properties.Set(ExifLibrary.ExifTag.ImageUniqueID, imageUniqueIdentifier);

file.Save("path_to_jpg_file");

Nuget软件包:ExifLibNet。

以下是一些只需要.NET与PresentationCore和WindowsBase配合使用的代码。WPF使用的底层技术是WIC()。WIC全力支持这项计划

EXIF的ImageUniqueID专门作为名为

如果添加相应的列“Camera Model”,则可以在Windows资源管理器详细视图中直接看到其他一些属性,如System.Photo.CameraModel,而不是System.Image.ImageID、AFAIK

// needs PresentationCore & WindowsBase references
var frame = BitmapDecoder.Create(new Uri("test1.jpg", UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None).Frames[0];

// create encoder, add frame, we need to copy since we want to update metadata
var encoder = BitmapEncoder.Create(frame.Decoder.CodecInfo.ContainerFormat);
var copy = BitmapFrame.Create(frame);

// get frame metadata
var metaData = (BitmapMetadata)copy.Metadata;

// show existing System.Image.ImageID (if any)
Console.WriteLine("ImageUniqueID: " + metaData.GetQuery("System.Image.ImageID"));

// for some reason, we can't use "System.Image.ImageID" to set the meta data
// so use the "Metadata Query Language"
metaData.SetQuery("/app1/ifd/exif/{ushort=42016}", "My Super ID");

// write file back
encoder.Frames.Add(copy);
using (var stream = File.OpenWrite("test1copy.jpg"))
{
    encoder.Save(stream);
}

我看了一眼这个图书馆,乍一看,它似乎满足了我的需要。在原始测试图像和修改后的图像之间使用Beyond Compare进行的测试表明,正确插入了图像唯一ID。然而,其他一些属性也发生了变化,尽管我仍在努力确定它们是否重要。它们是GPS偏移量(8825)、互操作性偏移量(A005)、场景类型(A301)和JIF偏移量(0201)。你真的需要imagesharp还是WPF可以?如果你有1000美元,你可以试试这个。它似乎做了你想做的:。或者我使用ImageSharp的@SimonMourier解决方案之一,因为我认为它可能会起作用。我当时正在处理要上传到web上的文件,希望给它们一个唯一的嵌入ID,这样,如果文件名因任何原因发生更改,我仍然可以找到它。我之所以这样问,是因为有一个仅适用于.NET的解决方案,我没有在asp.NET中进行测试,但它可能会工作(不使用任何桌面UI)。我很高兴有一个.NET解决方案,但它是否局限于Windows演示基金会?我正在努力寻找一种在.NET核心5控制台项目中引用WIC的方法(我只是在使用一个控制台项目进行实验)。我试着加快速度,但是它对C++来说有点太快了C++,我已经几十年没做C++了。我的VisualStudio安装包括.NET桌面开发,但不是C++桌面开发。如果我能帮助的话,我真的不想安装后者。@CrispinH-您不必使用WIC本身。如果您使用的是.NET Core控制台应用程序,代码是相同的,只需确保您的csproj中有此代码:
net5.0-windowsdisablewinexeoutputineference>truetrue
,该程序现已运行,但与原始程序相比,某些参数已发生更改:Exif偏移(8769)、GPS偏移(8825)、互操作性偏移(A005)、JIF偏移(0201)和未知(E1AD)补充。我猜最后一个可能无关紧要。至于其他的,我将尝试花一些时间阅读这些的原始值,并可能通过编程方式将它们设置回原来的值。
// needs PresentationCore & WindowsBase references
var frame = BitmapDecoder.Create(new Uri("test1.jpg", UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None).Frames[0];

// create encoder, add frame, we need to copy since we want to update metadata
var encoder = BitmapEncoder.Create(frame.Decoder.CodecInfo.ContainerFormat);
var copy = BitmapFrame.Create(frame);

// get frame metadata
var metaData = (BitmapMetadata)copy.Metadata;

// show existing System.Image.ImageID (if any)
Console.WriteLine("ImageUniqueID: " + metaData.GetQuery("System.Image.ImageID"));

// for some reason, we can't use "System.Image.ImageID" to set the meta data
// so use the "Metadata Query Language"
metaData.SetQuery("/app1/ifd/exif/{ushort=42016}", "My Super ID");

// write file back
encoder.Frames.Add(copy);
using (var stream = File.OpenWrite("test1copy.jpg"))
{
    encoder.Save(stream);
}