using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; using System.Drawing; using System.Drawing.Imaging; namespace LibwebpSharp { /// /// /// public static class Utilities { /// /// Copy data from managed to unmanaged memory /// /// The data you want to copy /// Pointer to the location of the unmanaged data public static IntPtr CopyDataToUnmanagedMemory(byte[] data) { // Initialize unmanged memory to hold the array int size = Marshal.SizeOf(data[0]) * data.Length; IntPtr pnt = Marshal.AllocHGlobal(size); // Copy the array to unmanaged memory Marshal.Copy(data, 0, pnt, data.Length); return pnt; } /// /// Get data from unmanaged memory back to managed memory /// /// A Pointer where the data lifes /// How many bytes you want to copy /// public static byte[] GetDataFromUnmanagedMemory(IntPtr source, int lenght) { // Initialize managed memory to hold the array byte[] data = new byte[lenght]; // Copy the array back to managed memory Marshal.Copy(source, data, 0, lenght); return data; } /// /// Get a file from the disk and copy it into a managed byte array /// /// The path to the file /// A byte array containing the file public static byte[] CopyFileToManagedArray(string path) { // Load file from disk and copy it into a byte array FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); fs.Close(); return data; } /// /// Convert raw image data into a Bitmap object /// /// The byte array containing the image data /// The width of your image /// The height of your image /// The PixelFormat the Bitmap should use /// The Bitmap object conating you image public static Bitmap ConvertDataToBitmap(byte[] data, int imgWidth, int imgHeight, PixelFormat format) { // Create the Bitmap to the know height, width and format Bitmap bmp = new Bitmap(imgWidth, imgHeight, format); // Create a BitmapData and Lock all pixels to be written BitmapData bmpData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); //Copy the data from the byte array into BitmapData.Scan0 Marshal.Copy(data, 0, bmpData.Scan0, data.Length); //Unlock the pixels bmp.UnlockBits(bmpData); //Return the bitmap return bmp; } /// /// Calculate the needed size for a bitmap /// /// The image width /// The image height /// The pixel format you want to use /// The bitmap size in bytes public static int CalculateBitmapSize(int imgWidth, int imgHeight, PixelFormat format) { return imgWidth * imgHeight * Image.GetPixelFormatSize(format) / 8; } } }