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 是关于饱和度,亮度,对比度,色调的调节, ...
随机推荐
- shell 脚本通过Webhook 发送消息到微信群
代码如下: #!/bin/sh # Filename: msg.sh # # Usage msg.sh "message text" # # 1. check if missing ...
- php实现简单链式操作mysql数据库类
<?php $dbConfig = require_once(dirname(__FILE__).'/config.php'); class Db{ public $conn; ...
- LTE 网元功能
E-NodeB : 无线资源管理,无线承载控制.无线接入控制.连接移动性控制.UE的上下行动态资源分配 IP头压缩及用户数据流加密 UE连接期间选择MME 路由用户面数据至S-GW 寻呼消息的组织和发 ...
- 设计模式课程 设计模式精讲 8-2 单例设计模式-懒汉式及多线程Debug实战
1 主要内容 1.1 多线程debug 1.2 synchronized同步锁的调用 1.3 懒加载的应用 2 代码演练 2.1 单线程调用 2.2 多线程调用 2.3 锁的调用 1 主要内容 1.1 ...
- 解决在高分屏下开发winform界面变形
Form.AutoScaleMode = AutoScaleMode.None; 需要在超大屏下显示的时候,再考虑 AutoScaleMode.Font; AutoScaleMode.Dpi;
- 回文串[APIO2014](回文树)
题目描述 给你一个由小写拉丁字母组成的字符串 s.我们定义 s 的一个子串的存在值为这个子串在 s中出现的次数乘以这个子串的长度.对于给你的这个字符串 s,求所有回文子串中的最大存在值. 输入格式 一 ...
- Linux-使用之vim出现的问题
参考来源: https://stackoverflow.com/questions/47667119/ycm-error-the-ycmd-server-shut-down-restart-wit-t ...
- Numpy与List之间的转换
说明:在做NLP的时候,经常需要查看当前数组数据的维度,也就是data.shape,而List是没有这个属性的,因此需要先将其转换成Numpy,以下为两者户想转换的方法 List转Numpy:nump ...
- updatexml()报错注入
首先了解下updatexml()函数 UPDATEXML (XML_document, XPath_string, new_value); 第一个参数:XML_document是String格式,为X ...
- 5.2 Nginx Http 反向代理