Ciao a tutti,
vi posto l'intero codice di una pagina che creato per generare delle miniature. Non riesco a capire perchè non funziona.
Imports System
Imports System.Net
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Namespace MySite
Partial Public Class 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 = Request.QueryString("h")
Dim strWidth As Integer = Request.QueryString("w")
Dim img As 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()
Catch ex As Exception
MyBase.Response.ContentType = "image/jpeg"
Dim img As Image = Image.FromFile(Server.MapPath("~/Images/Thumbnail.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 Image
If (strPath.IndexOf("http://") > -1) Or (strPath.IndexOf("https://") > -1) Then
Try
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(strPath), HttpWebRequest)
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
Return Image.FromStream(response.GetResponseStream)
Catch ex As Exception
Return Image.FromFile(Server.MapPath("~/Images/Thumbnail.jpg"))
End Try
End If
Return Image.FromFile(Server.MapPath(strPath))
End Function
End Class
End Namespace
Qualcuno riesce a capire dove sta l'errore?
Grazie mille!
Luca