2
\$\begingroup\$

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[]));
}
\$\endgroup\$
  • \$\begingroup\$ Can you post on what they are meant to look like? \$\endgroup\$ – Ahkam Nihardeen Jan 9 '15 at 1:34
  • \$\begingroup\$ I have updated the question \$\endgroup\$ – Xeon Jan 9 '15 at 4:52
1
\$\begingroup\$

As far as I know, for accessing bitmap's pixel data, you should use LockBits() function:

https://msdn.microsoft.com/en-us/library/5ey6h79d(v=vs.110).aspx

Something like this:

var bitmapData = scaledBitmap.LockBits(...);
var length = bitmapData.Stride * bitmapData.Height;
byte[] bytes = new byte[length];
// load image
targetImg.sprite.texture.LoadImage(ImageToByte(bmp));
scaledBitmap.UnlockBits(bitmapData);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.