亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Winform實現(xiàn)將網(wǎng)頁生成圖片的方法

 更新時間:2014年09月24日 15:51:02   投稿:shichen2014  
這篇文章主要介紹了Winform實現(xiàn)將網(wǎng)頁生成圖片的方法,類似于一般瀏覽器自帶的網(wǎng)頁生成圖片的功能,需要的朋友可以參考下

通常瀏覽器都有將網(wǎng)頁生成圖片的功能,本文實例講述了Winform實現(xiàn)將網(wǎng)頁生成圖片的方法。分享給大家供大家參考。具體方法如下:

工具截圖如下:

生成后的圖片如下:

手動填寫網(wǎng)站地址,可選擇圖片類型和保持圖片地址,來生成頁面的圖片,當圖片路徑未選擇時則保存桌面;

具體代碼如下:

將html生成圖片的類

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 
using System.Security;
namespace Print
{
  public class Test
  {
    public static Bitmap GetHtmlImage(Uri UrlString, int Width)
    {
      WebBrowser MyControl = new WebBrowser();
      MyControl.Size = new Size(Width, 10);
      MyControl.Url = UrlString;
      while (MyControl.ReadyState != WebBrowserReadyState.Complete)
      {
        Application.DoEvents();
      }
      MyControl.Height = MyControl.Document.Body.ScrollRectangle.Height + 20;
      MyControl.Url = UrlString;
      WebControlImage.Snapshot snap = new WebControlImage.Snapshot();
      Bitmap MyImage = snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(0, 0, MyControl.Width, MyControl.Height));
      MyControl.Dispose();
      return MyImage;
    }
    /// 
    /// WebBrowser獲取圖形 
    /// 
    private class WebControlImage
    {
      internal static class NativeMethods
      {
        [StructLayout(LayoutKind.Sequential)]
        public sealed class tagDVTARGETDEVICE
        {
          [MarshalAs(UnmanagedType.U4)]
          public int tdSize;
          [MarshalAs(UnmanagedType.U2)]
          public short tdDriverNameOffset;
          [MarshalAs(UnmanagedType.U2)]
          public short tdDeviceNameOffset;
          [MarshalAs(UnmanagedType.U2)]
          public short tdPortNameOffset;
          [MarshalAs(UnmanagedType.U2)]
          public short tdExtDevmodeOffset;
        }
        [StructLayout(LayoutKind.Sequential)]
        public class COMRECT
        {
          public int left;
          public int top;
          public int right;
          public int bottom;
          public COMRECT()
          {
          }
          public COMRECT(Rectangle r)
          {
            this.left = r.X;
            this.top = r.Y;
            this.right = r.Right;
            this.bottom = r.Bottom;
          }
          public COMRECT(int left, int top, int right, int bottom)
          {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
          }
          public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height)
          {
            return new NativeMethods.COMRECT(x, y, x + width, y + height);
          }
          public override string ToString()
          {
            return string.Concat(new object[] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom });
          }
        }
        [StructLayout(LayoutKind.Sequential)]
        public sealed class tagLOGPALETTE
        {
          [MarshalAs(UnmanagedType.U2)]
          public short palVersion;
          [MarshalAs(UnmanagedType.U2)]
          public short palNumEntries;
        }
      }
      public class Snapshot
      {
        /// 
        /// ?煺? 
        /// 
        /// Com 對象 
        /// 圖象大小 
        /// 
        public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect)
        {
          if (pUnknown == null)
            return null;
          //必須為com對象 
          if (!Marshal.IsComObject(pUnknown))
            return null;
          //IViewObject 接口 
          UnsafeNativeMethods.IViewObject ViewObject = null;
          IntPtr pViewObject = IntPtr.Zero;
          //內(nèi)存圖 
          Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height);
          Graphics hDrawDC = Graphics.FromImage(pPicture);
          //獲取接口 
          object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown),
          ref UnsafeNativeMethods.IID_IViewObject, out pViewObject);
          try
          {
            ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject;
            //調(diào)用Draw方法 
            ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT,
            -1,
            IntPtr.Zero,
            null,
            IntPtr.Zero,
            hDrawDC.GetHdc(),
            new NativeMethods.COMRECT(bmpRect),
            null,
            IntPtr.Zero,
            0);
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            throw ex;
          }
          //釋放 
          hDrawDC.Dispose();
          return pPicture;
        }
      }
      [SuppressUnmanagedCodeSecurity]
      internal static class UnsafeNativeMethods
      {
        public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}");
        [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IViewObject
        {
          [PreserveSig]
          int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [In] NativeMethods.COMRECT lprcBounds, [In] NativeMethods.COMRECT lprcWBounds, IntPtr pfnContinue, [In] int dwContinue);
          [PreserveSig]
          int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet);
          [PreserveSig]
          int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
          [PreserveSig]
          int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);
          void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink);
          void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(UnmanagedType.LPArray)] System.Runtime.InteropServices.ComTypes.IAdviseSink[] pAdvSink);
        }
      }
    }
  }
}

winfrom后臺處理方面代碼如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Excel文件處理
{
  public partial class Html : Form
  {
    public Html()
    {
      InitializeComponent();
    }
    private string ImageUrl = "";//圖片地址
    private string ImageName = "";//圖片名稱
    private void button1_Click(object sender, EventArgs e)
    {
      string HtmlUrl = this.Txt_Url.Text.Trim(); 
      if (HtmlUrl=="")
      {
        MessageBox.Show("請輸入網(wǎng)址");
        return;
      } 
      if (ImageUrl.Trim()=="")
      { 
        ImageUrl = @"C:\Users\Administrator\Desktop";  
      }
      try
      {
        Uri ri = new Uri(this.Txt_Url.Text);
        Bitmap bit = Print.Test.GetHtmlImage(ri, 1200);
        ImageName = this.Txt_Name.Text.Trim();//圖片名稱
        if (ImageName != "")
        {
          if (ImageName.IndexOf('.') != -1)
          {//當用戶輸入圖片后綴時,將后綴截取
            ImageName.Substring(0, ImageName.LastIndexOf('.'));
          }
        }
        else
          ImageName = DateTime.Now.Ticks.ToString();//時間名稱
        switch (this.comboBox1.SelectedText)
        {
          case "GIF": ImageUrl += "\\" + ImageName + ".gif"; break;
          case "JPG": ImageUrl += "\\" + ImageName + ".jpg"; break;
          case "PNG": ImageUrl += "\\" + ImageName + ".png"; break;
          default: ImageUrl += "\\" + ImageName + ".png"; break;
        }

        switch (this.comboBox1.SelectedText)
        {
          case "GIF": bit.Save(ImageUrl, ImageFormat.Gif); break;
          case "JPG": bit.Save(ImageUrl, ImageFormat.Jpeg); break;
          case "PNG": bit.Save(ImageUrl, ImageFormat.Png); break;
          default: bit.Save(ImageUrl, ImageFormat.Png); break;
        }

        bit = null;
        ImageUrl = "";//圖片地址
        ImageName = "";//圖片名稱
        MessageBox.Show("生產(chǎn)成功");
      }
      catch
      {
        MessageBox.Show("網(wǎng)址輸入有誤!");
        return;
      }
    }

    private void button2_Click(object sender, EventArgs e)
    { 
      //獲取保存路徑
      if (this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
      {
        if (this.folderBrowserDialog1.SelectedPath.Trim()!="")
        {
          ImageUrl = folderBrowserDialog1.SelectedPath;
          this.label6.Text = ImageUrl;
        }
      }
    }
  }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • C#使用winform實現(xiàn)進度條效果

    C#使用winform實現(xiàn)進度條效果

    這篇文章主要為大家詳細介紹了C#使用winform實現(xiàn)進度條效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)

    C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)

    本文主要介紹了C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C#實現(xiàn)將32位MD5摘要串轉(zhuǎn)換為128位二進制字符串的方法

    C#實現(xiàn)將32位MD5摘要串轉(zhuǎn)換為128位二進制字符串的方法

    這篇文章主要介紹了C#實現(xiàn)將32位MD5摘要串轉(zhuǎn)換為128位二進制字符串的方法,涉及C#字符串遍歷、加密與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解

    C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解

    List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來都很簡單,這篇文章主要給大家介紹了關(guān)于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • C#實現(xiàn)圖片縮略圖功能的示例詳解

    C#實現(xiàn)圖片縮略圖功能的示例詳解

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)圖片縮略圖的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • 詳解C#如何讀寫config配置文件

    詳解C#如何讀寫config配置文件

    這篇文章主要介紹了詳解C#如何讀寫config配置文件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 最新評論