• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Richard 3 · May 22, 2014 at 11:16 AM · imagestringencodingbased

How to encode an image to a base64 string?

How do I go about encoding an image to base64? I would like to store images in a database using t$$anonymous$$s method.

I have successfully converted a base64 string to an image and am using it in my project. Most of the information I have found works with strings. Like the following example.

 byte[] bytesToEncode = Encoding.UTF8.GetBytes (inputText);
 string encodedText = Convert.ToBase64String (bytesToEncode);

T$$anonymous$$s is simple enough, but what do I do with an image?

T$$anonymous$$s is how I am loading my image.

 byte[] b64_bytes = System.Convert.FromBase64String(b64_string); 
 tex = new Texture2D(1,1);
 tex.LoadImage(b64_bytes);

Any help will be hugely appreciated.

Thank You Oh, I am working in C#.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by kamgru123 · May 22, 2014 at 11:29 AM

EDIT: Apparently you can't serialize Texture2D (w$$anonymous$$ch I didn't know). But there's an easier solution: Texture2D.EncodeToPng() w$$anonymous$$ch returns byte array.

 Texture2D mytexture;
 byte[] bytes;
 
 bytes = mytexture.EncodeToPng();
 


The following text is wrong. Don't use it :P


You could use BinaryFormatter to serialize it to bytes and then encode it.

 using System.Runtime.Serialization.Formatters.Binary;
 
 
 byte[] bytes;
 
 using (MemoryStream ms = new MemoryStream())
 {
     BinaryFormatter bf = new BinaryFormatter();
     bf.Serialize(ms, tex);
     bytes = ms.ToArray();
 }
 
 string enc = Convert.ToBase64String(bytes);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Richard 3 · May 22, 2014 at 12:37 PM 0
Share

Hi Kamgru, Thanks for your quick reply! Im getting this error message though.

"SerializationException: Type UnityEngine.Texture2D is not marked as Serializable."

avatar image Richard 3 · May 22, 2014 at 01:12 PM 0
Share

Sweet! thanks man. Works!

avatar image bhatiarohan156 Richard 3 · Mar 09, 2016 at 06:42 AM 0
Share

Hi How did you solve the serialisation problem ?

avatar image Bunny83 bhatiarohan156 · Mar 09, 2016 at 11:29 AM 0
Share

The serialization problem can't be solved, because Texture2d can't be serialized by any .Net serialization method.

You should use EncodeToPng and Convert.ToBase64String just like the answer suggests. Base64 is just a way to encode any binary data as printable text.

avatar image
1

Answer by BytesCrafter · Aug 09, 2016 at 09:01 AM

public RawImage prevUserPhoto = ; T$$anonymous$$s must be not null! Put a texture wd here! public RawImage newUserPhoto = ; Leave t$$anonymous$$s null. T$$anonymous$$s is where decoded texture go.

ToLoad Image saved on PlayerPrefs as "string"

Texture2D newPhoto = new Texture2D (1, 1); newPhoto.LoadImage(Convert.FromBase64String(PlayerPrefs.GetString("PhotoSaved"))); newPhoto.Apply (); newUserPhoto = newPhoto;

ToSave Image from a Texture 2D w$$anonymous$$ch must be a "readable" and RGBA 32 format.

string stringData = Convert.ToBase64String (prevUserPhoto .EncodeToJPG ()); //1 PlayerPrefs.SetString(PhotoSaved, stringData );

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EternalLife · Feb 17, 2020 at 01:23 PM 0
Share

thx a lot!

avatar image
0

Answer by sebastiao_lucio · Jul 24, 2020 at 06:47 PM

@Richard 3
https://assetstore.unity.com/packages/tools/network/basic-web-request-pro-155363

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Real-time Image Compression/Encoding on GPU 0 Answers

Image Based Reflection 0 Answers

Image in label + string..? 1 Answer

Problems with Russian characters within strings (C#) 3 Answers

Finding a character unicode in C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges