栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用C#将“注释”添加到JPEG文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用C#将“注释”添加到JPEG文件

以下代码解决了我的问题,并在给定的JPEG图像中添加了注释:

public void addImageComment(string imageFlePath, string comments)    {        string jpegDirectory = Path.GetDirectoryName(imageFlePath);        string jpegFileName = Path.GetFileNameWithoutExtension(imageFlePath);        BitmapDeprer deprer = null;        Bitmapframe bitmapframe = null;        Bitmapmetadata metadata = null;        FileInfo originalImage = new FileInfo(imageFlePath);        if (File.Exists(imageFlePath))        { // load the jpg file with a JpegBitmapDeprer     using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {     deprer = new JpegBitmapDeprer(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } bitmapframe = deprer.frames[0]; metadata = (Bitmapmetadata)bitmapframe.metadata; if (bitmapframe != null) {     Bitmapmetadata metaData = (Bitmapmetadata)bitmapframe.metadata.Clone();     if (metaData != null)     {         // modify the metadata metaData.SetQuery("/app1/ifd/exif:{uint=40092}", comments);         // get an enprer to create a new jpg file with the new metadata.    JpegBitmapEnprer enprer = new JpegBitmapEnprer();         enprer.frames.Add(Bitmapframe.Create(bitmapframe, bitmapframe.Thumbnail, metaData, bitmapframe.ColorContexts));         //string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg");         // Delete the original         originalImage.Delete();         // Save the new image          using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))         {  enprer.Save(jpegStreamOut);         }     } }        }    }

这实际上是Konamiman友善提供的链接下找到的代码的轻度修改版本。

请注意,要进行此工作,您需要向 PresentationCoreWindowsbase 添加.NET引用。如果使用Visual Studio
2008,则可以通过以下方式实现:

  1. 在解决方案资源管理器中右键单击您的项目

  2. 从下拉列表中,选择添加“参考…”。

  3. 在打开的新框中,选择“ .NET”选项卡

  4. 滚动到上面提到的两个参考,然后在每个参考上单击“确定”。

非常感谢danbystrom和Konamiman在这件事上的帮助。我非常感谢您的快速回应。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号