Skip to main content
2 of 3
added image for desired output
Xeon
  • 308
  • 1
  • 9

Unity3D sprite texture problem

I'm trying to extract file icons from the files inside a folder (eg: Program Files), and have them set as my Sprite's texture. I extracted the file icons, converted them to Bitmaps and then to byte array, and call the LoadImage(byte[] byte) function in my Sprite object. But when I test, the textures become the image belows. Any idea how to fix this? I just want to set my sprites texture as the extracted file icon, I'm creating something similar to a file explorer.

this is the result that i get

enter image description here

this is what I should get if I navigate to the notepad++ folder (eg : "C:\Program Files(x86)\Notepad++\")

enter image description here

this is my settings for the texture (supposedly, later being replaced with the extracted icons) enter image description here

Here's my code

public UnityEngine.UI.Image targetImg;
public void Load(string directory) 
{
    Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(directory);
    Bitmap bmp = icon.ToBitmap();
    targetImg.sprite.texture.LoadImage(ImageToByte(bmp));
}

public static byte[] ImageToByte(Bitmap img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
Xeon
  • 308
  • 1
  • 9