【Unity】如何对外部图片进行裁剪?

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;

public class CutBitmap : MonoBehaviour {    
    DirectoryInfo myFolder;
    DirectoryInfo newFolder;
    FileInfo[] files;
    FileInfo[] saveFiles;
    FileInfo newFile;
    string myPath;
    string savePath;
    public Material m;
    Texture2D texture;
    Texture2D texCode;
    string key;

    void Start () {
        myPath = Environment.CurrentDirectory + @"\Z_DrawTextures";//原图所在文件夹
        savePath= @"Assets\Resources\Z_CutTextures";//方便扫描,在Resources下创建目录
        myFolder = new DirectoryInfo(myPath);
        newFolder = new DirectoryInfo(savePath);
        if(!newFolder.Exists) newFolder.Create();

        //裁剪图片,得到两个贴图
        CutTexture(myPath+"\\"+newFile.Name);

        //扫描二维码
        key= Scan.ScanQRCode(texCode);

        //构造材质
        m= ProduceMaterial(texture);

        //生成对应物体
        SpawnNewRole(m,key);
    }

    private void SortAsFileCreationTime(ref FileInfo[] arrFi)//按照文件创建时间对文件夹进行排序
    {
        Array.Sort(arrFi, delegate (FileInfo x, FileInfo y) { return x.CreationTime.CompareTo(y.CreationTime); });
    }

 
    void CutTexture(string path)//裁剪Bitmap格式的图片
    {
        Bitmap source = new Bitmap(path);  
        
        //将要裁剪的图片大小(参数是:左上角顶点和width,height)
        RectangleF rectTex = new RectangleF(0, 280, 1000, 1020);
        RectangleF rectCode = new RectangleF(0, 0, 280, 280);

        Bitmap texBitmap = source.Clone(rectTex,PixelFormat.DontCare);//裁1
        Bitmap codeBitmap = source.Clone(rectCode, PixelFormat.DontCare);//裁2                                                 

        texBitmap.Save(savePath+"\\"+"bit1.png");
        codeBitmap.Save(savePath + "\\" + "bit2.png");
        saveFiles = newFolder.GetFiles();
        SortAsFileCreationTime(ref saveFiles);

        byte[] bytes1 = File.ReadAllBytes(savePath+"\\"+ saveFiles[0].Name);//将png文件读成字节数组
        byte[] bytes2 = File.ReadAllBytes(savePath + "\\" + saveFiles[1].Name);

        texture = new Texture2D(1000,1020);
        texCode = new Texture2D(280, 280);
        texture.LoadImage(bytes1);//将图片字节流转为Unity支持的Texture2D格式
        texCode.LoadImage(bytes2);

        //这样处理是因为扫描二维码的函数中,需要一个在Assets文件夹的图片路径
        texture = Resources.Load<Texture2D>("Z_CutTextures/" + "bit1");
        texCode = Resources.Load<Texture2D>("Z_CutTextures/" + "bit2");

    }

C#裁剪原理:

 Bitmap texBitmap = source.Clone(rectTex,PixelFormat.DontCare);//裁1

调用Bitmap类对象的一个Clone方法,可以将原图的指定长方形区域复制过来,也就是“裁剪”。

具体过程:

1.先得到原图(Bitmap格式)

自己在工程外创建一个文件夹保存所有需要裁剪的图片。为了方便找到具体是哪个图片,可以采用不同的文件排序方式。因为我们项目的图片是动态生成的,所以我用创建时间排序,最新的哪个就是我的目标图片。

2.计算好原图和将要裁剪的图片的大小(以像素为单位),构建裁剪矩形。

构建裁剪的矩形时,以左上角为原点,向右为X正轴,向下为Y正轴

3.保存裁剪后的图片,转存为Unity支持的Texture2D格式。

  对原图进行裁剪后保存为png格式或Jpeg格式(Unity的Texture2D类对象的LoadImage方法只支持这两种格式),保存在本地或者内存中。

MemoryStream ms1 = new MemoryStream();//新建流
texBitmap.Save(ms1,ImageFormat.Png);//存
byte[] bytes1 = ms1.GetBuffer();//取
ms1.Close();//关闭流
4.拿到Texture2D后,你爱干嘛就干嘛了,在这里,一般是用来填充材质的MainTex。

补充:如果想要对bitmap进行旋转,可以:

texBitmap.RotateFlip(RotateFlipType.Rotate180FlipY); //沿Y轴旋转180度;                                


猜你喜欢

转载自blog.csdn.net/qq_36622009/article/details/80836536