Saving images to temp folder or alternative solution

I am generating thumbnails of the fly using the following code:

    Private Sub FormatThumbnail(ByVal a_relativePath As String, ByVal a_image As Image)

        'Initialize variables
        Dim l_filePath As String = Server.MapPath(ResolveUrl(a_relativePath))
        Dim l_bmp As Bitmap = New Bitmap(l_filePath)
        Dim l_newBmp As Bitmap = CType(l_bmp.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero), Bitmap)

    End Sub

The trick is getting it to display withing an <asp:image tag.  I need some sort of url to this file, but I dont need to send this file permanently, so i was thinking a temp folder, how could i do this?
LVL 8
rodmjayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

naveenkohliCommented:
1. Create a folder where you want to store TNails.
2. MAke sure that ASPNET or Network Server user (depending on OS) has permisisons to write to this folder.
3. After you have generated the thumbnail image, call Save method on Bitmap object and specify path to this temp folder.
4. And assign ImageUrl property of Image control to this path in temp folder.
0
rodmjayAuthor Commented:
Can i just delete the file that same instant?  These arent photos i want hanging around if you know what i mean.
0
naveenkohliCommented:
No you can't delete the file immediately. You need to run some kind of cleanup utility that will cleanup the folder at specified interval of time.
Something like the following link..

http://www.netomatix.com/Products/FsCleaner/Default.aspx
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

rodmjayAuthor Commented:
The more i am thinking, I think it would be pretty cool if i could do something like this... tell me if it would work

I could use something like this  <asp:image imageurl="Images.ashx?id=/images/1001_web.jpg" runat="server"/>

The images handler will take the url variable and display the image in the page, what are your thoughts on this

0
naveenkohliCommented:
That would be perfect solution. This way you can stream the image in response and you would not require any temp files.
0
rodmjayAuthor Commented:
Here is the jist of my code, i still have not implemented the resizing but that is the easiest part

<%@ WebHandler Language="VB" Class="Thumbnail" %>

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web

Public Class Thumbnail : Implements IHttpHandler
   
    'Constants that will be userd for this request
    Private Const WIDTH As Integer = 100
    Private Const HEIGHT As Integer = 100
   
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "image/jpg"
       
        'Get the image
        Dim sPath As String = context.Request.QueryString("id")
        Dim sFilePath As String = context.Server.MapPath("~/App_Themes/Huntn/Images/Associates/1002_thumb1.jpg")
        Dim bmp As Bitmap = New Bitmap(sFilePath)
       
        'Manipulate the image
        bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg)
       
        'Kill the object
        bmp.Dispose()
       
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
0
rodmjayAuthor Commented:
Here is the final

<%@ WebHandler Language="VB" Class="Thumbnail" %>

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web

Public Class Thumbnail : Implements IHttpHandler
   
    'Constants that will be userd for this request
    Private Const WIDTH As Integer = 100
    Private Const HEIGHT As Integer = 100
   
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "image/jpeg"
       
        'Get the image
        Dim sPath As String = context.Request.QueryString("id")
        Dim sFilePath As String = context.Server.MapPath("~/App_Themes/Huntn/Images/Associates/1002_thumb1.jpg")
        Dim bmp As Bitmap = New Bitmap(sFilePath)
       
        'Working height and width
        Dim iHeight As Integer = bmp.Height
        Dim iWidth As Integer = bmp.Width
       
        'Get aspect ratio of the image
        Dim sngAspect As Single = iWidth / iHeight
       
        If iWidth > iHeight Then
            iWidth = WIDTH
            iHeight = iWidth / sngAspect
        ElseIf iWidth <= iHeight Then
            iHeight = HEIGHT
            iWidth = iHeight * sngAspect
        End If
       
        Dim newBmp As Bitmap = bmp.GetThumbnailImage(iWidth, iHeight, Nothing, IntPtr.Zero)
       
        'Manipulate the image
        'context.Response.Clear()
        newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg)
       
        'Kill the object
        bmp.Dispose()
        newBmp.Dispose()
       
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
0
rodmjayAuthor Commented:
The images dont come out crisp at all, they are really pixelated, are there any known solutions for this?
0
naveenkohliCommented:
The problem is that you have fixed the size of thumbnail to 100x100. And depending on size of original image that may not give you best result. You may want to look into proportionate sizing.
Another issue is that GetThumbnailImage is not the best way to create thumbnail. Follow this procedure..

1. Create a new Bitmap graphics object to the size that you need for thumbnail.
2. Set the quality properties on the graphics object. For example BiCubic interpolation, Anti-aliasing.
2. Use Draw mthod to draw the orriginal image on this bitmap. One of the overloads of Draw mthod lets you supply the sizes.
4. Then save this image in PNG or GIF form.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rodmjayAuthor Commented:
Actually the problem was quite simple, i was using a thumbnail to begin with (:.  It works fine with normal images, but thanks for the great info.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.