Convert a Bitmap to a Texture2D in Unity

前端 未结 2 948
死守一世寂寞
死守一世寂寞 2021-01-28 06:22

I\'m in a scenario where I\'m manipulating bitmaps using AForge.net in Unity. However, a Bitmap can\'t be applied to a texture in Unity, so I visibly can see my output, so how i

相关标签:
2条回答
  • 2021-01-28 06:29

    I managed to achieve this by using a memorystream, i.e.:

            MemoryStream msFinger = new MemoryStream();
            bitmapCurrentframeRed.Save(msFinger, bitmapCurrentframeRed.RawFormat);
            redCamera.LoadImage(msFinger.ToArray());
            redFilter.GetComponent<Renderer>().material.mainTexture = redCamera;
    

    With bitmapCurrentframeRed being a Bitmap, redCamera being a texture2D and redFilter being a GameObject(plane) used to view my output.

    0 讨论(0)
  • 2021-01-28 06:53

    you can try these line to convert System.Drawing.Bitmap to UnityEngine.Texture2D

    Bitmap bmp = new Bitmap;
    MemoryStream ms= new MemoryStream();
    bmp.Save(ms,ImageFormat.PNG);
    var buffer = new byte[ms.Length];
    ms.position = 0;
    ms.Read(buffer,0,buffer.Length);
    Texture2D t = new Texture2D(1,1);
    t.LoadImage(buffer);
    
    0 讨论(0)
提交回复
热议问题