0

Once again I'm here with a question..

I want to extract the icon from a file that is stored in azure blob storage.

Action:

public FileResult GetUploadedFileIcon(string blobname)
{
    string SASURI = GetUploadedFileIconSASURL(blobname);

    Icon stream = System.Drawing.Icon.ExtractAssociatedIcon(SASURI);
    Bitmap pngIcon = stream.ToBitmap();

    MemoryStream ms = new MemoryStream();
    pngIcon.Save(ms, ImageFormat.Png);

    ms.Position = 0;

    return new FileStreamResult(ms, "image/png");
}

View:

<img src="@Url.Action("GetUploadedFileIcon", "Home", new { blobname = file.BlobName })" />

The problem:

It's telling me icon stream is null. Nothing is wrong with the SASURI. I tried removing it completely and go with public container but that did not help. I also tried getting the icon from a locally stored file, like this:

Icon stream = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");

And that worked..

The content type for all the blobs are correct, textfiles are text/plain etc.

Why am i getting null value on my icon when I try to extract the icon from a blob?

And as always i appreciate all the help I can get!!

Thanks

UPDATE:

Found a solution here:

Link: ExtractAssociatedIcon returns null

But for some reason I'm not getting any icons in my view now(?)

My code:

[DllImport("shell32.dll")]
    static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,
       StringBuilder lpIconPath, out ushort lpiIcon);

    public FileResult GetUploadedFileIcon(string blobname)
    {
        string SASURI = GetUploadedFileIconSASURL(blobname);

        ushort uicon;
        StringBuilder strB = new StringBuilder(SASURI);
        IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
        Icon ico = Icon.FromHandle(handle);

        Bitmap pngIcon = ico.ToBitmap();

        MemoryStream ms = new MemoryStream();
        pngIcon.Save(ms, ImageFormat.Png);

        ms.Position = 0;

        return new FileStreamResult(ms, "image/png");
    }
Community
  • 1
  • 1
Reft
  • 2,333
  • 5
  • 36
  • 64
  • What is SASURI? Is it an actual URI? Or is it a file path? It looks like the ExtractAssociatedIcon only takes a file path, not a URI. – Tim Jun 10 '14 at 14:10
  • Shared access signature. Its a direct address to the blob. Im not sure, i think its a URI but.. I dont know.. – Reft Jun 10 '14 at 14:19
  • But I tried with the real URL aswell with a public container but it didnt help so i dont think its that – Reft Jun 10 '14 at 14:20
  • ExtractAssociatedIcon is literally looking for a local file path. I used reflector to look at the code and it does a check to make sure it is a file path. So you can't pass it a uri to a remote resource. – Tim Jun 10 '14 at 14:44
  • So my only choice is to save the icon? :/ – Reft Jun 10 '14 at 14:50
  • Or find a different method to extract the icon other than the built-in way. – Tim Jun 10 '14 at 14:54
  • Found a solution but i'm abit lost. If youre still interested in helping me check out my update! Thanks:) – Reft Jun 10 '14 at 15:52
  • Looking at the ExtractAssociatedIcon Win32 API documentation (http://msdn.microsoft.com/en-us/library/windows/desktop/ms648067(v=vs.85).aspx), I don't think it works with remote URIs either. It looks like it works with UNC paths, whereas the .NET way does not. But I'm guessing your `IntPtr handle` is always `Zero` this way (because you're using a remote/http url) – Tim Jun 10 '14 at 16:18

0 Answers0