简体   繁体   中英

Convert a Bitmap to a Texture2D in Unity

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 is this done?

I believe I have to use the MemoryStream, but in what fashion is unknown to me.

I managed to achieve this by using a memorystream, ie:

        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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM