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");
}