图片裁剪和旋转处理 (unity适用)

来源:互联网 发布:unity3d 室内灯光 编辑:程序博客网 时间:2024/05/21 12:55

        unity实现旋转和裁剪需要在图片导入的时候设置好属性为可读写,但是对于一些动态加载来的图片设置的话要using UnityEditor了,这样打包的时候又会有问题。网上还有一些直接截屏的方法,个人认为不是很好,尤其是屏幕上有其他界面会出现遮挡或特效遮挡界面的时候,我目前想到的方法就是如下啦~~~


 //裁剪
    public static string CutImage(string filePath, int beginX, int beginY, int lenth, int height, string fileName,
      string savePath,
      string fileExtension)
    {
        Bitmap bitmap = new Bitmap(filePath);

        if (((beginX + lenth) <= bitmap.Width) && ((beginY + height) <= bitmap.Height))
        {
            Bitmap destBitmap = new Bitmap(lenth, height);
            Rectangle destRect = new Rectangle(0, 0, lenth, height);
            Rectangle srcRect = new Rectangle(beginX, beginY, lenth, height);
            //bitmap.RotateFlip(flipType);
            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBitmap);
            graph.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);

            ImageFormat format = ImageFormat.Png;//建议用png,jpg会导致纹理被压缩变的越来越模糊。
            switch (fileExtension.ToLower())
            {
                case "png":
                    format = ImageFormat.Png;
                    break;
                case "bmp":
                    format = ImageFormat.Bmp;
                    break;
                case "gif":
                    format = ImageFormat.Gif;
                    break;
                case "jpeg":
                case "jpg":
                    format = ImageFormat.Jpeg;
                    break;
            }
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            string path = savePath + "/" + fileName.Split('.')[0] + "." + fileExtension;
            destBitmap.Save(path, format);

            destBitmap.Dispose();//要释放掉资源
            bitmap.Dispose();//要释放掉资源
            return path;

        }
        bitmap.Dispose();

        Debug.Log("截取范围超出图片范围");
        return null;

    }



  //旋转
    public static string RotateImage(string filePath, string fileName, string savePath, string fileExtension, RotateFlipType flipType)
    {
        Bitmap bitmap = new Bitmap(filePath);
        Bitmap destBitmap = new Bitmap(bitmap.Height, bitmap.Width);
        Rectangle destRect = new Rectangle(0, 0, bitmap.Height, bitmap.Width);

        bitmap.RotateFlip(flipType);
        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBitmap);
        graph.DrawImage(bitmap, destRect);

        ImageFormat format = ImageFormat.Png;
        switch (fileExtension.ToLower())
        {
            case "png":
                format = ImageFormat.Png;
                break;
            case "bmp":
                format = ImageFormat.Bmp;
                break;
            case "gif":
                format = ImageFormat.Gif;
                break;
            case "jpeg":
            case "jpg":
                format = ImageFormat.Jpeg;
                break;
        }
        if (!Directory.Exists(savePath))
        {
            Directory.CreateDirectory(savePath);
        }
        string path = savePath + "/" + fileName.Split('.')[0] + "." + fileExtension;
        destBitmap.Save(path, format);

        destBitmap.Dispose();//要释放掉资源
        bitmap.Dispose();  //要释放掉资源
        return path;

    }



阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 第29个全国助残日 郑州29路公交车路线 西安29路 29路公交 西安29路公交线路 29是什么意思 福州29路公交路线 宿州29路 29路公交路线 北京29路公交路线 29路公交车 南宁市29路公交线路 南宁29路公交路线 郑州29路公交线路 厦门29路 厦门29路公交车路线 29路车 南京29路公交线路 wwwbbb b2轰炸机 b2驾照 b2 bb ab 维生素b2作用 维生素b2缺乏病 维生素b2千万别乱补 b2b什么意思 2b r2b重返基地 2b小姐姐 b驾照 bo b2隐形轰炸机 长宏b2b b1和b2驾照哪个实用 ba 维生素b2一次吃几粒 2b铅笔 维生素b2说明书 pb