Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How can i read a bitmap image pixels colors ?

Discussion in 'Scripting' started by Chocolade, Jun 2, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    941
    What i want to do is to get the pixels of a bitmap file from the hard disk.
    Then to decide each pixel color as parameter in unity object.
    For example if we have RGB then R will be height. So if the range of pixels is between 0-255 now how can i convert it to height units un unity ?

    R = Height G = width and B = ?
    For example if i draw on a paper a cube in a red color.
    Now i read the pixels using GetPixels of the red cube.
    The cube i draw is from the top view. Now i want to use the rgb to define the object in unity.
    So the cube in unity will be for example 255,5,20 so the cube will be maybe X,Y,Z(RGB) scale.
    So if i have the pixels i will decide that the cube size will be 255,5,20

    And i need to convert the bitmap size to my terrain size to decide where to position the cube.

    This is script i'm using trying to read the pixels:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.Drawing;
    6.  
    7. public class BitmapToTexture : MonoBehaviour {
    8.  
    9.     private void Start()
    10.     {
    11.         Bitmap bmp = new Bitmap("d:\test1.bmp");
    12.     }
    13.  
    14.     public Texture2D BmpToTexture(Bitmap img)
    15.     {
    16.         Texture2D newTex = new Texture2D(img.Width, img.Height);
    17.         for (int x = 0; x < newTex.width; x++)
    18.         {
    19.             for (int y = 0; y < newTex.height; y++)
    20.             {
    21.                 float r = Normalize(img.GetPixel(x, y).R, 0f, 255f);
    22.                 float g = Normalize(img.GetPixel(x, y).G, 0f, 255f);
    23.                 float b = Normalize(img.GetPixel(x, y).B, 0f, 255f);
    24.                 float a = Normalize(img.GetPixel(x, y).A, 0f, 255f);
    25.                 UnityEngine.Color nColor = new UnityEngine.Color(r, g, b, a);
    26.                 newTex.SetPixel(x, y, nColor);
    27.             }
    28.         }
    29.         newTex.Apply();
    30.         return newTex;
    31.     }
    32.  
    33.     public static float Normalize(float current, float min, float max)
    34.     {
    35.         return (current - min) / (max - min);
    36.     }
    37. }
    38.  
    But i'm getting exception on the line:

    Code (csharp):
    1.  
    2. Bitmap bmp = new Bitmap("d:\test1.bmp");
    3.  
    ArgumentException: Illegal characters in path.
    System.IO.Path.IsPathRooted (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:508)
    System.IO.Path.InsecureGetFullPath (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:357)
    System.IO.Path.GetFullPath (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:289)
    System.Drawing.IntSecurity.UnsafeGetFullPath (System.String fileName)
    System.Drawing.IntSecurity.DemandReadFileIO (System.String fileName)
    System.Drawing.Bitmap..ctor (System.String filename)
    (wrapper remoting-invoke-with-check) System.Drawing.Bitmap:.ctor (string)
    BitmapToTexture.Start () (at Assets/MyScripts/BitmapToTexture.cs:10)


    I'm using visual studio csharp for scripting.
    Unity 5.5.1f1 personal
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I did not think that you could even use System.Drawing in Unity.
    You could try : new Bitmap(@"d:\test1.bmp");
     
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    941
    Working by adding this @ and also running unity as admin. Thanks.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah cool. :) Learned something new today. heh. Cheers.
     
    Chocolade likes this.
  5. shahab-mos

    shahab-mos

    Joined:
    Apr 30, 2013
    Posts:
    5
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    To further detail this: The problem was because the \ character is also used as the escape character in strings by c#. So your original file path was reading \t as horizontal tab. That's why using the two slashes works to provide a single slash.

    Its also worth pointing out that you should use one of the various data paths to read your data from, rather then storing it directly on the drive. This will remove the need for administrator rights.

    Choose the one that is appropriate for your use case.

    https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
    https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
    https://docs.unity3d.com/ScriptReference/Application-dataPath.html
     
    Chocolade likes this.