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和图片互转的更多相关文章

  1. unity中把一个图片切割成两个UI图片

    1.在unity3D的Project视图下选中需要更改的图片,将图片的Texture Type更改为Sprite (2D and UI),点击Apply即可.操作如图所示: 2.完成步骤一,点击App ...

  2. 在Unity 3D中加入Image图片

    在Unity 3D中加入Image图片,我在刚开始是加不进去的,为什么呢?因为没有图片,图如下: 原因就是我们没有把图片设置为Script,图片的格式还是默认的那个,这只能作为贴图使用.我们将图片进行 ...

  3. unity中实现三个Logo图片进行3秒钟的若隐若现后互相切换Logo图片

    private List<Sprite> storeTexture; public void Start() { storeTexture = new List<Sprite> ...

  4. Unity中创建二维码

    在网络上发现了一个可以把字符串转换成二维码的dll,但是我们要怎么使用他呢.不废话,直接进入主题. 用到的引用 using UnityEngine;using ZXing;using ZXing.Qr ...

  5. 【原创翻译】初识Unity中的Compute Shader

    一直以来都想试着自己翻译一些东西,现在发现翻译真的很不容易,如果你直接把作者的原文按照英文的思维翻译过来,你会发现中国人读起来很是别扭,但是如果你想完全利用中国人的语言方式来翻译,又怕自己理解的不到位 ...

  6. C#中的yield return与Unity中的Coroutine(协程)(下)

    Unity中的Coroutine(协程) 估计熟悉Unity的人看过或者用过StartCoroutine() 假设我们在场景中有一个UGUI组件, Image: 将以下代码绑定到Image using ...

  7. Unity中制作游戏的快照游戏支持玩家拍快照

    Unity中制作游戏的快照游戏支持玩家拍快照 有些游戏支持玩家“拍快照”,也就是将游戏的精彩瞬间以图片的形式记录下来的功能.这个功能比较有趣,而且以后的用途也会很广,为此本节打算介绍:截取矩形区域内游 ...

  8. Unity中2D和UGUI图集的理解与使用

    图集 什么是图集? 在使用3D技术开发2D游戏或制作UI时(即使用GPU绘制),都会使用到图集,而使用CPU渲染的2D游戏和UI则不存在图集这个概念(比如Flash的原生显示列表),那么什么是图集呢? ...

  9. unity3d ppsspp模拟器中的post processing shader在unity中使用

    这个位置可以看到ppsspp的特殊处理文件位置来看看这些特效 用来测试的未加特效图片 ppsspp: 传说系列一生爱---英杰传说 最后的战士 aacolor 是关于饱和度,亮度,对比度,色调的调节, ...

随机推荐

  1. 「SP10628 COT - Count on a tree」

    主席树的综合运用题. 前置芝士 可持久化线段树:其实就是主席树了. LCA:最近公共祖先,本题需要在\(\log_2N\)及以内的时间复杂度内解决这个问题. 具体做法 主席树维护每个点到根节点这一条链 ...

  2. H.264 中的Annex B格式和AVCC格式

    首先要理解的是没有标准的H.264基本流格式.文档中的确包含了一个Annex,特别是描述了一种可能的格式Annex B格式,但是这个并不是一个必须要求的格式.标准文档中指定了视频怎样编码成独立的包,但 ...

  3. 刷题56. Merge Intervals

    一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...

  4. Go 开发者平均年薪 46 万?爬数据展示国内 Go 的市场行情到底如何

    随着云原生时代的到来,拥有高并发性.语法易学等特点的 Golang 地位逐渐凸显,在云原生编程中占据了主导地位.在近期出炉的 TIOBE 10 月编程语言排行榜中,Golang 从前一个月的 16 位 ...

  5. Lesson 12 banks and their customers

    Why is there no risk to the customer when a bank prints the customer's name on his cheques? When any ...

  6. idea配置使用

    1.下载时注意连带下载git 2.实时清除内存 打开 show memory indicator 3.插件安装  前端常用插件(vue,element,css,html,node,ts等)找到适合自己 ...

  7. python之常见模块(time,datetime,random,os,sys,json,pickle)

    目录 time 为什么要有time模块,time模块有什么用?(自己总结) 1. 记录某一项操作的时间 2. 让某一块代码逻辑延迟执行 时间的形式 时间戳形式 格式化时间 结构化时间 时间转化 总结: ...

  8. java程序中的经常出现的的异常处理课后总结

    一.JDK中常见的异常情况 1.常见异常总结图 2.java中异常分类 Throwable类有两个直接子类: (1)Exception:出现的问题是可以被捕获的 (2)Error:系统错误,通常由JV ...

  9. C++ 定位错误行

    ] = {}; SYSTEMTIME st; GetLocalTime(&st); sprintf_s(buf, , "%02d-%02d-%02d %02d:%02d:%02d | ...

  10. MariaDB——备份与恢复

    备份和恢复 为什么要备份?   灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作   测试   要注意的点:   备份需要多少时间   能够容忍多少的数据丢失   恢复数据需要在多长时间完成  ...