簡體   English   中英

在Unity中將位圖轉換為Texture2D

[英]Convert a Bitmap to a Texture2D in Unity

我處於一個在Unity中使用AForge.net處理位圖的場景中。 但是,位圖無法應用於Unity中的紋理,因此我可以看到我的輸出,這是怎么做的?

我相信我必須使用MemoryStream,但是我不知道以哪種方式使用。

我設法通過使用一個內存流來實現這一點,即:

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

bitmapCurrentframeRed是位圖,redCamera是texture2D,redFilter是GameObject(plane),用於查看我的輸出。

您可以嘗試以下行將System.Drawing.Bitmap轉換為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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM