0

Datazen ダッシュボード レポートを画像ファイルに保存する必要があります。

レポートは、iframe を介して MVC ビューでホストされます。基礎となるレポートは、Datazen 内の Active Directory 認証によって保護されます。

STA モードで起動されたスレッドで WebBrowser コントロールを使用することを考えていました。この種の作品ですが、次のような URL を表示しようとすると、ログイン画面が表示されます。

http://MyServerAddress/viewer/dashboard?dashboardguid=17EF844E-DEBE-4FE5-B22E-CD6F74A9E6C9

これは私がこれまでに持っているコードです。

    public ActionResult Save()
    {
        var url = "http://MyServerAddress/viewer/dashboard?dashboardguid=17EF844E-DEBE-4FE5-B22E-CD6F74A9E6C9";

        FileContentResult result = null;
        Bitmap bitmap = null;

        var thread = new Thread(
            () =>
            {
                bitmap = ExportUrlToImage(url, 1280, 1024);
            });

        thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
        thread.Start();
        thread.Join();

        if (bitmap != null)
        {
            using (var memstream = new MemoryStream())
            {
                bitmap.Save(memstream, ImageFormat.Jpeg);
                result = this.File(memstream.GetBuffer(), "image/jpeg");
            }
        }

        return result;
    }

    private Bitmap ExportUrlToImage(string url, int width, int height)
    {
        // Load the webpage into a WebBrowser control
        WebBrowser wb = new WebBrowser();
        wb.ScrollBarsEnabled = false;
        wb.ScriptErrorsSuppressed = true;

        string hdr = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("username" + ":" + "password")) + System.Environment.NewLine;


        wb.Navigate(url, null, null, hdr);
        while (wb.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

        // Set the size of the WebBrowser control
        wb.Width = width;
        wb.Height = height;

        Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
        wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
        wb.Dispose();

        return bitmap;
    }

私が軌道に乗っていて、別のアプローチを見逃していないかどうかを確認したいですか?

4

1 に答える 1

0

最終的に、次のクライアント ライブラリHtml2Canvasを使用することにしました。

CORS の問題に注意する必要があるため、Datazen iframe が Web ページと同じサーバー/ポートなどでホストされていることを確認してください。

ページ全体 (つまり、親ページと iframe コンテンツ) のレンダリングに問題があったため、iframe コンテンツのみをエクスポートすることになりました。

$(document).ready(function () {

        $("#btnSave").click(function () {
            // just render the body of the datazen iframe 
            var body = $("#iframe").contents().find('body');
            html2canvas(body, {
                allowTaint: true,
                onrendered: function (canvas) {
                    // canvas is the final rendered <canvas> element
                    var myImage = canvas.toDataURL("image/png");
                    window.open(myImage);
                }
            })
        });
于 2015-11-06T14:49:17.920 に答える