【问题标题】:Load image in .Net Standard在 .Net 标准中加载图像
【发布时间】:2017-10-29 06:08:23
【问题描述】:

我正在尝试加载一个 png 文件(其他格式是一个选项),以便在面向 .netstandard 1.4 的项目中渲染为 OpenTk 中的纹理,它不支持 System.Drawing 库。

我能找到的每个 OpenTk 示例都依赖于 System.Drawing.Bitmap 类。

这是我想在没有 System.Drawing 库的情况下创建的一种方法示例,来自 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);
    }

加载图像并格式化它们以供 OpenTk 使用的另一种方法是什么?

【问题讨论】:

标签: c# image system.drawing opentk .net-standard


【解决方案1】:

您可以从 NuGet 安装 System.Drawing 所需的库。只需打开 NuGet 控制台并键入以下命令:

Install-Package System.Drawing.Common

这是 Microsoft 的官方库。它需要 .net Standard 2.0。

【讨论】:

    【解决方案2】:

    将 OpenTK 转换为 .NETStandard2.0 的中间选项

    【讨论】:

    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多