本文告诉大家如何在 UWP 中,保存图片的时候,写入 Exif 信息,也就是如照片的 相机型号 制造商 光圈值等信息的写入

在 UWP 中,保存图片或照片需要用到图片编码器,在使用编码器写入前可以设置编码器写入图片的属性,此时就可以包含了 Exif 信息。关于啥是 Exif 信息,还请自行百度

不同的图片格式可以支持的 Exif 信息范围不相同,咱以下使用 jpg 图片作为例子。如果大家切换为其他图片格式,还请自行测试一下

在创建编码器可以在构造函数传入参数,通过参数设置一些 Exif 信息,如质量信息。下面代码在创建时传入质量信息

                BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);

上面代码的 pngStream 是一个文件,用于写入图片,这部分代码不是本文重点,如果要获取全部的代码,还请到本文最后获取代码

在创建完成编码器之后,依然可以再次设置图片信息,通过调用 encoder.BitmapProperties.SetPropertiesAsync 方法进行设置

如以下代码,设置作者信息

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);

                propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);

写入之后,可以右击图片文件的属性,进入详细信息。在详细信息里面可以看到图片的信息

以上有一个问题是,能写入属性有哪些,写入的类型是什么?这些可以从 官方文档 获取

如官方文档里面说写入相机型号的描述如下

propertyDescription
name = System.Photo.CameraManufacturer
shellPKey = PKEY_Photo_CameraManufacturer
formatID = 14B81DA1-0135-4D31-96D9-6CBFC9671A99
propID = 271
SearchInfo
InInvertedIndex = true
IsColumn = true
typeInfo
type = String

以上的含义就是写入的 Key 是 System.Photo.CameraManufacturer 要求传入的类型是 PropertyType.String 字符串,根据这个即可了解如何写以上的代码。如写入相机型号的描述等代码如下

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);

                // https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system?WT.mc_id=WD-MVP-5003260
propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
// 相机型号
propertySet.Add("System.Photo.CameraModel", new BitmapTypedValue("lindexi", PropertyType.String));
// 制造商
propertySet.Add("System.Photo.CameraManufacturer", new BitmapTypedValue("lindexi manufacturer", PropertyType.String));
// 光圈值 System.Photo.FNumberNumerator/System.Photo.FNumberDenominator
propertySet.Add("System.Photo.FNumberNumerator", new BitmapTypedValue(1, PropertyType.UInt32));
propertySet.Add("System.Photo.FNumberDenominator", new BitmapTypedValue(10, PropertyType.UInt32)); await encoder.BitmapProperties.SetPropertiesAsync(propertySet);

下面代码是在加载页面,然后进行截图,保存截图到本地文件的代码

        public MainPage()
{
this.InitializeComponent(); Loaded += MainPage_Loaded;
} private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await Task.Delay(TimeSpan.FromSeconds(1)); var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Grid); var pngFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(Path.GetRandomFileName() + ".jpg");
using (var pngStream = await pngFile.OpenStreamForWriteAsync())
{
BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet); // https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system?WT.mc_id=WD-MVP-5003260
propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
// 相机型号
propertySet.Add("System.Photo.CameraModel", new BitmapTypedValue("lindexi", PropertyType.String));
// 制造商
propertySet.Add("System.Photo.CameraManufacturer", new BitmapTypedValue("lindexi manufacturer", PropertyType.String));
// 光圈值 System.Photo.FNumberNumerator/System.Photo.FNumberDenominator
propertySet.Add("System.Photo.FNumberNumerator", new BitmapTypedValue(1, PropertyType.UInt32));
propertySet.Add("System.Photo.FNumberDenominator", new BitmapTypedValue(10, PropertyType.UInt32)); await encoder.BitmapProperties.SetPropertiesAsync(propertySet); var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); softwareBitmap.Dispose();
} await Launcher.LaunchFolderAsync(ApplicationData.Current.TemporaryFolder);
}

本文代码可以到 写入图片Exif信息.7z-CSDN 下载

本文上面代码放在 githubgitee 欢迎访问

可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin acdca3ea99682d6549cf2622fb96685531ab9ded

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

获取代码之后,进入 KechinabeleenalLechefahar 文件夹

UWP 写入图片 Exif 信息的更多相关文章

  1. Android 图片Exif信息相关的获取与修改

    1 Exif是什么 Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.ISO.焦距.日期时间 ...

  2. Android--操作图片Exif信息

    前言 在Android系统中,图片文件在内存中以像素点的二维数组加载,存放像素信息,还会在开头加上一些额外的照片拍摄参数信息,这些信息就是Exif.Android2.0之后,媒体库加入了操作图片Exi ...

  3. Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息

    1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...

  4. 改动图片exif信息

    我们先了解一下EXIF: EXIF能够附加于JPEG.TIFF.RIFF等文件之中.为其添加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本号信息. 全部的JPEG文件以字符串"0xF ...

  5. 七牛--关于图片上传方向不统一的问题--主要关于图片EXIF信息中旋转参数Orientation的理解

    [图片引用方向纠正]直接在图片后面添加 ?imageMogr/auto-orient eg:http://data.upfitapp.com/data/2016/10/18/1629114767606 ...

  6. 图片Exif 信息中Orientation的理解和对此的处理

    这个问题是在用七牛上传图片后获取宽高时发现的,一张图片,用图片浏览器打开始终是竖图,但是查看属性或者用七牛获取宽高,却发现宽大于高,也就是在属性中这是个横图.这样导致客户端用该宽高来展示图片会出现问题 ...

  7. Android 获取图片exif信息

    使用android api读取图片的exif信息 布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  8. 七牛:关于图片 EXIF 信息中旋转参数 Orientation 的理解

    EXIF(Exchangeable Image File)是 “可交换图像文件” 的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数.缩略图及其他属性信息,简单来说,Ex ...

  9. 图片Exif信息

    Exif文件格式简述链接:https://www.zhihu.com/question/23727439/answer/25467748 可交换图像文件常被简称为Exif(Exchangeable i ...

  10. Java读取图片exif信息实现图片方向自动纠正

    起因 一个对试卷进行OCR识别需求,需要实现一个功能,一个章节下的题目图片需要上下拼接合成一张大图,起初写了一个工具实现图片的合并,程序一直很稳定的运行着,有一反馈合成的图片方向不对,起初怀疑是本身图 ...

随机推荐

  1. 使用现代身份验证(OAuth)调用 EWS 服务

    我的博客园:https://www.cnblogs.com/CQman/ 转载: https://mp.weixin.qq.com/s?__biz=MzU0MzUxMzU2NA==&mid=2 ...

  2. html 本地预览图片 图片上绘制矩形框

    效果如图 完整html代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" / ...

  3. 基于ARM联合ZYNQ的设计小结

    基于ARM联合ZYNQ的设计小结 1.硬件设计 硬件设计就是使用PS的自带硬核,外接其他可以连接AXI的IP核,构成一个自定义的硬件平台.如果简单理解,可以把这些操作统称为底层.这部分的设计还是比较方 ...

  4. copy 导入包含特殊符号的文本

    客户提供了一份数据记录需要导入数据库,但是文本中有一个列的内容是反斜杠"\" ,因为""是特殊的转义字符,需要使用两个"\"才能表示,如果直 ...

  5. #树状数组,离散#洛谷 3586 [POI2015]LOG

    题目 分析 考虑\(\geq s\)的部分最多取到\(s\), 设\(<s\)的总和为\(p\),个数为\(t\), 那么\(p+(n-t)*s\geq c*s\)就一定能取到 代码 #incl ...

  6. VS的 x86_64 , x64_86 , x64 , x86 有什么区别

    x86 Native Tools Command Prompt - Sets the environment to use 32-bit, x86-native tools to build 32-b ...

  7. Python 内置数据类型详解

    内置数据类型 在编程中,数据类型是一个重要的概念. 变量可以存储不同类型的数据,不同类型可以执行不同的操作. Python默认内置了以下这些数据类型,分为以下几类: 文本类型:str 数值类型:int ...

  8. centos运行django,遇到sqlite报错

    在centos上运行django,报错: django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required ...

  9. ython 并不合适职场编程,SPL 才行

    职场人员使用 Excel 进行数据处理已经成为家常便饭.不过相信大家一定有过很无助的情况,比如复杂计算.重复计算.自动处理等,再遇上个死机没保存,整个人崩溃掉也不是完全不可能. 如果学会了程序语言,这 ...

  10. k8s 深入篇———— k8s 的本质[四]

    前言 简单整理一下k8s的本质. 正文 首先,Kubernetes 项目要解决的问题是什么? 编排?调度?容器云?还是集群管理? 实际上,这个问题到目前为止都没有固定的答案.因为在不同的发展阶段,Ku ...