Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to extract the file icon from a locally stored file.

Home controller:
public byte[] Icon()
{
    Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(@"C:/Users/Filip/Desktop/test.txt");

    Bitmap iconPng = icon.ToBitmap();

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        iconPng.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }
}

View:
< img src="@Url.Action("Icon", "Home")" />

Why is this not working? All im getting is a small picture of an X. What am i doing wrong?

Appreciate all the help you can give me, thanks!
Posted
Updated 9-Jun-14 1:24am
v2
Comments
[no name] 9-Jun-14 6:58am    
I really doubt that test.txt contains an image that you can extract an image from. http://msdn.microsoft.com/en-us/library/system.drawing.icon.extractassociatedicon.aspx
Filip Danielsson 9-Jun-14 7:02am    
Oh have I completely misunderstood this? I want the fileimage from a file. A txt file has a notepad image, the image that you see when the file is stored on the desktop. I want that image, not something that is inside the file. Is this even possible to extract? Am i doing it all wrong? Thanks!
[no name] 9-Jun-14 7:27am    
Yes, you are doing it wrong. You need to go read the documentation.

1 solution

By definition img element src attribute must have a url that points to an image...
In your case you push into it some binary value but img try to interpret it as url - that the reason for the X (missing image) picture.
There is a non-standard, but mostly supported way of pushing binary data into img element, but in that case the format is a bit different...
HTML
<img src="data:image/jpeg;base64{binary data in base64}" />

---
I've never done it in MVC, but have a sample in WebForms, that may give you the direction...
C#
//pic.aspx
public<pre> partial class Pic : Page
{
  protected override void OnLoad ( EventArgs e )
  {
    // get the image file from page query parameters
    byte[ ] bBuffer = File.ReadAllBytes( szPath );

    Response.Clear( );

    Response.Buffer = true;

    // send to browser
    Response.OutputStream.Write( bBuffer, 0, bBuffer.Length );

    Response.Flush( );

    Response.End( );</pre>
  }
}

HTML
<!-- usage in web page -->
<img atl="me" src="Pic.aspx?path=me.jpg" />

It's can be done also without real files as long as you can get the binary content of the image, let say using memory stream or some sort of thing...
 
Share this answer
 
v2
Comments
Filip Danielsson 9-Jun-14 8:01am    
So it would be better to just save the icon image, in my case as a azure blob, because I heard IE6 doesnt support this. Then I dont have to stream anything right? Just extract icon and save it?
Also, I have never ever done this before, what do I need to insert into {binary data in base64}?

Big thanks
Kornfeld Eliyahu Peter 9-Jun-14 8:48am    
Added a kind of solution to my answer...
Joezer BH 10-Jun-14 6:50am    
5ed!
Kornfeld Eliyahu Peter 10-Jun-14 6:55am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900