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 是关于饱和度,亮度,对比度,色调的调节, ...
随机推荐
- Eth合约攻击
前言 Ethernaut是一个类似于CTF的智能合约平台,集成了不少的智能合约相关的安全问题,这对于安全审计人员来说是一个很不错的学习平台,本篇文章将通过该平台来学习智能合约相关的各种安全问题,由于关 ...
- JSTL中获取URL参数
使用JSTL时,URL会被隐含的对象param包裹起来,使用param.变量名,直接获取值 <body>hello:${param.name}</body> 依据此逻辑,在使用 ...
- canvas画扇形、饼图
画扇形的方法 方法一:起始角度是0,那么第一条线就是line(r,0),通过旋转扇形的角度,第二条线就是line(r,0) //圆弧 ctx.save(); ctx.translate(100, 10 ...
- Vue源码(下篇)
上一篇是mount之前的添加一些方法,包括全局方法gloal-api,XXXMixin,initXXX,然后一切准备就绪,来到了mount阶段,这个阶段主要是 解析template 创建watcher ...
- Windows一键启动多个软件
@echo off title 启动工作环境 @echo 正在启动FileZilla%start+空格+/d+空格+目录路径+空格+程序名 % start /d"F:\安装包\FileZil ...
- Java连载78-深入自动拆装箱、Date类和SimpleDateFormat格式化
一.深入自动拆装箱 1.直接举例: public class D78_AutomaticUnpackingAndPacking{ public static void main(String[] ar ...
- JS操作网页中的iframe
/* *parent.html */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 01 MATLAB基本概念
基本概念 整数类型 相同整数类型相乘还是整数 整数与浮点数相乘是这种整数类型 不同整数类型不能相乘,除非强制类型转换 整数与整数相乘: >> x = uint32(120); >&g ...
- C语言动静态链接库使用(笔记)
看了视频一直没空写........... C静态链接库不用说了跟你写在cpp文件里的函数一样不会有单独的模块 不再赘述生活中用的比较少 例子 .h文件 int Plus(int x, int y); ...
- 哈希表,Java中的hashCode
哈希表: 将我们所需的键通过哈希函数转换索引,然后存储在一个数组中. 哈希表是时间和空间之间的平衡,体现空间换时间的算法思想(联想到预加载,缓存等,有时候多存储,预处理缓存一些东西,带来时间复杂度的改 ...