Hx1719255967 2022-11-11 13:27 采纳率: 0%
浏览 39

大内存图片加载失败,求帮助

加载了一张大小为19M的8位深度的png图片,用下面两种方法,出现错误,有没有朋友提供下思路
方法1
private Texture2D SetImageToString_Temp(string imgPath)
    {
        FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
        fs.Seek(0, SeekOrigin.Begin);
        byte[] imgByte = new byte[fs.Length];
        fs.Read(imgByte, 0, imgByte.Length);
        fs.Close();
        fs.Dispose();
        fs = null;

        int width = 5472;
        int height = 3648;
        Texture2D texture = new Texture2D(width, height,TextureFormat.RGB24,false);
        texture.LoadImage(imgByte);
        texture.Apply();
        return texture;

    }
结果:图片为问号,加载失败
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/575964441866141.png)

#######方法二:
```c#
void ShowPicHandle(string picPatj)
    {
        byte[] b = File.ReadAllBytes(picPatj);

        int width = 5472;
        int height = 3648;

        int division = 8; // 長寬各除以 division 倍。可以填入 : 2 4 8 16 的值。

        Texture2D t = new Texture2D(width / division, height / division, TextureFormat.RGB24, false);

        byte[] b2 = new byte[t.width * t.height * 4]; // 不知為啥要乘 4 ,理論上乘 3 就好..但會報錯

        int i = 0; // i : Gr R B Gb
        int k = 0; // k : R  G B

        int w = width * (division - 1); // 跳 w 行

        for (int y = 0; y < height; y += division, i += w)
        {
            for (int x = 0; x < width; x += division, i += division, k += 3)
            {
                b2[k + 0] = b[i + 1];     // R
                b2[k + 1] = b[i];         // Gr
                b2[k + 2] = b[i + width]; // B
            }
        }

        t.LoadRawTextureData(b2);
        t.Apply();

        RawImageShowImage.texture = t;
    }
结果:按照这位思路,链接:https://blog.csdn.net/weixin_38884324/article/details/83796187,图片出现偏移,我不太理解他写的什么意思,有朋友教导下嘛
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/532302441866156.png)


```

  • 写回答

4条回答 默认 最新

  • 於黾 2022-11-11 13:49
    关注

    人家解的是raw,你解的是个图片,格式都不一样,照搬人家的代码不出错才怪
    读图片直接image.fromFile(path)不就行了,用的着费这么大劲

    评论

报告相同问题?

问题事件

  • 修改了问题 11月11日
  • 创建了问题 11月11日

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序