private void bLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
FIBITMAP dib = new FIBITMAP();
try
{
ofd.RestoreDirectory = true;
// Get image filename
if (ofd.ShowDialog() == DialogResult.OK)
{
// Load the image
dib = FreeImage.LoadEx(ofd.FileName);
// Check if image was loaded successfully
if (dib.IsNull) throw new Exception("Failed to load image.");
// Clear the treeview
tvMetadata.Nodes.Clear();
// Create a wrapper for all metadata the image contains
ImageMetadata iMetadata = new ImageMetadata(dib);
// Get each metadata model
foreach (MetadataModel metadataModel in iMetadata)
{
// Create a new node for each model
TreeNode modelNode = tvMetadata.Nodes.Add(metadataModel.ToString());
// Get each metadata tag and create a subnode for it
foreach (MetadataTag metadataTag in metadataModel)
{
modelNode.Nodes.Add(metadataTag.Key + ": " + metadataTag.ToString());
// Изменяю тэг
if (metadataTag.Key == "XPKeywords")
{
metadataTag.SetValue("new Tag", FREE_IMAGE_MDTYPE.FIDT_ASCII);
metadataTag.AddToImage(dib);
}
}
}
// тэги не сохраняются
FreeImage.SaveEx(dib, ofd.FileName);
}
}
catch (Exception ex)
{
while (ex.InnerException != null)
ex = ex.InnerException;
MessageBox.Show(ex.ToString(), "Exception caught");
}
// Clean up
finally
{
ofd.Dispose();
FreeImage.UnloadEx(ref dib);
}
}
MetadataTag tag = new MetadataTag(FREE_IMAGE_MDMODEL.FIMD_COMMENTS);
tag.Key = "new Tag";
tag.Value = "foobar";
FreeImage.SetMetadata(FREE_IMAGE_MDMODEL.FIMD_COMMENTS, dib, "new Tag", tag);
FreeImage.SaveEx(dib, ofd.FileName);