Asp.net core 读取jpeg元数据asp.net core 3.1

Asp.net core 读取jpeg元数据asp.net core 3.1,asp.net-core,metadata,jpeg,exif,Asp.net Core,Metadata,Jpeg,Exif,我在用asp.net core 3.1实现一个网站时,试图从jpg文件中读取exif元数据,如标题、描述和日期。之前,我使用BitmapMetadata类完成了此操作,该类在asp.net core中不可用。我尝试过ExifLib和ExifLib.Standard NuGet包,但没有成功 在剃须刀页面上,我有 <form enctype="multipart/form-data" method="post"> <div class=

我在用asp.net core 3.1实现一个网站时,试图从jpg文件中读取exif元数据,如标题、描述和日期。之前,我使用BitmapMetadata类完成了此操作,该类在asp.net core中不可用。我尝试过ExifLib和ExifLib.Standard NuGet包,但没有成功

在剃须刀页面上,我有

<form enctype="multipart/form-data" method="post">
  <div class="form-group">
    <label asp-for="FormFile"></label>
    <input asp-for="FormFile" class="form-control-file" type="file" />
  </div>
  <input accept="image/jpeg" asp-page-handler="LoadInfo" class="btn btn-primary btn-sm" type="submit" value="Load Info" />
</form>
public IFormFile FormFile { get, set }
public string NewPhotoCaption { get, set }
public string NewPhotoDescription { get, set }
public DateTime NewPhotoDate { get, set }

public async Task<IActionResult> OnPostLoadInfoAsync()
{
  if (ModelState.IsValid)
  {
     using (ExifReader reader = new ExifReader(FormFile.OpenReadStream()))
     {
       reader.GetTagValue(ExifTags.XPTitle, out string newPhotoCaption);
       NewPhotoCaption = newPhotoCaption;
       reader.GetTagValue(ExifTags.XPDescription, out string newPhotoDescription);
       NewPhotoDescription = newPhotoDescription;
       reader.GetTagValue(ExifTags.DateTime, out DateTime newPhotoDate);
       NewPhotoDate = newPhotoDate;
     }
   }
   return Page();
 }

在剃须刀页面的模型中,我有

<form enctype="multipart/form-data" method="post">
  <div class="form-group">
    <label asp-for="FormFile"></label>
    <input asp-for="FormFile" class="form-control-file" type="file" />
  </div>
  <input accept="image/jpeg" asp-page-handler="LoadInfo" class="btn btn-primary btn-sm" type="submit" value="Load Info" />
</form>
public IFormFile FormFile { get, set }
public string NewPhotoCaption { get, set }
public string NewPhotoDescription { get, set }
public DateTime NewPhotoDate { get, set }

public async Task<IActionResult> OnPostLoadInfoAsync()
{
  if (ModelState.IsValid)
  {
     using (ExifReader reader = new ExifReader(FormFile.OpenReadStream()))
     {
       reader.GetTagValue(ExifTags.XPTitle, out string newPhotoCaption);
       NewPhotoCaption = newPhotoCaption;
       reader.GetTagValue(ExifTags.XPDescription, out string newPhotoDescription);
       NewPhotoDescription = newPhotoDescription;
       reader.GetTagValue(ExifTags.DateTime, out DateTime newPhotoDate);
       NewPhotoDate = newPhotoDate;
     }
   }
   return Page();
 }
public-FormFile-FormFile{get,set}
公共字符串NewPhotoCaption{get,set}
公共字符串NewPhotoDescription{get,set}
public DateTime NewPhotoDate{get,set}
OnPostLoadInfoAsync()上的公共异步任务
{
if(ModelState.IsValid)
{
使用(ExifReader reader=newexifreader(FormFile.OpenReadStream()))
{
GetTagValue(ExifTags.XPTitle,输出字符串newPhotoCaption);
newphotocation=newphotocation;
reader.GetTagValue(ExifTags.XPDescription,out字符串newPhotoDescription);
NewPhotoDescription=NewPhotoDescription;
reader.GetTagValue(ExifTags.DateTime,out-DateTime-newPhotoDate);
NewPhotoDate=NewPhotoDate;
}
}
返回页();
}

reader.GetTagValue方法的输出参数始终为null。我正在成功实例化FormFile属性,因为我可以读取文件名、长度等(代码未显示)。

我可以使用SixLabors.ImageSharp NuGet包从文件中读取jpeg元数据

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Iptc;

public string NewPhotoCaption { get; set; }
public string NewPhotoDescription { get; set; }
public DateTime? NewPhotoDate { get; set; }

public async Task<IActionResult> OnPostLoadInfoAsync()
{
  if (ModelState.IsValid)
  {
    Image image = Image.Load(FormFile.OpenReadStream());
    List<IptcValue> nameValues = image.Metadata.IptcProfile.GetValues(IptcTag.Name);
    if (nameValues.Count > 0)
    {
     NewPhotoCaption = nameValues[0].Value;
    }      

    List<IptcValue> captionValues = image.Metadata.IptcProfile.GetValues(IptcTag.Caption);
    if (captionValues.Count > 0)
    {
      NewPhotoDescription = captionValues[0].Value;
    }

    string dateString;
    DateTime newPhotoDate;
    List<IptcValue> dateValues = 
      Image.Metadata.IptcProfile.GetValues(IptcTag.CreatedDate);
    if (dateValues.Count > 0)
    {
      dateString = dateValues[0].Value;
      if (DateTime.TryParseExact(
        dateString,
        "yyyyMMdd",
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out newPhotoDate))
      {
        NewPhotoDate = newPhotoDate;
      }
      else
      {
        NewPhotoDate = null;
      }
    }
    else
    {
      NewPhotoDate = null;
    }
   return Page();
}
使用SixLabors.ImageSharp;
使用SixLabors.ImageSharp.Metadata.Profiles.Iptc;
公共字符串NewPhotoCaption{get;set;}
公共字符串NewPhotoDescription{get;set;}
公共约会时间?NewPhotoDate{get;set;}
OnPostLoadInfoAsync()上的公共异步任务
{
if(ModelState.IsValid)
{
Image=Image.Load(FormFile.OpenReadStream());
List nameValues=image.Metadata.IptcProfile.GetValues(IptcTag.Name);
如果(nameValues.Count>0)
{
NewPhotoCaption=nameValues[0]。值;
}      
List captionValues=image.Metadata.IptcProfile.GetValues(IptcTag.Caption);
如果(captionValues.Count>0)
{
NewPhotoDescription=captionValues[0]。值;
}
字符串日期字符串;
日期时间新光状态;
列表日期值=
Image.Metadata.IptcProfile.GetValues(IptcTag.CreatedDate);
如果(dateValues.Count>0)
{
dateString=dateValues[0]。值;
如果(DateTime.TryParseExact)(
日期字符串,
“yyyyMMdd”,
CultureInfo.InvariantCulture,
DateTimeStyles。无,
输出(新光状态)
{
NewPhotoDate=NewPhotoDate;
}
其他的
{
NewPhotoDate=null;
}
}
其他的
{
NewPhotoDate=null;
}
返回页();
}

我能够使用SixLabors.ImageSharp NuGet包从文件中读取jpeg元数据

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Iptc;

public string NewPhotoCaption { get; set; }
public string NewPhotoDescription { get; set; }
public DateTime? NewPhotoDate { get; set; }

public async Task<IActionResult> OnPostLoadInfoAsync()
{
  if (ModelState.IsValid)
  {
    Image image = Image.Load(FormFile.OpenReadStream());
    List<IptcValue> nameValues = image.Metadata.IptcProfile.GetValues(IptcTag.Name);
    if (nameValues.Count > 0)
    {
     NewPhotoCaption = nameValues[0].Value;
    }      

    List<IptcValue> captionValues = image.Metadata.IptcProfile.GetValues(IptcTag.Caption);
    if (captionValues.Count > 0)
    {
      NewPhotoDescription = captionValues[0].Value;
    }

    string dateString;
    DateTime newPhotoDate;
    List<IptcValue> dateValues = 
      Image.Metadata.IptcProfile.GetValues(IptcTag.CreatedDate);
    if (dateValues.Count > 0)
    {
      dateString = dateValues[0].Value;
      if (DateTime.TryParseExact(
        dateString,
        "yyyyMMdd",
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out newPhotoDate))
      {
        NewPhotoDate = newPhotoDate;
      }
      else
      {
        NewPhotoDate = null;
      }
    }
    else
    {
      NewPhotoDate = null;
    }
   return Page();
}
使用SixLabors.ImageSharp;
使用SixLabors.ImageSharp.Metadata.Profiles.Iptc;
公共字符串NewPhotoCaption{get;set;}
公共字符串NewPhotoDescription{get;set;}
public DateTime?NewPhotoDate{get;set;}
OnPostLoadInfoAsync()上的公共异步任务
{
if(ModelState.IsValid)
{
Image=Image.Load(FormFile.OpenReadStream());
List nameValues=image.Metadata.IptcProfile.GetValues(IptcTag.Name);
如果(nameValues.Count>0)
{
NewPhotoCaption=nameValues[0]。值;
}      
List captionValues=image.Metadata.IptcProfile.GetValues(IptcTag.Caption);
如果(captionValues.Count>0)
{
NewPhotoDescription=captionValues[0]。值;
}
字符串日期字符串;
日期时间新光状态;
列表日期值=
Image.Metadata.IptcProfile.GetValues(IptcTag.CreatedDate);
如果(dateValues.Count>0)
{
dateString=dateValues[0]。值;
如果(DateTime.TryParseExact)(
日期字符串,
“yyyyMMdd”,
CultureInfo.InvariantCulture,
DateTimeStyles。无,
输出(新光状态)
{
NewPhotoDate=NewPhotoDate;
}
其他的
{
NewPhotoDate=null;
}
}
其他的
{
NewPhotoDate=null;
}
返回页();
}

感谢您发布解决方案。感谢您发布解决方案。