Skip to content

Instantly share code, notes, and snippets.

@idkaaa
Created December 2, 2016 01:20
Show Gist options
  • Save idkaaa/6bed25db4c7d61ab23b51bc6a2b698b1 to your computer and use it in GitHub Desktop.
Save idkaaa/6bed25db4c7d61ab23b51bc6a2b698b1 to your computer and use it in GitHub Desktop.
C# Serializer/Deserializer of codec options AVISaveOptions method.
///All of this borrowed from: https://msdn.microsoft.com/en-us/magazine/hh580739.aspx
/// <summary>
/// Writes codec options from codec configuration dialog to a file.
/// </summary>
private static void WriteAviSaveOptions(string FileName, ref AVICOMPRESSOPTIONS Options)
{
using (BinaryWriter Writer = new BinaryWriter(
File.Open(FileName, FileMode.Create)))
{
int OptionsSize = Marshal.SizeOf(Options);
byte[] OptionsByteArray = StructureToByteArray<AVICOMPRESSOPTIONS>(Options);
Writer.Write(OptionsByteArray);
if (Options.parametersSize > 0)
{
int ParametersSize = Options.parametersSize;
byte[] ParametersByteArray = new byte[ParametersSize];
Marshal.Copy((IntPtr)Options.parameters, ParametersByteArray, 0, ParametersSize);
Writer.Write(ParametersByteArray);
}
if (Options.formatSize > 0)
{
int FormatSize = Options.formatSize;
byte[] FormatByteArray = new byte[FormatSize];
Marshal.Copy((IntPtr)Options.format, FormatByteArray, 0, FormatSize);
Writer.Write(FormatByteArray);
}
}
}
/// <summary>
/// Reads saved codec configuration options from a file.
/// </summary>
private static void ReadAviSaveOptions(string FileName, ref AVICOMPRESSOPTIONS Options)
{
using (BinaryReader Reader = new BinaryReader(
File.Open(FileName, FileMode.Open)))
{
int OptionsSize = Marshal.SizeOf(Options);
byte[] OptionsByteArray = Reader.ReadBytes(OptionsSize);
Options = ByteArrayToStructure<AVICOMPRESSOPTIONS>(OptionsByteArray);
int ParametersSize = Options.parametersSize;
int FormatSize = Options.formatSize;
if (ParametersSize > 0)
{
Byte[] ParametersByteArray = Reader.ReadBytes(ParametersSize);
IntPtr ParametersUnmanagedPtr = Marshal.AllocHGlobal(ParametersSize);
Marshal.Copy(ParametersByteArray, 0, ParametersUnmanagedPtr, ParametersSize);
Options.parameters = (int)ParametersUnmanagedPtr;
}
if (FormatSize > 0)
{
Byte[] FormatByteArray = Reader.ReadBytes(FormatSize);
IntPtr FormatUnmanagedPtr = Marshal.AllocHGlobal(FormatSize);
Marshal.Copy(FormatByteArray, 0, FormatUnmanagedPtr, FormatSize);
Options.format = (int)FormatUnmanagedPtr;
}
}
}
/// <summary>
/// Converts a structure to a byte array.
/// https://gist.github.com/Unril/5823360
/// </summary>
public static byte[] StructureToByteArray<T>(T obj)
{
int len = Marshal.SizeOf(typeof(T));
var arr = new byte[len];
IntPtr ptr = IntPtr.Zero;
try
{
ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
}
finally
{
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
}
}
return arr;
}
/// <summary>
/// Converts a byte array to a structure.
/// https://gist.github.com/Unril/5823360
/// </summary>
public static T ByteArrayToStructure<T>(byte[] bytearray)
{
int len = Marshal.SizeOf(typeof(T));
IntPtr ptr = IntPtr.Zero;
T obj;
try
{
ptr = Marshal.AllocHGlobal(len);
Marshal.Copy(bytearray, 0, ptr, len);
obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
}
finally
{
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment