using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; namespace ImageLoader { /// /// Image library class, used for loading images via DevIL.dll /// public unsafe class ImageLib { #region DllImports [DllImport("devil.dll")] private static extern void ilInit(); //initializes DevIL [DllImport("devil.dll")] private static extern void ilGenImages( //generates an image to work with int Num, uint* Images ); [DllImport("devil.dll")] private static extern void ilBindImage( //binds DevIL functions to this image uint Image ); [DllImport("devil.dll")] private static extern bool ilLoadImage( //load the image string FileName ); [DllImport("devil.dll")] private static extern int ilGetInteger( //get a value uint Mode ); [DllImport("devil.dll")] private static extern bool ilConvertImage( //convert the image uint DestFormat, uint DestType ); [DllImport("devil.dll")] private static extern void ilCopyPixels( //copy pixels to a buffer uint XOff, uint YOff, uint ZOff, uint Width, uint Height, uint Depth, uint Format, uint Type, IntPtr Data ); [DllImport("devil.dll")] private static extern void ilDeleteImages( //delete generated images int Num, uint *Images ); #endregion /// /// Loads an image using DevIL.dll /// /// The image to load /// A nice and crisp .NET bitmap public static Bitmap LoadImage(string filename) { //example of use: //pictureBox1.Image = ImageLib.LoadImage("image.dds"); uint imageID = 0; //initialize DevIL ilInit(); //generate 1 image id ilGenImages(1, &imageID); //bind functions to generated image ilBindImage(imageID); //load image from file ilLoadImage(filename); //get width/height of image int width = ilGetInteger((uint)DevILValue.IL_IMAGE_WIDTH); int height = ilGetInteger((uint)DevILValue.IL_IMAGE_HEIGHT); //create a bitmap Bitmap bitmap1 = new Bitmap(width, height, PixelFormat.Format32bppArgb); //used for Bitmap.LockBits() Rectangle rectangle1 = new Rectangle(0, 0, width, height); //create BitmapData class to obtain a pointer to the first byte of data //lock bits to system memory BitmapData data1 = bitmap1.LockBits(rectangle1, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); //get the pointer to the data IntPtr ptr = data1.Scan0; //convert the image to BGRA format //not sure why BGRA, i would expect RGBA, this might be wrong ilConvertImage((uint)DevILImage.IL_BGRA, (uint)DevILData.IL_UNSIGNED_BYTE); //copy the DevIL image bitmap data to the address pointed to by ptr ilCopyPixels(0, 0, 0, (uint)width, (uint)height, 1, (uint)DevILImage.IL_BGRA, (uint)DevILData.IL_UNSIGNED_BYTE, ptr); //unlock bits from memory bitmap1.UnlockBits(data1); //clean up by deleting image ilDeleteImages(1, &imageID); //return a nice and clean .NET bitmap return bitmap1; } } public enum DevILValue : uint { IL_ORIGIN_SET = 0x0600, IL_ORIGIN_LOWER_LEFT = 0x0601, IL_ORIGIN_UPPER_LEFT = 0x0602, IL_ORIGIN_MODE = 0x0603, IL_FORMAT_SET = 0x0610, IL_FORMAT_MODE = 0x0611, IL_TYPE_SET = 0x0612, IL_TYPE_MODE = 0x0613, IL_FILE_MODE = 0x0621, IL_CONV_PAL = 0x0630, IL_VERSION_NUM = 0x0DE2, IL_IMAGE_WIDTH = 0x0DE4, IL_IMAGE_HEIGHT = 0x0DE5, IL_IMAGE_DEPTH = 0x0DE6, IL_IMAGE_SIZE_OF_DATA = 0x0DE7, IL_IMAGE_BPP = 0x0DE8, IL_IMAGE_BYTES_PER_PIXEL = 0x0DE8, IL_IMAGE_BITS_PER_PIXEL = 0x0DE9, IL_IMAGE_FORMAT = 0x0DEA, IL_IMAGE_TYPE = 0x0DEB, IL_PALETTE_TYPE = 0x0DEC, IL_PALETTE_SIZE = 0x0DED, IL_PALETTE_BPP = 0x0DEE, IL_PALETTE_NUM_COLS = 0x0DEF, IL_PALETTE_BASE_TYPE = 0x0DF0, IL_NUM_IMAGES = 0x0DF1, IL_NUM_MIPMAPS = 0x0DF2, IL_NUM_LAYERS = 0x0DF3, IL_ACTIVE_IMAGE = 0x0DF4, IL_ACTIVE_MIPMAP = 0x0DF5, IL_ACTIVE_LAYER = 0x0DF6, IL_CUR_IMAGE = 0x0DF7, IL_IMAGE_DURATION = 0x0DF8, IL_IMAGE_PLANESIZE = 0x0DF9, IL_IMAGE_BPC = 0x0DFA, IL_IMAGE_OFFX = 0x0DFB, IL_IMAGE_OFFY = 0x0DFC, IL_IMAGE_CUBEFLAGS = 0x0DFD, IL_IMAGE_ORIGIN = 0x0DFE, IL_IMAGE_CHANNELS = 0x0DFF, } public enum DevILImage : uint { IL_COLOUR_INDEX = 0x1900, IL_COLOR_INDEX = 0x1900, IL_RGB = 0x1907, IL_RGBA = 0x1908, IL_BGR = 0x80E0, IL_BGRA = 0x80E1, IL_LUMINANCE = 0x1909, } public enum DevILData : uint { IL_BYTE = 0x1400, IL_UNSIGNED_BYTE = 0x1401, IL_SHORT = 0x1402, IL_UNSIGNED_SHORT = 0x1403, IL_INT = 0x1404, IL_UNSIGNED_INT = 0x1405, IL_FLOAT = 0x1406, IL_DOUBLE = 0x140A, } }