Documents & code‎ > ‎

Website Thumbnail

Website Thumbnail   
post id90 
post length8290 
post datetime5/19/2012 8:30:09 PM 
post ip10.10.10.254 

Website Thumbnail

                          ::Framework 4 


============={ 
correct`ed 6.07.2012 
}======== 

Alternative variant: 




        <ComImport(), _ 
         ComVisible(True), _ 
         Guid("0000010D-0000-0000-C000-000000000046"), _ 
         InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _ 
        Public Interface IViewObject 
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _ 
                Sub Draw(<MarshalAs(UnmanagedType.U4)> ByVal dwAspect As UInt32 _ 
                                                     , ByVal lindex As Integer _ 
                                                     , ByVal pvAspect As IntPtr _ 
                                              , <[In]> ByVal ptd As IntPtr _ 
                                                     , ByVal hdcTargetDev As IntPtr _ 
                                                     , ByVal hdcDraw As IntPtr _ 
                   , <MarshalAs(UnmanagedType.Struct)> ByRef lprcBounds As Rectangle _ 
                                              , <[In]> ByVal lprcWBounds As IntPtr _ 
                                                     , ByVal pfnContinue As IntPtr _ 
                       , <MarshalAs(UnmanagedType.U4)> ByVal dwContinue As UInt32) 
        End Interface 

        Protected Sub GetThumbnail() 
                Using [Web] As WebBrowser = New WebBrowser 
                    [Web].ClientSize = New Size(1024, 768) 
                    '... 
                    AddHandler [Web].NewWindow _ 
                        , New CancelEventHandler(AddressOf [NEW]) 
                    AddHandler [Web].DocumentCompleted _ 
                        , New WebBrowserDocumentCompletedEventHandler(AddressOf [COMPLETED]) 
                    '... 
                    [Web].Navigate(URI) 
                    [Web].Dispose() 
                    'read event <END::OFF> 
                End Using 
        End Sub 

        Protected Sub [NEW](ByVal [obj] As Object, ByVal e As CancelEventArgs) 
            e.Cancel = True 
        End Sub 
        Protected Sub [COMPLETED](ByVal [obj] As WebBrowser _ 
                                , ByVal e As WebBrowserDocumentCompletedEventArgs) 
                Application.DoEvents() _ 
                    : Thread.Sleep(2500) 
                [obj].Stop() 
                        Using As Graphics = Graphics.FromImage([SCR]) 
                              G.Clear(Color.Black) 
                            Dim [ptr] As IntPtr = G.GetHdc 
                            Dim rect As New Rectangle(0, 0, [SCR].Width, [SCR].Height) 
                            TryCast([obj].Document.DomDocument, IViewObject).Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, [ptr], rect, IntPtr.Zero, IntPtr.Zero, 0) 
                            If ([ptr] <> IntPtr.Zero) Then 
                                G.ReleaseHdc([ptr]) 
                            End If 
                        End Using 
                        'Add event <END> 
        End Sub 


============={ 
origin 
}======== 

We used this urlhttp://www.codeproject.com/Articles/119330/Creating-Website-Thumbnails-in-ASP-NET

example and some have improved it by putting in [IHttpHandler] (or [IHttpAsuncHandler]) to prevent 

blocking of [application pool] in processing, and added timeout 

on rendering page.
 



1. Create EXT1 HTTP handler class in [App_Code] directory: 


#Region "IMPORT`S" 

Imports Microsoft.VisualBasic _ 
      , System.Drawing.Imaging _ 
      , System.Windows.Forms _ 
      , System.Web.Caching _ 
      , System.Threading _ 
      , System.Drawing _ 
      , System.Net _ 
      , System.Web _ 
      , System.IO _ 
      , System 

#End Region 

Public Class EXT1 : Implements IHttpHandler 

    Public Class WebsiteThumbnail 

        Public [SCR] As Bitmap = Nothing _ 
               , URI As String = Nothing 

        Sub New(ByVal T As String) 
            Me.URI = T 
        End Sub 

        Public Function PROCESSING() As Bitmap 
            Dim _Thread As Thread _ 
                     = New Thread(New ThreadStart(AddressOf GetThumbnail)) 
            _Thread.SetApartmentState(ApartmentState.STA) 
            _Thread.Start() 
            _Thread.Join() 
            Return [SCR] 
        End Function 

        Protected Sub GetThumbnail() 
            Dim ID As Integer = 0 
            Try 
                Using [Web] As WebBrowser = New WebBrowser 
                    [Web].ClientSize = New Size(1024, 768) 
                    [Web].ScrollBarsEnabled = False 
                    [Web].ScriptErrorsSuppressed = True 
                    [Web].Navigate(URI) 
                    While [Web].ReadyState <> WebBrowserReadyState.Complete 
                        If ID > 500 Then 
                            Exit While 
                        End If 
                        Application.DoEvents() _ 
                                : Thread.Sleep(50) 
                        ID += 1 
                    End While 
                    [SCR] = New Bitmap(1024, 768) 
                    [Web].DrawToBitmap([SCR], New Rectangle(0, 0, 1024, 768)) 
                    [Web].Dispose() 
                End Using 
            Catch ex As Exception 
            End Try 
        End Sub 

    End Class 

    Function CheckBid(ByRef context As HttpContext) As String 
        ' 
        Dim bid As String = context.Request.Url.ToString _ 
          , value As String = Nothing 
        Try 
            Dim arr As String() _ 
                    = bid.Split("?") _ 
              , ID As Integer = 0 
            For Each str As String In arr 
                Select Case ID 
                    Case 
                        ' 
                        ' 
                    Case 
                        value = String.Concat(value, str.Substring(4, str.Length - 4)) 
                    Case Else 
                        value = String.Concat(value, "?", str) 
                End Select 
                ID += 1 
            Next 
        Catch ex As Exception 
        End Try 
        ' 
        Return value 
        '' 
    End Function 

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
        ' 
        Dim bmp As Bitmap _ 
                = New Bitmap(248, 192) _ 
          , G As Graphics _ 
                = Graphics.FromImage(bmp) _ 
          , URI As String _ 
                = Nothing 
        Try 
            URI = CheckBid(context).Split("|")(0).ToLower 
        Catch ex As Exception 
        End Try 
        Try 
            If Not IsNothing(URI) Then 
                Dim [tmp] As Bitmap = New WebsiteThumbnail(URI).PROCESSING() 
                Using [ITextureBrush] As TextureBrush _ 
                                   = New TextureBrush([tmp], New Rectangle(0, 0, 1024, 768)) 
                    ITextureBrush.ScaleTransform(0.24, 0.24, Drawing2D.MatrixOrder.Append) 
                    G.FillRectangle(ITextureBrush, New Rectangle(0, 0, 248, 192)) 
                    ITextureBrush.Dispose() 
                End Using 
                [tmp].Dispose() 
            End If 
        Catch ex As Exception 
        End Try 
        G.Dispose() 
        context.Response.ContentType = "image/jpeg" 
        bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg) 
        bmp.Dispose() 
        ' 
    End Sub 

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable 
        Get 
            Return True 
        End Get 
    End Property 

End Class 


2. Registering the Custom HTTP Handler 

        /Web.config:/ 


<configuration> 
<system.web> 
<httpHandlers> 
      <add verb="*" path="*GET.ASPX"  type="EXT1"/> 
    </httpHandlers> 
</system.web> 
<system.webServer> 
<handlers> 
      <add verb="*" path="*GET.ASPX"  name="EXT1" type="EXT1"/> 
    </handlers> 
</system.webServer> 
</configuration> 


3. Testing: 

requesting a GET.ASPX from the webbrowser... 

EXAMPLE: 

GET.ASPX?pid=http://www.test.com 
Comments