C#与unity中base64string和图片互转
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
namespace test_CS_1
{
class Program
{ static void Main(string[] args)
{ string fileDir = "E:/DX12/Sketch3DToolkit-master/matlab/";
//文件名称
string filePath = Path.Combine(fileDir, "new");
string UserPhoto; //读图片转为Base64String
System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Path.Combine(fileDir, "2.png"));
using (MemoryStream ms1 = new MemoryStream())
{
//将文件指定格式保存到指定流中
bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Png);
byte[] arr1 = new byte[ms1.Length];
ms1.Position = ;
//从当前流读取字节块,并写入缓存区
ms1.Read(arr1, , (int)ms1.Length);
ms1.Close();
//将缓存区数据byte[],转为base64string
UserPhoto = Convert.ToBase64String(arr1);
} //将Base64String转为图片并保存
byte[] arr2 = Convert.FromBase64String(UserPhoto);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
//以指定格式保存到指定文件
bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png); } }
}
}
unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO; public class test_texture2d : MonoBehaviour { // Use this for initialization
//base64转图片
public string Base64ToTexture2d(string Base64STR)
{
Texture2D pic = new Texture2D(, );
byte[] data = System.Convert.FromBase64String(Base64STR);
pic.LoadImage(data);
byte[] bytes = pic.EncodeToPNG(); //下面是为了方便了解图片的信息写的
string year = System.DateTime.Now.Year.ToString();
string month = System.DateTime.Now.Month.ToString();
string day = System.DateTime.Now.Day.ToString();
string hour = System.DateTime.Now.Hour.ToString();
string minute = System.DateTime.Now.Minute.ToString();
string secend = System.DateTime.Now.Second.ToString();
//存储路径
string FileFullPath = Application.dataPath/*这是获取assets前的文件路径*/ + "/" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + secend + ".png"; File.WriteAllBytes(FileFullPath, bytes);
return FileFullPath;
}
//图片转base64string
public string Texture2dToBase64(string texture2d_path)
{
//将图片文件转为流文件
FileStream fs = new System.IO.FileStream(texture2d_path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] thebytes = new byte[fs.Length]; fs.Read(thebytes, , (int)fs.Length);
//转为base64string
string base64_texture2d = Convert.ToBase64String(thebytes);
return base64_texture2d;
}
void Start () { } // Update is called once per frame
void Update () { }
}
注意:需要生成其他格式图片,改相应的输出图片格式方法。
C#与unity中base64string和图片互转的更多相关文章
- unity中把一个图片切割成两个UI图片
1.在unity3D的Project视图下选中需要更改的图片,将图片的Texture Type更改为Sprite (2D and UI),点击Apply即可.操作如图所示: 2.完成步骤一,点击App ...
- 在Unity 3D中加入Image图片
在Unity 3D中加入Image图片,我在刚开始是加不进去的,为什么呢?因为没有图片,图如下: 原因就是我们没有把图片设置为Script,图片的格式还是默认的那个,这只能作为贴图使用.我们将图片进行 ...
- unity中实现三个Logo图片进行3秒钟的若隐若现后互相切换Logo图片
private List<Sprite> storeTexture; public void Start() { storeTexture = new List<Sprite> ...
- Unity中创建二维码
在网络上发现了一个可以把字符串转换成二维码的dll,但是我们要怎么使用他呢.不废话,直接进入主题. 用到的引用 using UnityEngine;using ZXing;using ZXing.Qr ...
- 【原创翻译】初识Unity中的Compute Shader
一直以来都想试着自己翻译一些东西,现在发现翻译真的很不容易,如果你直接把作者的原文按照英文的思维翻译过来,你会发现中国人读起来很是别扭,但是如果你想完全利用中国人的语言方式来翻译,又怕自己理解的不到位 ...
- C#中的yield return与Unity中的Coroutine(协程)(下)
Unity中的Coroutine(协程) 估计熟悉Unity的人看过或者用过StartCoroutine() 假设我们在场景中有一个UGUI组件, Image: 将以下代码绑定到Image using ...
- Unity中制作游戏的快照游戏支持玩家拍快照
Unity中制作游戏的快照游戏支持玩家拍快照 有些游戏支持玩家“拍快照”,也就是将游戏的精彩瞬间以图片的形式记录下来的功能.这个功能比较有趣,而且以后的用途也会很广,为此本节打算介绍:截取矩形区域内游 ...
- Unity中2D和UGUI图集的理解与使用
图集 什么是图集? 在使用3D技术开发2D游戏或制作UI时(即使用GPU绘制),都会使用到图集,而使用CPU渲染的2D游戏和UI则不存在图集这个概念(比如Flash的原生显示列表),那么什么是图集呢? ...
- unity3d ppsspp模拟器中的post processing shader在unity中使用
这个位置可以看到ppsspp的特殊处理文件位置来看看这些特效 用来测试的未加特效图片 ppsspp: 传说系列一生爱---英杰传说 最后的战士 aacolor 是关于饱和度,亮度,对比度,色调的调节, ...
随机推荐
- vue + element ui table表格二次封装 常用功能
因为在做后台管理项目的时候用到了大量的表格, 且功能大多相同,因此封装了一些常用的功能, 方便多次复用. 组件封装代码: <template> <el-table :data=&qu ...
- Integer a = 200,b=200比较详解
题记:前几天面试Java基础给来了个面试题Integer a=200,b=200;System.out.println(a==b);当时回答是false,后来面试官又来了一个Integer a=100 ...
- px(像素)、pt(点)、ppi、dpi、dp、sp之间的关系
px:pixel,像素,电子屏幕上组成一幅图画或照片的最基本单元 pt:point,点,印刷行业常用单位,等于1/72英寸 ppi:pixel per inch,每英寸像素数,该值越高,则屏幕越细腻 ...
- UniGUI的SQLite数据库(04)
1]放FDConnection1和FDQuery1到界面上 一定要 放一个 FDPhysSQLiteDriverLink1到ServerModule上 2]在OnFormCreate事件里写 FDQ ...
- Python中property属性的概论和使用方法
property属性 概念: 定义一个方法但是使用装饰器property,只可以有一个self形参 可以用这样的属性动态的获取属性的值 定义方式(经典类) class Fun(): @property ...
- 01 MATLAB基本概念
基本概念 整数类型 相同整数类型相乘还是整数 整数与浮点数相乘是这种整数类型 不同整数类型不能相乘,除非强制类型转换 整数与整数相乘: >> x = uint32(120); >&g ...
- 题解 P3950 【部落冲突】
树链剖分吼啊 一看就看出是LCT模板题啦 前记 见这么多人写LCT,却很少人写树链剖分,于是我就来一发树链剖分(其实是因为自己不会LCT) 本蒟蒻的写法和诸位写树链剖分的大神有点不同 思路 树链剖分, ...
- node指针
- 任意两点之间的最短路(floyed)
F.Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each ...
- BUAA软工第一次作业-热身
第一次作业-热身 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) (北京航空航天大学 - 计算机学院) 这个作业的要求在哪里 第一次作业-热身作业(阅读) 我在这个课程 ...