Making copies of image files and removing all metadata
I have an app that lets users load in jpg, png, tif, gif, and bmp files. I want to be able to make copies of these while removing the metadata in the copies. I'm not sure of the proper way to do this. They are currently loaded in WPF like so:
BitmapImage newImage = new BitmapImage();
using (FileStream stream = File.OpenRead(filePath))
{
newImage .BeginInit();
newImage .StreamSource = stream;
newImage .CacheOption = BitmapCacheOption.OnLoad;
newImage .EndInit();
}
Would it make sense to do a System.IO.File.Copy on each file, and then loop through and remove all the metadata on the copies? or is there a way to do it when the copy is initially created?
If I do the following, it removes all metadata. However, the file size and vertical/horizontal resolution are different in the copy, and gifs are no longer animated.
using (var originalImage = Image.FromFile(filePath))
{
using (Bitmap copyBitmap = new Bitmap(originalImage))
{
// Draw the original image onto the new bitmap
using (Graphics graphics = Graphics.FromImage(copyBitmap))
{
graphics.DrawImage(originalImage, new Rectangle(0, 0, copyBitmap.Width, copyBitmap.Height));
}
// Save the copy without metadata
copyBitmap.Save(outputFileName);
}
}
0 Replies