C#保存Texture到本地图片的方式

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大侠后花园

如果觉得文章不错请给点支持吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值