http://blog.csdn.net/awnuxcvbn/article/details/9199245

效果

代码

  1. <pre name="code" class="csharp">using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. public class CropPicture : MonoBehaviour
  5. {
  6. string localPath = "http://192.168.1.100:8080/picture/15.jpg";
  7. Texture2D image;
  8. Texture2D cutImage;
  9. WWW www;
  10. Rect rect;
  11. float time;
  12. Vector2 pos1;
  13. Vector2 pos2;
  14. // Use this for initialization
  15. void Start()
  16. {
  17. StartCoroutine(LoadImage());
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. //点击鼠标左键,记录第一个位置
  23. if (Input.GetMouseButtonDown(0))
  24. {
  25. pos1 = Input.mousePosition;
  26. time = Time.time;
  27. if (time > 1f)
  28. {
  29. Debug.Log(pos1);
  30. }
  31. }
  32. //放开左键记录第二个位置
  33. if (Input.GetMouseButtonUp(0))
  34. {
  35. pos2 = Input.mousePosition;
  36. Debug.Log(pos2);
  37. StartCoroutine(CutImage());
  38. time = 0;
  39. }
  40. }
  41. void OnGUI()
  42. {
  43. //当下载完成
  44. if (www.isDone)
  45. {
  46. GUI.DrawTexture(new Rect(0, 0, 600, 904), image);
  47. }
  48. GUI.Button(new Rect(0, 0, 100, 50), "W" + Screen.width + "H" + Screen.height);
  49. if (pos1 != null)
  50. {
  51. GUI.Button(new Rect(0, 50, 150, 50), pos1.ToString());
  52. }
  53. if (pos2 != null)
  54. {
  55. GUI.Button(new Rect(0, 100, 150, 50), pos2.ToString());
  56. }
  57. if (cutImage != null)
  58. {
  59. GUI.Button(new Rect(0, 150, 150, 50), "image W" + cutImage.width + "H" + cutImage.height);
  60. }
  61. if (rect != null)
  62. {
  63. GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());
  64. }
  65. }
  66. //下载图片
  67. IEnumerator LoadImage()
  68. {
  69. www = new WWW(localPath);
  70. yield return www;
  71. image = www.texture;
  72. if (www.error != null)
  73. {
  74. Debug.Log(www.error);
  75. }
  76. }
  77. //截图
  78. IEnumerator CutImage()
  79. {
  80. //图片大小
  81. cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);
  82. //坐标左下角为0
  83. rect = new Rect((int)pos1.x, Screen.height - (int)(Screen.height - pos2.y), (int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y));
  84. yield return new WaitForEndOfFrame();
  85. cutImage.ReadPixels(rect, 0, 0, true);
  86. cutImage.Apply();
  87. yield return cutImage;
  88. byte[] byt = cutImage.EncodeToPNG();
  89. //保存截图
  90. File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage.png", byt);
  91. }
  92. }

Unity3D随意截图并保存的更多相关文章

  1. Unity3d之截图方法

    http://blog.csdn.net/highning0007/article/details/37991787 Unity3d之截图方法 分类: Unity3D2013-11-28 17:13  ...

  2. [Android] 拍照、截图、保存并显示在ImageView控件中

    近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法.     PS:作者购买 ...

  3. opencv 截图并保存

    opencv 截图并保存(转载) 代码功能:选择图像中矩形区,按S键截图并保存,Q键退出. #include<opencv2/opencv.hpp> #include<iostrea ...

  4. html2canvas插件对整个网页或者网页某一部分截图并保存为图片

    html2canvas能够实现在用户浏览器端直接对整个或部分页面进行截屏.这个脚本将当前页面渲染成一个canvas图片,通过读取DOM并将不同的样式应用到这些元素上实现.它不需要来自服务器任何渲染,整 ...

  5. 对html进行截图并保存为本地图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Python+Selenium练习篇之21-如何截图并保存

    本文介绍如何利用Selenium的方法进行截图,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图.在selenium for python中主要有三个截图方法,我们挑选其中最常用的一种. ge ...

  7. Python3.X Selenium 自动化测试中如何截图并保存成功

    在selenium for python中主要有三个截图方法,我们挑选其中最常用的一种. 挑最常用的:get_screenshot_as_file() 相关代码如下:(下面的代码可直接复制) # co ...

  8. selenium 页面截图并保存

    import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org. ...

  9. 利用HTML5的Video进行视频截图并保存到本地

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

随机推荐

  1. android intent 传递 二进制数据

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 实现序列化.

  2. 矩阵&行列式

    # 代数 排列 对换,对于一个排列操作,对于一个偶排列一次对换之后变为奇排列 反之变为偶排列 行列式 N阶行列式室友N^2个数aij(i,j = 1,2,3,...n) 行列式的数=\(\sum_ { ...

  3. [SDOI2017]数字表格 --- 套路反演

    [SDOI2017]数字表格 由于使用markdown的关系 我无法很好的掌控格式,见谅 对于这么简单的一道题竟然能在洛谷混到黑,我感到无语 \[\begin{align*} \prod\limits ...

  4. CodeForces - 1009E Intercity Travelling

    题面在这里! 可以发现全是求和,直接拆开算贡献就好了 #include<bits/stdc++.h> #define ll long long using namespace std; c ...

  5. Android 应用内多进程实现

    android平台支持多进程通信,也支持应用内实现多进程那么多进程应该能为我们带来什么呢我们都知道,android平台对应用都有内存限制,其实这个理解有点问题,应该是说android平台对每个进程有内 ...

  6. bzoj 1086 树分块

    将树分成一些块,做法见vfleaking博客. /************************************************************** Problem: 108 ...

  7. 《深入理解Spark-核心思想与源码分析》(五)第五章任务提交与执行

    即欲捭之贵周,即欲阖之贵密.周密之贵,微而与道相随.---<鬼谷子> 解释:译文:如果要分析问题,关键在于周详,如果要综合归纳问题,关键在于严密.周详严密的关键在于精深而与道相随. 解词: ...

  8. 实用小工具 -- 国家地区IP段范围查询工具

    如果想限制某个国家地区IP段访问,这几个查询工具就很有用了. 可以查询各个国家IP段范围,并且是持续更新的,使用方便. 当然,除此之外,你还可以通过APNIC.ARIN.RIPE这些官方IP分配机构查 ...

  9. [转]Android中的android:layout_width和android:width

      android:width 其实是定义控件上面的文本(TextView) 的宽度,当然这个宽度也是和 android:layout_width 配合起来作用的,如果 android:layout_ ...

  10. 05-树6. Path in a Heap (25) 小根堆

    05-树6. Path in a Heap (25) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...