📄 metadatatag.cs
字号:
private unsafe byte[] GetData()
{
uint length = Length;
byte[] value = new byte[length];
byte* ptr = (byte*)FreeImage.GetTagValue(tag);
for (int i = 0; i < length; i++)
{
value[i] = ptr[i];
}
return value;
}
/// <summary>
/// Gets or sets the value of the metadata.
/// <para> In case value is of byte or byte[], <see cref="FREE_IMAGE_MDTYPE.FIDT_UNDEFINED"/> is assumed.</para>
/// <para> In case value is of uint or uint[], <see cref="FREE_IMAGE_MDTYPE.FIDT_LONG"/> is assumed.</para>
/// </summary>
public unsafe object Value
{
get
{
CheckDisposed();
int cnt = (int)Count;
if (Type == FREE_IMAGE_MDTYPE.FIDT_ASCII)
{
byte* value = (byte*)FreeImage.GetTagValue(tag);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cnt; i++)
{
sb.Append(Convert.ToChar(value[i]));
}
return sb.ToString();
}
else if (Type == FREE_IMAGE_MDTYPE.FIDT_NOTYPE)
{
return null;
}
Array array = Array.CreateInstance(idList[Type], Count);
void* src = (void*)FreeImage.GetTagValue(tag);
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
void* dst = (void*)Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
FreeImage.CopyMemory(dst, src, Length);
handle.Free();
return array;
}
set
{
SetValue(value);
}
}
/// <summary>
/// Sets the value of the metadata.
/// <para> In case value is of byte or byte[] <see cref="FREE_IMAGE_MDTYPE.FIDT_UNDEFINED"/> is assumed.</para>
/// <para> In case value is of uint or uint[] <see cref="FREE_IMAGE_MDTYPE.FIDT_LONG"/> is assumed.</para>
/// </summary>
/// <param name="value">New data of the metadata.</param>
/// <returns>True on success, false on failure.</returns>
/// <exception cref="NotSupportedException">
/// The data format is not supported.</exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="value"/> is null.</exception>
public bool SetValue(object value)
{
Type type = value.GetType();
if (!typeList.ContainsKey(type))
{
throw new NotSupportedException();
}
return SetValue(value, typeList[type]);
}
/// <summary>
/// Sets the value of the metadata.
/// </summary>
/// <param name="value">New data of the metadata.</param>
/// <param name="type">Type of the data.</param>
/// <returns>True on success, false on failure.</returns>
/// <exception cref="NotSupportedException">
/// The data type is not supported.</exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="value"/> and <paramref name="type"/> to not fit.</exception>
public bool SetValue(object value, FREE_IMAGE_MDTYPE type)
{
CheckDisposed();
if ((!value.GetType().IsArray) && (!(value is string)))
{
Array array = Array.CreateInstance(value.GetType(), 1);
array.SetValue(value, 0);
return SetArrayValue(array, type);
}
return SetArrayValue(value, type);
}
/// <summary>
/// Sets the value of this tag to the value of <paramref name="value"/>
/// using the given type.
/// </summary>
/// <param name="value">New value of the tag.</param>
/// <param name="type">Data-type of the tag.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="value"/> is a null reference.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="type"/> is FIDT_ASCII and
/// <paramref name="value"/> is not String.
/// <paramref name="type"/> is not FIDT_ASCII and
/// <paramref name="value"/> is not Array.</exception>
/// <exception cref="NotSupportedException">
/// <paramref name="type"/> is FIDT_NOTYPE.</exception>
private unsafe bool SetArrayValue(object value, FREE_IMAGE_MDTYPE type)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
byte[] data = null;
if (type == FREE_IMAGE_MDTYPE.FIDT_ASCII)
{
string tempValue = value as string;
if (tempValue == null)
{
throw new ArgumentException("value");
}
Type = type;
Count = (uint)(tempValue.Length + 1);
Length = (uint)((tempValue.Length * sizeof(byte)) + 1);
data = new byte[Length + 1];
for (int i = 0; i < tempValue.Length; i++)
{
data[i] = (byte)tempValue[i];
}
data[data.Length - 1] = 0;
}
else if (type == FREE_IMAGE_MDTYPE.FIDT_NOTYPE)
{
throw new NotSupportedException();
}
else
{
Array array = value as Array;
if (array == null)
{
throw new ArgumentException("value");
}
Type = type;
Count = (uint)array.Length;
Length = (uint)(array.Length * Marshal.SizeOf(idList[type]));
data = new byte[Length];
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
void* src = (void*)Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
fixed (byte* dst = data)
{
FreeImage.CopyMemory(dst, src, Length);
}
handle.Free();
}
return FreeImage.SetTagValue(tag, data);
}
/// <summary>
/// Add this metadata to an image.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <returns>True on success, false on failure.</returns>
public bool AddToImage(FIBITMAP dib)
{
CheckDisposed();
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
if (Key == null)
{
throw new ArgumentNullException("Key");
}
if (!selfCreated)
{
tag = FreeImage.CloneTag(tag);
if (tag.IsNull)
{
throw new Exception();
}
selfCreated = true;
}
if (!FreeImage.SetMetadata(Model, dib, Key, tag))
{
return false;
}
FREE_IMAGE_MDMODEL _model = Model;
string _key = Key;
selfCreated = false;
FreeImage.DeleteTag(tag);
return FreeImage.GetMetadata(_model, dib, _key, out tag);
}
/// <summary>
/// Gets a .NET PropertyItem for this metadata tag.
/// </summary>
/// <returns>The .NET PropertyItem.</returns>
public unsafe System.Drawing.Imaging.PropertyItem GetPropertyItem()
{
System.Drawing.Imaging.PropertyItem item = FreeImage.CreatePropertyItem();
item.Id = ID;
item.Len = (int)Length;
item.Type = (short)Type;
byte[] data = new byte[item.Len];
byte* ptr = (byte*)FreeImage.GetTagValue(tag);
for (int i = 0; i < data.Length; i++)
{
data[i] = ptr[i];
}
item.Value = data;
return item;
}
/// <summary>
/// Converts the value of the <see cref="MetadataTag"/> object
/// to its equivalent string representation.
/// </summary>
/// <returns>The string representation of the value of this instance.</returns>
public override string ToString()
{
CheckDisposed();
string fiString = FreeImage.TagToString(model, tag, 0);
if (String.IsNullOrEmpty(fiString))
{
return tag.ToString();
}
else
{
return fiString;
}
}
/// <summary>
/// Creates a deep copy of this <see cref="MetadataTag"/>.
/// </summary>
/// <returns>A deep copy of this <see cref="MetadataTag"/>.</returns>
public object Clone()
{
CheckDisposed();
MetadataTag clone = new MetadataTag();
clone.model = model;
clone.tag = FreeImage.CloneTag(tag);
clone.selfCreated = true;
return clone;
}
/// <summary>
/// Tests whether the specified object is a <see cref="MetadataTag"/> instance
/// and is equivalent to this <see cref="MetadataTag"/> instance.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="MetadataTag"/> instance
/// equivalent to this <see cref="MetadataTag"/> instance; otherwise, <b>false</b>.</returns>
public override bool Equals(object obj)
{
return ((obj is MetadataTag) && (Equals((MetadataTag)obj)));
}
/// <summary>
/// Tests whether the specified <see cref="MetadataTag"/> instance is equivalent to this <see cref="MetadataTag"/> instance.
/// </summary>
/// <param name="other">A <see cref="MetadataTag"/> instance to compare to this instance.</param>
/// <returns><b>true</b> if <paramref name="obj"/> equivalent to this <see cref="MetadataTag"/> instance;
/// otherwise, <b>false</b>.</returns>
public bool Equals(MetadataTag other)
{
return (this == other);
}
/// <summary>
/// Returns a hash code for this <see cref="MetadataTag"/> structure.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="MetadataTag"/>.</returns>
public override int GetHashCode()
{
return tag.GetHashCode();
}
/// <summary>
/// Compares this instance with a specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer indicating the lexical relationship between the two comparands.</returns>
/// <exception cref="ArgumentException"><paramref name="obj"/> is not a <see cref="MetadataTag"/>.</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (!(obj is MetadataTag))
{
throw new ArgumentException();
}
return CompareTo((MetadataTag)obj);
}
/// <summary>
/// Compares the current instance with another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
public int CompareTo(MetadataTag other)
{
CheckDisposed();
other.CheckDisposed();
return tag.CompareTo(other.tag);
}
/// <summary>
/// Releases all resources used by the instance.
/// </summary>
public void Dispose()
{
if (!disposed)
{
disposed = true;
if (selfCreated)
{
FreeImage.DeleteTag(tag);
}
}
}
/// <summary>
/// Gets whether this instance has already been disposed.
/// </summary>
public bool Disposed
{
get { return disposed; }
}
/// <summary>
/// Throwns an <see cref="ObjectDisposedException"/> in case
/// this instance has already been disposed.
/// </summary>
private void CheckDisposed()
{
if (disposed)
{
throw new ObjectDisposedException("The object has already been disposed.");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -