Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
2
Question by rawatenator · Apr 22, 2012 at 08:57 AM · c#texture2dimagevision

How to convert texture2D to image in C#

Hello, I am trying to use Aforge for Computer Vision with the unity webcam. I need to turn a texture2D into a C# image file, and I don't know how to do it. Based on some looking aroudn the net, I wrote the following functions. However, I am apparently missing a dll: gdiplus.dll , which I am starting to think doesn't exist on the OSX operating system. How else can I convert texture2D into an Image?

And I am surprised there isn't some code already available to do this.

 # region texture2image 
 public static System.Drawing.Image Texture2Image(Texture2D texture)
 {
   if (texture == null)
   {
       return null;
   }
   //Save the texture to the stream.
   byte[] bytes = texture.EncodeToPNG();
 
    //Memory stream to store the bitmap data.
   MemoryStream ms = new MemoryStream(bytes);
 
   //Seek the beginning of the stream.
   ms.Seek(0, SeekOrigin.Begin);
 
   //Create an image from a stream.
   System.Drawing.Image bmp2 = System.Drawing.Bitmap.FromStream(ms);
 
   //Close the stream, we nolonger need it.
   ms.Close();
   ms = null;
   
   return bmp2;
 }
 #endregion





 # region image2texture 
 public static Texture2D Image2Texture(System.Drawing.Image im)
 {
     if (im == null)
       {
           return new Texture2D(4,4);
       }
 
 
       //Memory stream to store the bitmap data.
       MemoryStream ms = new MemoryStream();
 
     
       //Save to that memory stream.
       im.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
     
        //Go to the beginning of the memory stream.
       ms.Seek(0, SeekOrigin.Begin);

       //make a new Texture2D
       Texture2D tex = new Texture2D(im.Width, im.Height);
      
       tex.LoadImage(ms.ToArray());
               
       //Close the stream.
       ms.Close(); 
       ms = null;
 
       //
       return tex;
 }
 #endregion


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
1
Best Answer

Answer by Eric5h5 · Apr 22, 2012 at 09:33 AM

It's not a matter of OS X...you can't use System.Drawing, that's not in Mono. You don't need that anyway, just use File.WriteAllBytes for the PNG data. Look at the code example in the docs for EncodeToPNG.

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
avatar image
2

Answer by Soldeplata · Mar 25, 2016 at 05:50 PM

Hi, I will answer to this question:

"How to convert texture2D to image in Unity using C#"

 Image pictureInScene;
 byte[] temp= File.ReadAllBytes ("pathTo/image.png");
 Texture2D tempPic= new Texture2D (2, 2);
 fotillo.LoadImage (temp);
 pictureInScene.sprite = Sprite.Create (tempPic, new Rect (0, 0, 128, 128), new Vector2 ());//set the Rect with position and dimensions as you need


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 awsapps · Feb 17 at 01:42 PM 0
Share

Don't forget to use texture.Apply() if you modify the pixels of the Texture2D

avatar image
0

Answer by Bunny83 · Apr 22, 2012 at 11:26 AM

A quick search on google and the AForge forums gives me this:

Is AForge supported on Mono

Support for Mac OS

From the first link it seems it's possible to use "some" parts of the framework, but you have to compile them yourself for mono.

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Making Images Fade In and Out 5 Answers

How do I iterate over a large Texture2d quickly? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to cut an image at an angle from the center? 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