-
Notifications
You must be signed in to change notification settings - Fork 0
/
fImgMetadata.cs
70 lines (63 loc) · 2.64 KB
/
fImgMetadata.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Wew.Control;
using Wew.Media;
namespace Demo;
class fImgMetadata : cDockControl
{ cBitmap? m_bmpImg;
readonly cEditControl edDat;
public fImgMetadata()
{ cButton btn;
Text = "Image metadata";
edDat = new cEditControl { Margins = new Rect(10, 10, 10, 40), TabSize = 500};
AddControl(edDat);
btn = new cButton { Text = "Open", Margins = new Rect(10, float.NaN, float.NaN, 10)};
btn.Click += btnOpen_Click;
AddControl(btn);
btn = new cButton { Text = "Write metadata", Margins = new Rect(120, float.NaN, float.NaN, 10)};
btn.Click += btnWrite_Click;
AddControl(btn);
}
public override void Close() { m_bmpImg?.Dispose(); base.Close();}
private void btnOpen_Click(object sender)
{ string? sFile;
sFile = mDialog.ShowOpenFile(mMod.DLG_IMG_EXTS, mMod.DLG_IMG_GUID); if (sFile is null) return;
m_bmpImg?.Dispose();
m_bmpImg = new cBitmap(sFile, true);
if (m_bmpImg.Metadata is null) { m_bmpImg.Dispose(); m_bmpImg = null; mDialog.MsgBoxExclamation("No metadata"); return;}
edDat.Text = null;
m_AddPathsRecursive(true, "", m_bmpImg.Metadata.GetPaths(true));
m_AddPathsRecursive(false, "", m_bmpImg.Metadata.GetPaths(false));
edDat.ClearUndo();
}
private void btnWrite_Click(object sender)
{ string? sFile;
if (m_bmpImg is null) { mDialog.MsgBoxExclamation("Open an image first"); return;}
sFile = mDialog.ShowSaveFile("Jpeg|*.jpg|Tiff|*.tif", mMod.DLG_IMG_GUID); if (sFile is null) return;
cBitmap.Save(sFile, default, null, m_bmpImg.FrameCount
, (cBitmap.cFrameEncoder fe) =>
{ cBitmap.MetadataWriter? mw;
_ = fe.WriteContainerMetadata(m_bmpImg.Metadata);
for (int i = 0, iFrames = (mImaging.IsMultiframe(fe.ContainerFormat) ? m_bmpImg.FrameCount : 1); i < iFrames; i++)
{ fe.Create();
mw = fe.WriteMetadata(m_bmpImg[i].Metadata);
if (mw is not null)
{ if (fe.ContainerFormat == cBitmap.eContainerFormat.Jpeg)
{ mw.Value.WriteString("/app1/ifd/{ushort=315}", "John Smith"); // Author
//mw.Value.Remove("/xmp/dc:creator"); // Author: different path when removing
} else if (fe.ContainerFormat == cBitmap.eContainerFormat.Tiff)
{ mw.Value.WriteString("/ifd/{ushort=315}", "John Smith"); // Author
}
}
fe.Write(m_bmpImg[i]);
}
});
}
void m_AddPathsRecursive(bool ImageLevel, string sRoot, string[]? a_sNodes)
{ string sPath;
if (a_sNodes is null) return;
foreach (string sNode in a_sNodes)
{ sPath = sRoot + sNode;
edDat.AddText(string.Format("{0} {1}\n", sPath, m_bmpImg!.Metadata!.GetValue(ImageLevel, sPath)));
m_AddPathsRecursive(ImageLevel, sPath, m_bmpImg.Metadata.GetPaths(ImageLevel, sPath));
}
}
}