Hi Chris:
Here's a page I wrote awhile ago using VB.NET that gets an image from a
remote location, then makes a thumbnail out of it. It'll be easy enough to
grab the image from a local file, and convert this code to C#, once you know
the classes that I've used to accomplish this. I know this is a C#
newsgroup, but all I had is my old VB.NET example handy.
----------
Sub Page_load(Sender As Object, E As EventArgs)
Dim thisURL As String
Dim Req As WebRequest
Dim Resp As WebResponse
Dim myImage As System.Drawing.Image
Dim oThumbNail As System.Drawing.Image
Dim scale As Decimal
Dim newHeight As Integer
Dim newwidth As Integer
Dim oGraphic As Graphics
Dim oRectangle As Rectangle
dim result as Boolean = false
scale = 0.4
' DEFAULT TO SHOW WATERMARK
dim ShowWaterMark as boolean = true
try
if (request.querystring("M") = "N") then
ShowWaterMark = false
end if
catch
ShowWaterMark = true
end try
thisURL = request.querystring("url")
Dim url As New Uri(thisURL)
' Get the eternal image
Req = WebRequest.Create(url)
Try
Resp = Req.GetResponse
Catch exc As Exception
MsgBox(exc.Message)
Exit Sub
End Try
' make sure we got an image back
If Resp.ContentType.IndexOf("image") <> -1 Then
' Load up original Image
' This is where you would load the image from local file instead
myImage = System.Drawing.Image.FromStream(Resp.GetResponseStream)
' setup new size
' if a new height/width is not specified on the querystring, use the
scale.
if len(request.querystring("h") & "") > 0 then
newHeight = request.querystring("h")
newwidth = request.querystring("w")
else
newHeight = scale * myImage.Height
newwidth = scale * myImage.Width
end if
' create a new image
oThumbNail = New Bitmap(newwidth, newHeight, myImage.PixelFormat)
oGraphic = Graphics.FromImage(oThumbNail)
oGraphic.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighSpeed
oGraphic.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighSpeed
oGraphic.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oRectangle = New Rectangle(0, 0, newwidth, newHeight)
oGraphic.DrawImage(myImage, oRectangle)
if (ShowWaterMark) then
' Draw the watermark
' Setup Drawing objects
Dim semiTransBrush As SolidBrush = New
SolidBrush(Color.FromArgb(128, 0, 102, 153))
Dim drawFont As New Font("TarzanaNarrowBold", 18)
' How to draw text
oGraphic.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias
Dim WaterMarkString As String = "Watermark!"
Dim textSize As SizeF = oGraphic.MeasureString(WaterMarkString,
drawFont)
Dim drawPoint As New PointF((newwidth - textSize.Width),
newHeight * 0.6)
oGraphic.DrawString(WaterMarkString, drawFont, semiTransBrush,
drawPoint)
end if
Response.ContentType = "image/Jpeg"
' THIS DUMPS THE BINARY IMAGE OUT THE RESPONSE STREAM
oThumbNail.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
oThumbNail.Dispose()
result = True
End If
If Not result Then
Response.Redirect("/img/noThumbnail2.gif")
End If