1

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.

Jon Dier
  • 157
  • 1
  • 3
  • 17
  • Just for info, image.Save(ms, format); always crashes unity, example code: http://stackoverflow.com/a/7350732/5452781 , I just ended copying it pixel by pixel.. would had been better to convert Bitmap into byte[] array, and load that array into unity texture. – mgear Nov 08 '16 at 09:35
  • Does this link help for this? https://forum.unity3d.com/threads/convert-writeablebitmap-to-texture2d-efficiently.210305/ – ctc chen Nov 08 '16 at 10:37
  • May be you just try copy texture pixel by pixel using https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html ? – OnionFan Nov 08 '16 at 10:51
  • It's likely you're looking for the incredibly useful "RawImage" facility in Unity ... just google. – Fattie Dec 05 '16 at 13:05

2 Answers2

1

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.

Jon Dier
  • 157
  • 1
  • 3
  • 17
1

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