0
Texture2D tex = new Texture2D(200, 300, TextureFormat.RGB24, false);
Rectangle screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds;

Bitmap target = new Bitmap(screenSize.Width, screenSize.Height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(target))
{
    g.CopyFromScreen(0, 0, 0, 0, new Size(screenSize.Width, screenSize.Height));
}

m_Renderer.enabled = true;

m_Renderer.material.SetTexture("_MainTex", target);

I'm trying to set a bitmap as a texture in Unity, but I get the error

cannot convert from 'System.Drawing.Bitmap' to 'UnityEngine.Texture'

Any ideas how I can convert the bitmap to a texture?

janlindso
  • 1,225
  • 3
  • 16
  • 41

1 Answers1

0

You could try converting Bitmap to byte[] (as shown here). After that you can call Texture2D.LoadRawTextureData(byte[]) or Texture2D.LoadImage(byte[]).

Sebastián
  • 88
  • 1
  • 5