Link to home
Start Free TrialLog in
Avatar of rodmjay
rodmjayFlag for United States of America

asked on

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?
Avatar of naveenkohli
naveenkohli

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.
Avatar of rodmjay

ASKER

Can i just delete the file that same instant?  These arent photos i want hanging around if you know what i mean.
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
Avatar of rodmjay

ASKER

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

That would be perfect solution. This way you can stream the image in response and you would not require any temp files.
Avatar of rodmjay

ASKER

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
Avatar of rodmjay

ASKER

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
Avatar of rodmjay

ASKER

The images dont come out crisp at all, they are really pixelated, are there any known solutions for this?
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rodmjay

ASKER

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.