【VB.NET】サーバー側でブラウザの画面キャプチャ処理を実行する【メモ】

:::note warn
こちらはおよそ 3 年程度前に書かれたメモとなっております。
:::

下書きに残っていてもったいないので投下。

```javascript
    var mng = Sys.WebForms.PageRequestManager.getInstance();

    //非同期通信開始
    mng.add_initializeRequest(function (sender, args) {
        要素.disabled = true;
    });

    //非同期通信終了
    mng.add_endRequest(function (sender, args) {
        要素.disabled = false;
    });

```

```vb:呼び出し
Dim websiteToImage As New WebsiteToImage(fileUrl, width * zoom, height * zoom)
Dim img As Bitmap = websiteToImage.Generate()
```

```vb
Public Class WebsiteToImage
    Private m_Bitmap As Bitmap
    Private m_Url As String
    Private m_width As Integer
    Private m_height As Integer
    Public Sub New(url As String, width As Integer, height As Integer)
        m_Url = url
        m_width = width
        m_height = height
    End Sub
    Public Function Generate() As Bitmap
        Dim m_thread = New Thread(AddressOf _Generate)
        m_thread.SetApartmentState(ApartmentState.STA)
        m_thread.Start()
        m_thread.Join()
        Return m_Bitmap
    End Function
    Private Sub _Generate()
        Dim browser = New WebBrowser
        browser.ScrollBarsEnabled = False
        browser.ScriptErrorsSuppressed = True
        browser.Navigate(m_Url)
        AddHandler browser.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
        Do While browser.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        browser.Dispose()
    End Sub
    Private Sub WebBrowser_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        Dim browser = DirectCast(sender, WebBrowser)
        browser.ClientSize = New Size(m_width, m_height)
        browser.ScrollBarsEnabled = False
        m_Bitmap = New Bitmap(m_width, m_height, Imaging.PixelFormat.Format24bppRgb)
        browser.BringToFront()
        browser.DrawToBitmap(m_Bitmap, browser.Bounds)
    End Sub

End Class
```

```vb
Namespace CssClassController
    Module CssClassExtension
        <Extension()> Public Sub AddClass(Element As WebControl, ClassName As String)
            Dim SetClassList() As String = (Element.CssClass + " " + ClassName).Split(" "c)
            Dim ClassList As New List(Of String)
            For Each _ClassName As String In SetClassList
                _ClassName = _ClassName.Trim()
                If _ClassName <> "" AndAlso Not ClassList.Contains(_ClassName) Then
                    ClassList.Add(_ClassName)
                End If
            Next
            Element.CssClass = String.Join(" ", ClassList.ToArray())
        End Sub
        <Extension()> Public Sub RemoveClass(Element As WebControl, ClassName As String)
            Dim SetClassList() As String = Element.CssClass.Split(" "c)
            Dim ClassList As New List(Of String)
            For Each _ClassName As String In SetClassList
                _ClassName = _ClassName.Trim()
                ClassName = ClassName.Trim()
                If _ClassName <> "" AndAlso _ClassName <> ClassName AndAlso Not ClassList.Contains(_ClassName) Then
                    ClassList.Add(_ClassName)
                End If
            Next
            Element.CssClass = String.Join(" ", ClassList.ToArray())
        End Sub
    End Module
End Namespace
```