winterhell's picture

Problem loading images on Linux

So, My code works on Windows. On Linux it works only for a single image, otherwise throws "Unhandled Exception: System.ArgumentException: A null reference or invalid value was found [GDI+ status: InvalidParameter] at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] in :0 " etc
The code that crashes it is

if (!File.Exists(filename)) throw new Exception("File not found") ;
            Bitmap TextureBitmap = new Bitmap(filename);
            int width = TextureBitmap.Width;
            int height = TextureBitmap.Height;  
            System.Drawing.Imaging.BitmapData TextureData =
            TextureBitmap.LockBits(new System.Drawing.Rectangle(0, 0, TextureBitmap.Width, TextureBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.GenTextures(1, out texIndex);
            //tell OpenGL that this is a 2D texture
            GL.BindTexture(TextureTarget.Texture2D, texIndex);
            //the following code sets certian parameters for the texture
            //load the data by telling OpenGL to build mipmaps out of the bitmap data
            // Glu.Build2DMipmap(TextureTarget.Texture2D,(int)PixelInternalFormat.Three, TextureBitmap.Width, TextureBitmap.Height,PixelFormat.Bgr, PixelType.UnsignedByte,TextureData.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextureBitmap.Width, TextureBitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, TextureData.Scan0);
            //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver

My guess is it has to do with the Bitmap class and the IntPtr. Do I have to find a way to read pixel data from png images with external library or am I doing something wrong?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
AndyKorth's picture

So I spent 10 minutes comparing it to my drawing code, and everything you do looks about right to me. I noticed that I set all my TexParameters after loading the TexImage2D, but whatever.

Then I noticed the "System.Drawing.GDIPlus" reference. This comes up a lot. Here's some information to help.

winterhell's picture

This is a bit late response but still for people who may encouter similar issues. My solution was to convert all the textures into raw data and save to a file. Now I'm creating the textures directly from the RGBA data and completely skip the usage of System.Drawing

AltSoftLab's picture

It maybe happens because after TextureBitmap disposed (pixel data became not valid - unmanaged data destructed in Bitmap.Dispose). Or if you try to open already opened file - it is because Bitmap locks file. Just try AltSketch and be happy! :)