gpt4 book ai didi

c# - 从图像中获取元数据 C#

转载 作者:行者123 更新时间:2023-11-30 16:59:15 30 4
gpt4 key购买 nike

我正在尝试将图像导入我的 WPF 应用程序,并在单击保存按钮后将图像及其元数据保存到不同的位置。

我目前有:

BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
BitmapMetadata importedMetaData = new BitmapMetadata("jpg");
using (Stream sourceStream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None);
// Check source is has valid frames
if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
{
sourceDecoder.Frames[0].Metadata.Freeze();
// Get a clone copy of the metadata
BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
importedMetaData = sourceMetadata;
}
}
if (!Directory.Exists(Settings.LocalPhotoDirectory))
{
Directory.CreateDirectory(Settings.LocalPhotoDirectory);
}
string photoPath = Path.Combine(Settings.LocalPhotoDirectory, this.BasicTags.ElementAt(8).Value.ToString());
if (!Directory.Exists(photoPath))
{
Directory.CreateDirectory(photoPath);
}
string localfileName = Path.Combine(photoPath, PhotoId.ToString() + ".jpg");
string fileName = Path.Combine(this.Settings.QueueFolder, PhotoId.ToString() + ".jpg");

using (FileStream stream = new FileStream(fileName, FileMode.Create))
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = this.Settings.ImageQuality;
encoder.Frames.Add(BitmapFrame.Create(Photo, null, importedMetaData, null));
encoder.Save(stream);
}

其中 Photo 是一个 BitmapSourcefileName 是照片的文件名。但是我的代码一直在 encoder.Save lin 崩溃。出现以下错误:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll

additional information: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

这整个方法正在作为 STA 线程运行,因为我读到您必须使用 STA 线程访问 BitmapMetadata 类。但仍然没有运气。我做错了什么?

最佳答案

不,您不必使用BitmapCacheOption.OnLoad

您只需不要使用 BitmapCacheOption.None

  • 默认:将整个图像缓存到内存中。这是默认值。
  • :不创建内存存储。所有对图像的请求都直接由图像文件填充。
  • OnDemand:仅为请求的数据创建内存存储。第一个请求直接加载图片;随后的请求是从缓存中填充。
  • OnLoad:在加载时将整个图像缓存到内存中。所有对图像数据的请求都从内存存储中填充。

所以,基本上你可以使用

BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.Default);

More about BitmapCacheOption

关于c# - 从图像中获取元数据 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888260/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com