rodmjay
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_relative Path))
Dim l_bmp As Bitmap = New Bitmap(l_filePath)
Dim l_newBmp As Bitmap = CType(l_bmp.GetThumbnailIm age(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?
Private Sub FormatThumbnail(ByVal a_relativePath As String, ByVal a_image As Image)
'Initialize variables
Dim l_filePath As String = Server.MapPath(ResolveUrl(
Dim l_bmp As Bitmap = New Bitmap(l_filePath)
Dim l_newBmp As Bitmap = CType(l_bmp.GetThumbnailIm
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?
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
Something like the following link..
http://www.netomatix.com/Products/FsCleaner/Default.aspx
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/100 1_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
I could use something like this <asp:image imageurl="Images.ashx?id=/
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.
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.ProcessReques t
context.Response.ContentTy pe = "image/jpg"
'Get the image
Dim sPath As String = context.Request.QueryStrin g("id")
Dim sFilePath As String = context.Server.MapPath("~/ App_Themes /Huntn/Ima ges/Associ ates/1002_ thumb1.jpg ")
Dim bmp As Bitmap = New Bitmap(sFilePath)
'Manipulate the image
bmp.Save(context.Response. OutputStre am, 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
<%@ 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.ProcessReques
context.Response.ContentTy
'Get the image
Dim sPath As String = context.Request.QueryStrin
Dim sFilePath As String = context.Server.MapPath("~/
Dim bmp As Bitmap = New Bitmap(sFilePath)
'Manipulate the image
bmp.Save(context.Response.
'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
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.ProcessReques t
context.Response.ContentTy pe = "image/jpeg"
'Get the image
Dim sPath As String = context.Request.QueryStrin g("id")
Dim sFilePath As String = context.Server.MapPath("~/ App_Themes /Huntn/Ima ges/Associ ates/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(iWid th, iHeight, Nothing, IntPtr.Zero)
'Manipulate the image
'context.Response.Clear()
newBmp.Save(context.Respon se.OutputS tream, 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
<%@ 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.ProcessReques
context.Response.ContentTy
'Get the image
Dim sPath As String = context.Request.QueryStrin
Dim sFilePath As String = context.Server.MapPath("~/
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(iWid
'Manipulate the image
'context.Response.Clear()
newBmp.Save(context.Respon
'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
ASKER
The images dont come out crisp at all, they are really pixelated, are there any known solutions for this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.