I'm trying to add little finesse to my page for file downloads. I have a page that recursively searches for all files saved in a specific folder on the server. It creates categories and links for the downloads on the fly based on the naming of the folders
the files are saved to.
I have been manually creating images to put next to the links based on the file's extension. What I would like to do is actually extract the ICON image from the file and then use it as the image placed next to the link.
Here's the code I've been playing around with, which seems to be on the right track. But all I get for output is a bunch of garbage across the screen:
<%@ Import Namespace = "System.Drawing" %>
<%@ Import Namespace = "System.Drawing.Imaging" %>
<script runat="server">
Sub Page_Load()
Dim ico As System.Drawing.Icon = (System.Drawing.Icon.ExtractAssociatedIcon("C:\WINDOWS\system32\notepad.exe"))
Dim bmp As System.Drawing.Bitmap = ico.ToBitmap()
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
End Sub
</script>
<html>
<body>
</body>
</html>
If anyone has any idea what I'm doing wrong here I would appreciate the feed back. The code samples that I've looked perhaps have made this look a lot easier than it really is.
If you just do a .Save("folder/icon.bmp") it works fine, so it's the way you are trying to put it into your OutputStream.
What I would do would be to create a page for nothing but streaming the icon, then create an <img...> tag for each entry as you render it, passing the SRC=YourStreamPage.aspx?id=filename and then in YourStreamPage take the filename param and get the file,
streaming it back.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Clear()
Try
If Not Request.QueryString Is Nothing AndAlso Not Request.QueryString("id") Is Nothing Then
...your code to get the image here....
Else
'DEFAULT - NO IMAGE
Response.ContentType = "image/png"
Response.WriteFile(Server.MapPath("~/images/default.png"))
End If
Catch ex As Exception
'DEFAULT - NO IMAGE
Response.ContentType = "image/png"
Response.WriteFile(Server.MapPath("~/images/default.png"))
End Try
Response.End()
End Sub
Have you read the book? - ASP.Net 3.5 CMS Development (now on Kindle)
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
Loganix77
Participant
1351 Points
412 Posts
Extracting and Displaying ICONs
Apr 12, 2012 04:01 PM|LINK
I'm trying to add little finesse to my page for file downloads. I have a page that recursively searches for all files saved in a specific folder on the server. It creates categories and links for the downloads on the fly based on the naming of the folders the files are saved to.
I have been manually creating images to put next to the links based on the file's extension. What I would like to do is actually extract the ICON image from the file and then use it as the image placed next to the link.
Here's the code I've been playing around with, which seems to be on the right track. But all I get for output is a bunch of garbage across the screen:
<%@ Import Namespace = "System.Drawing" %> <%@ Import Namespace = "System.Drawing.Imaging" %> <script runat="server"> Sub Page_Load() Dim ico As System.Drawing.Icon = (System.Drawing.Icon.ExtractAssociatedIcon("C:\WINDOWS\system32\notepad.exe")) Dim bmp As System.Drawing.Bitmap = ico.ToBitmap() bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif) End Sub </script> <html> <body> </body> </html>If anyone has any idea what I'm doing wrong here I would appreciate the feed back. The code samples that I've looked perhaps have made this look a lot easier than it really is.
Thanks in advance for any help!
Curt_C
All-Star
66014 Points
7639 Posts
Moderator
Re: Extracting and Displaying ICONs
Apr 30, 2012 07:19 PM|LINK
it's the way you are trying to Save it.
If you just do a .Save("folder/icon.bmp") it works fine, so it's the way you are trying to put it into your OutputStream.
What I would do would be to create a page for nothing but streaming the icon, then create an <img...> tag for each entry as you render it, passing the SRC=YourStreamPage.aspx?id=filename and then in YourStreamPage take the filename param and get the file, streaming it back.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Clear()
Try
If Not Request.QueryString Is Nothing AndAlso Not Request.QueryString("id") Is Nothing Then
...your code to get the image here....
Else
'DEFAULT - NO IMAGE
Response.ContentType = "image/png"
Response.WriteFile(Server.MapPath("~/images/default.png"))
End If
Catch ex As Exception
'DEFAULT - NO IMAGE
Response.ContentType = "image/png"
Response.WriteFile(Server.MapPath("~/images/default.png"))
End Try
Response.End()
End Sub
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
Loganix77
Participant
1351 Points
412 Posts
Re: Extracting and Displaying ICONs
Apr 30, 2012 07:56 PM|LINK
Works like a champ Curt! Thanks for chiming in!