Per prova ho creato un sito nuovo con una sola pagina Thumbnail.aspx
pagina Thumbnail.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Thumbnail.aspx.vb" Inherits="prove_Thumbnail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
codice
Option Strict On
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.IO
Partial Class prove_Thumbnail
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Try
Dim strPath As String = Request.QueryString("p")
Dim strHeight As Integer = CInt(Request.QueryString("h"))
Dim strWidth As Integer = CInt(Request.QueryString("w"))
Dim img As System.Drawing.Image = Me.GetImage(strPath)
Dim format As ImageFormat = img.RawFormat
Dim size As Size = Me.ThumbSize(img.Width, img.Height, Convert.ToInt16(strWidth))
Dim bitmap As New Bitmap(size.Width, size.Height)
Dim graphics As Graphics = graphics.FromImage(bitmap)
graphics.SmoothingMode = SmoothingMode.HighQuality
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
graphics.DrawImage(img, 0, 0, size.Width, size.Height)
MyBase.Response.ContentType = Me.GetContentType(format)
Dim stream As New MemoryStream
bitmap.Save(stream, format)
stream.WriteTo(MyBase.Response.OutputStream)
img.Dispose()
bitmap.Dispose()
Response.End()
Catch ex As Exception
MyBase.Response.ContentType = "image/jpeg"
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("/tmp/prova.jpg"))
Dim bitmap As New Bitmap(40, 40)
Dim stream As New MemoryStream
bitmap.Save(stream, ImageFormat.Jpeg)
stream.WriteTo(MyBase.Response.OutputStream)
img.Dispose()
bitmap.Dispose()
End Try
End Sub
Private Function ThumbSize(ByVal currentWidth As Integer, ByVal currentHeight As Integer, ByVal newWidth As Integer) As Size
Dim num As Double
If currentWidth > newWidth Then
num = Convert.ToDouble(newWidth) / CDbl(currentWidth)
Else
num = 1
End If
Return New Size(Convert.ToInt16(CDbl((currentWidth * num))), Convert.ToInt16(CDbl((currentHeight * num))))
End Function
Private Function GetContentType(ByVal imageFormat As ImageFormat) As String
If imageFormat.Equals(imageFormat.Bmp) Then
Return "image/bmp"
End If
If imageFormat.Equals(imageFormat.Gif) Then
Return "image/gif"
End If
If imageFormat.Equals(imageFormat.Jpeg) Then
Return "image/jpeg"
End If
If imageFormat.Equals(imageFormat.Png) Then
Return "image/png"
End If
Return ""
End Function
Private Function GetImage(ByVal strPath As String) As System.Drawing.Image
If (strPath.IndexOf("http://") > -1) Or (strPath.IndexOf("https://") > -1) Then
Try
Dim request As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(strPath), System.Net.HttpWebRequest)
request.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim response As System.Net.HttpWebResponse = DirectCast(request.GetResponse, System.Net.HttpWebResponse)
Return System.Drawing.Image.FromStream(response.GetResponseStream)
Catch ex As Exception
Return System.Drawing.Image.FromFile(Server.MapPath("~/Images/Thumbnail.jpg"))
End Try
End If
Return System.Drawing.Image.FromFile(Server.MapPath(strPath))
End Function
End Class
pagina html che richiama l'immagine
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<img alt="" src="http://localhost/prova/Thumbnail.aspx?w=800&p=http://static.panoramio.com/photos/original/216697.jpg" />
</body>
</html>