• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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?
0
rodmjay
Asked:
rodmjay
  • 6
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now