从图像清除元数据的简便方法? [英] Easy way to clean metadata from an image?
问题描述
我正在为一个处理图像处理的公司项目提供服务,其中一种方法应该从传递给它的图像中清除元数据.
I'm working on a service for a company project that handles image processing, and one of the methods is supposed to clean the metadata from an image passed to it.
我认为实现目前可以使用,但是我不确定它是否影响图像质量或是否有更好的方法来处理此任务.你能告诉我是否有更好的方法吗?
I think implementation I currently have works, but I'm not sure if it's affecting the quality of images or if there's a better way to handle this task. Could you let me know if you know of a better way to do this?
这是有问题的方法:
public byte[] CleanMetadata(byte[] data)
{
Image image;
if (tryGetImageFromBytes(data, out image))
{
Bitmap bitmap = new Bitmap(image);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(image, new Point(0, 0));
}
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(image, typeof(byte[]));
}
return null;
}
tryGetImageFromBytes
方法供参考:
private bool tryGetImageFromBytes(byte[] data, out Image image)
{
try
{
using (var ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
}
}
catch (ArgumentException)
{
image = null;
return false;
}
return true;
}
重申:是否有更好的方法从图像中删除不涉及重绘的元数据?
To reiterate: is there a better way to remove metadata from an image that doesn't involve redrawing it?
谢谢.
推荐答案
- .NET方式:您可能想尝试一下
Metadata
集合.引用MSDN: - The .NET way: You may want to try your hand at the System.Windows.Media.Imaging.BitmapEncoder class - more precisely, its
Metadata
collection. Quoting MSDN: -
糟糕,我(不是偶然)忘记了某种方式:将原始位图文件打开到克隆它到一个新的
Bitmap
对象.将克隆内容写入新文件.像这样的单线:The 'Oops, I (not so accidentally) forgot something way: Open the original bitmap file into a
System.drawing.Bitmap
object. Clone it to a newBitmap
object. Write the clone's contents to a new file. Like this one-liner:(((System.Drawing.Bitmap)System.Drawing.Image.FromFile(@"C:\ file.png").Clone()).Save(@"C:\ file-nometa.png");
((System.Drawing.Bitmap)System.Drawing.Image.FromFile(@"C:\file.png").Clone()).Save(@"C:\file-nometa.png");
The direct file manipulation way (only for JPEG): Blog post about removing the EXIF area.
这篇关于从图像清除元数据的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
元数据-获取或设置与此相关的元数据 编码过程中的位图.
Metadata - Gets or sets the metadata that will be associated with this bitmap during encoding.