Thumbnailing graphics from a sister site

B

Blasting Cap

I have to change the front page for a sales system I work on, and want
to use some thumbnails of wallpaper that is on a sister site.

The wallpapers are jpegs, and are 640, 800, 1024 & 1280 in size, and
range from around 100k to about 2 megs.

What I want to do is on my web page, keep the aspect ratio, but size
them to be no more than 300 pixels wide.

I've found some code to do this from a database, but can't find any that
create thumbnails on the fly.

Anyone know how to do this or have some code that'll do it?

The source filename is:

11_2006_640.jpg, 12_2006_640.jpg and so on.

Any help appreciated.

BC
 
K

kferron

This is somewhat of a broad category. You are dealing with some
efficiency issues if you are:

(a) making a request to a server to retrieve the byte representation of
the jpg
(b) processing the image (the basic formula youre dealing with is
calculating the new Height as:

ratio = newWidth / currentWidth
newHeight = currentHeight * r;

(c) writing the newly created byte array to the output stream.


To deal with issue (a), you will probably want to look at WebRequest

To deal with issue(b), you will want to look at Bitmap class

To deal with issue(c), you will want to look at Response, in that what
you will be doing with the Bitmap in (b) is saving it to the output
stream


Make sure you're considering a proper caching strategy for this type of
on the fly processing (ie. once you have a thumbnail from the sister
site, hopefully you wouldn't need to resize it very often, if ever)

~kcf
 
B

Blasting Cap

Is there any code doing part b available on the net? I had been doing
some request servervariables in the code, and when I tried to do the
webrequest, that made those stop working.
 
B

Blasting Cap

I've looked at the code there, but I don't know if I'm trying to do
something that can't be easily done. All it ever does is hit the Catch
in the Thumbnail routine.

Here's the code I'm trying pull the image with:

'Dim Image As String = Request.QueryString("Image")
Dim Image As String = Session("filename")
If Image Is Nothing Then
Me.ErrorResult()
Return
End If

Dim sSize As String = Request("Size")
Dim Size As Integer = 120
If Not sSize Is Nothing Then
Size = Int32.Parse(sSize)
End If

'Dim Path As String = Server.MapPath(Request.ApplicationPath) +
"\\" + Image
Dim Path As String =
Server.UrlPathEncode("http://www.mywebsite.com/images/library/downloads/wallpaper/"
+ Image)
Dim bmp As Bitmap = CreateThumbnail(Path, Size, Size)

If bmp Is Nothing Then
Me.ErrorResult()
Return
End If

Dim OutputFilename As String = Nothing
OutputFilename = Request.QueryString("OutputFilename")

If Not OutputFilename Is Nothing Then
If Me.User.Identity.Name = "" Then
' *** Custom error display here
bmp.Dispose()
Me.ErrorResult()
End If
Try
bmp.Save(OutputFilename)
Catch ex As Exception
bmp.Dispose()
Me.ErrorResult()
Return
End Try
End If

' Put user code to initialize the page here
Response.ContentType = "image/jpeg"
bmp.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
bmp.Dispose()


The only thing I'm doing differently is creating the filename, and then
where it's looking for a path, I'm giving it a URL.

The other two pieces of code (CreateThumbnail & the Errorresult) are as
they were:

Public Shared Function CreateThumbnail(ByVal lcFilename As String,
ByVal lnWidth As Integer, ByVal lnHeight As Integer) As Bitmap

Dim bmpOut As System.Drawing.Bitmap = Nothing
Try
Dim loBMP As Bitmap = New Bitmap(lcFilename)
Dim loFormat As ImageFormat = loBMP.RawFormat

Dim lnRatio As Decimal
Dim lnNewWidth As Integer = 0
Dim lnNewHeight As Integer = 0

'*** If the image is smaller than a thumbnail just return it
If loBMP.Width < lnWidth And loBMP.Height < lnHeight Then
Return loBMP
End If

If loBMP.Width > loBMP.Height Then
lnRatio = CType(lnWidth / loBMP.Width, Decimal)
lnNewWidth = lnWidth
Dim lnTemp As Decimal = loBMP.Height * lnRatio
lnNewHeight = CType(lnTemp, Integer)
Else
lnRatio = CType(lnHeight / loBMP.Height, Decimal)
lnNewHeight = lnHeight
Dim lnTemp As Decimal = loBMP.Width * lnRatio
lnNewWidth = CType(lnTemp, Integer)
End If

' System.Drawing.Image imgOut =
' loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
' null,IntPtr.Zero);


' *** This code creates cleaner (though bigger) thumbnails
and properly
' *** and handles GIF files better by generating a white
background for
' *** transparent images (as opposed to black)
bmpOut = New Bitmap(lnNewWidth, lnNewHeight)
Dim g As Graphics = Graphics.FromImage(bmpOut)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)

loBMP.Dispose()
Catch
Return Nothing
End Try

Return bmpOut

End Function


Private Sub ErrorResult()
Response.Clear()
Response.StatusCode = 404
Response.End()
End Sub

All I really want to do is to put a thumbnail of a mm_yyyy_640.jpg
wallpaper from a sister site into a cell of a table as it's displayed on
the front page of my sales info website.

I'd appreciate any assistance in helping me figure out what's wrong with
what I'm trying to do?


BC
 
K

kferron

What you're missing is you can't pass the path like that, you need to
get a file stream of the image youre grabbing
 
K

kferron

What you're missing is you can't pass the path like that, you need to
get a file stream of the image youre grabbing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148
Top