Skip to content

Commit 5e9be87

Browse files
authored
Merge pull request sharpdx#887 from crowbar27/master
Added IFormattable to FourCC to implement fix for FromFourCC.
2 parents c45d3fb + a046bb8 commit 5e9be87

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Source/SharpDX.MediaFoundation/VideoFormatGuids.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static partial class VideoFormatGuids
1515
/// <returns>Media foundation unique ID</returns>
1616
public static Guid FromFourCC(SharpDX.Multimedia.FourCC fourCC)
1717
{
18-
return new Guid(string.Concat(fourCC.ToString(), "-0000-0010-8000-00aa00389b71"));
18+
return new Guid(string.Concat(fourCC.ToString("I", null), "-0000-0010-8000-00aa00389b71"));
1919
}
2020
}
2121
}

Source/SharpDX/Multimedia/FourCC.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020
using System;
21+
using System.Globalization;
2122
using System.Runtime.InteropServices;
2223

2324
namespace SharpDX.Multimedia
@@ -26,7 +27,7 @@ namespace SharpDX.Multimedia
2627
/// A FourCC descriptor.
2728
/// </summary>
2829
[StructLayout(LayoutKind.Sequential, Size = 4)]
29-
public struct FourCC : IEquatable<FourCC>
30+
public struct FourCC : IEquatable<FourCC>, IFormattable
3031
{
3132
/// <summary>
3233
/// Empty FourCC.
@@ -175,6 +176,48 @@ public override int GetHashCode()
175176
return (int) value;
176177
}
177178

179+
/// <summary>
180+
/// Provides a custom string representation of the FourCC descriptor.
181+
/// </summary>
182+
/// <remarks>
183+
/// The general format "G" is equivalent to the parameterless.
184+
/// <see cref="FourCC.ToString()"/>. The special format "I" returns a
185+
/// string representation which can be used to construct a Media
186+
/// Foundation format GUID. It is equivalent to "X08".
187+
/// </remarks>
188+
/// <param name="format">The format descriptor, which can be "G" (empty
189+
/// or <c>null</c> is equivalent to "G"), "I" or any valid standard
190+
/// number format.</param>
191+
/// <param name="formatProvider">The format provider for formatting
192+
/// numbers.</param>
193+
/// <returns>The requested string representation.</returns>
194+
/// <exception cref="System.FormatException">In case of
195+
/// <paramref name="format"/> is not "G", "I" or a valid number
196+
/// format.</exception>
197+
public string ToString(string format, IFormatProvider formatProvider)
198+
{
199+
if (string.IsNullOrEmpty(format))
200+
{
201+
format = "G";
202+
}
203+
if (formatProvider == null)
204+
{
205+
formatProvider = CultureInfo.CurrentCulture;
206+
}
207+
208+
switch (format.ToUpperInvariant())
209+
{
210+
case "G":
211+
return this.ToString();
212+
213+
case "I":
214+
return this.value.ToString("X08", formatProvider);
215+
216+
default:
217+
return this.value.ToString(format, formatProvider);
218+
}
219+
}
220+
178221
public static bool operator ==(FourCC left, FourCC right)
179222
{
180223
return left.Equals(right);

0 commit comments

Comments
 (0)