本地生成图片两种方法

一种sprite变为Textrue2d,存在本地 ,博客已经说过

一种相机截图 需要引入  System.Drawing.dll:

   public  void CaptureCamera(Camera camera)
    {
     
        Rect rect=new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
        // 创建一个RenderTexture对象 
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);

   camera.targetTexture = rt;
        camera.Render();

       RenderTexture.active = rt;
        //Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);

       screenShot.ReadPixels(rect,0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
        screenShot.Compress(true);
        screenShot.Apply();

 
         ui.TakePhoto.SetActive(true);
         Sprite pic = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f,   0.5f));//后面Vector2就是你Anchors的Pivot的x/y属性值
         ui.Photo.GetComponent<UnityEngine.UI.Image>().sprite = pic;

 camera.targetTexture = null;
        //ps: camera2.targetTexture = null; 
        RenderTexture.active = null; // JC: added to avoid errors 
        Destroy(rt);

    byte[] bytes = screenShot.EncodeToPNG();
        i++;
        //filename = Application.persistentDataPath + "/Screenshot" + i.ToString()+".png";
        //创建一个新文件夹
        path = "/storage/emulated/0/AR课本";
        Directory.CreateDirectory(path);
        pathpic = "/storage/emulated/0/AR课本/截图";
        Directory.CreateDirectory(pathpic);
        filenameOld = pathpic + "/IMG_" + i.ToString() + ".png";

  File.WriteAllBytes(filenameOld, bytes);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值