C#與unity中base64string和圖片互轉

來源:互聯網
上載者:User

標籤: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和圖片互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.