2012年6月27日 星期三

Convert url to bitmap and then WPF(Image)

Ref: Wait to add

1. There are some examples to save webpage into bitmap.

public System.Drawing.Bitmap SaveWebPage2Image(string url)
        {
            // Load the webpage into a WebBrowser control
            System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);

            while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }

            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;

            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();

            return bitmap;
        }
2. xaml file
 <Grid>
        <Image Name="im" />
    </Grid>
3. Add System.Drawing and System.Windows.Forms
4. Window_Loaded
   private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            //convert System.Drawing.Image to WPF image
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://www.google.com.tw");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            im.Source = WpfBitmap;
        }
5. 

沒有留言:

張貼留言