1.Texture2D保存为本地图片
public void SaveCacheImage(Texture2D image, string folderPath, string imageName)
{
if (!string.IsNullOrEmpty(folderPath) && image != null && !string.IsNullOrEmpty(imageName))
{
if (Directory.Exists(folderPath))
{
try
{
string texturePath = Path.Combine(folderPath, string.Format("{0}", imageName));
byte[] bytes = image.EncodeToPNG();
System.IO.File.WriteAllBytes(texturePath, bytes);
}
catch (System.Exception ex)
{
Debug.LogError("cls:PageViewShowControl cls:SaveCacheImage ExceptionInfo:" + ex);
}
}
else
{
Debug.LogError("cls:PageViewShowControl cls:SaveCacheImage info:CacheFoder isn't exist, path:!" + folderPath);
}
}
}
2.视频各帧流数据保存为本地图片
public Bitmap ToColorBitmap2(byte[] rawValues, int width, int height)
{
try
{
Debug.LogError("width:" + width+ "" + "height:" + height + ",Length:" + rawValues.Length);
Bitmap m_currBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Rectangle m_rect = new Rectangle(0, 0, width, height);
BitmapData m_bitmapData = m_currBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
IntPtr iptr = m_bitmapData.Scan0; // 获取bmpData的内存起始位置
用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(rawValues, 0, iptr, width * height * 4);
m_currBitmap.UnlockBits(m_bitmapData);
算法到此结束,返回结果
}
return m_currBitmap;
}
catch (System.Exception e)
{
Debug.LogError("== ToColorBitmap2 Exception info:" + e);
return null;
}
}