Jestliže jste chtěli někde při zadání webové adresy získat její náhled, nebo si uložit nějakou stránku jako obrázek. Pak to lze provést překvapivě jednoduší použitím komponenty WebBrowser. Zde je příklad uložení do formátu TIFF G4:
////// Konverze souboru sejmutím obrazovky webového prohlížeče /// /// /// ///Result CaptureWebPage(string input, string output) { int? width = 1024; int? height = null; // create a hidden web browser, which will navigate to the page using (WebBrowser web = new WebBrowser()) { web.ScrollBarsEnabled = false; // we don't want scrollbars on our image web.ScriptErrorsSuppressed = true; // don't let any errors shine through web.Navigate(input); // wait until the page is fully loaded while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents(); //web.Document.Body.InnerHtml = body; // set the size of our web browser to be the same size as the page if (width == null) width = web.Document.Body.ScrollRectangle.Width; if (height == null) height = web.Document.Body.ScrollRectangle.Height; web.Width = width.Value; web.Height = height.Value; // a bitmap that we will draw to using (System.Drawing.Bitmap tiff = new System.Drawing.Bitmap(width.Value, height.Value)) { // draw the web browser to the bitmap web.DrawToBitmap(tiff, new Rectangle(web.Location.X, web.Location.Y, web.Width, web.Height)); // draw the web browser to the bitmap using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { EncoderParameter qualityParam = null; EncoderParameters encoderParams = null; try { ImageCodecInfo encoderInfo = null; encoderInfo = GetEncoderInfo("image/tiff"); encoderParams = new EncoderParameters(2); EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); encoderParams.Param[0] = parameter; parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); encoderParams.Param[1] = parameter; tiff.Save(output, encoderInfo, encoderParams); tiff.Dispose(); return Result.Succes; } catch { return Result.Exception; } finally { if (encoderParams != null) encoderParams.Dispose(); if (qualityParam != null) qualityParam.Dispose(); } } } } }
Příspěvek vyšel ve Čtvrtek 22.3.2012 15:11 v kategorii Programování a byl 144113x zobrazen. Pokud se vám líbil můžete si jej zalinkovat: Linkuj.cz, Del.icio.us
"A jedeme dál ..." Človíčkův Weblog aneb Michal Horák bloguje. (c) Michal Horák (Človíček webdesign) 2006