標籤:ima eric matlab 寫入 .text frame mba top text
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 = 0; //從當前流讀取位元組塊,並寫入緩衝區 ms1.Read(arr1, 0, (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(1111, 1111); 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, 0, (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和圖片互轉