5

I am trying to load a png file (other formats are an option) for rendering as a texture in OpenTk in a project targeting .netstandard 1.4, which does not support the System.Drawing libraries.

Every OpenTk example I can find for this depends on the System.Drawing.Bitmap class.

Here is an example of the kind of method I want to create without the System.Drawing libraries, from the texture class of this Jitter Physics OpenGL Demo

    void CreateFromBitmap(System.Drawing.Bitmap image)
    {
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        GL.GenTextures(1, out name);
        GL.BindTexture(TextureTarget.Texture2D, name);

        // set pixel unpacking mode
        GL.PixelStore(PixelStoreParameter.UnpackSwapBytes, 0);
        GL.PixelStore(PixelStoreParameter.PackRowLength, 0);
        GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
        GL.PixelStore(PixelStoreParameter.UnpackSkipRows, 0);
        GL.PixelStore(PixelStoreParameter.UnpackSkipPixels, 0);

        BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        // Requieres OpenGL >= 1.4
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); // 1 = True
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                      OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

        image.UnlockBits(data);

        // set texture parameters - will these also be bound to the texture???
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);

        GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.Decal);

        image = null;
        // Unbind
        GL.BindTexture(TextureTarget.Texture2D, 0);
    }

What is another way of loading images and formating them for use with OpenTk?

Kelson Ball
  • 948
  • 1
  • 13
  • 30

2 Answers2

5

Intermediary options for transition of OpenTK to .NETStandard2.0

JimSEOW
  • 109
  • 1
  • 7
2

You can install the necessary libraries for System.Drawing from NuGet. Just open the NuGet console and type the following command:

Install-Package System.Drawing.Common

This is the official Library from Microsoft. It requires .net Standard 2.0.