Save+as+Image+using+DrawingImage()+in+WPF

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Amwell.Gps.MapControl.Interface;
using Amwell.Gps.MapControl.General;
using System.Security.Permissions;
using System.IO;

 // Creates thumbnail image form the webbrowser control in wpf

// Or Thumbnail of web pages image created can be easily loaded in any

// image control..

public  string GetThumbnailImage(WebBrowser CurrentBrowser,string filepath)
        {
            Guid guid = Guid.NewGuid();
            //string ThumbnailPath = @"E:\" + guid.ToString() + ".png";
            string ThumbnailPath = filepath + ".png";
            Image imgScreen = new Image();
            imgScreen.Width = CurrentBrowser.ActualWidth;
            imgScreen.Height = CurrentBrowser.ActualHeight;
            imgScreen.Source = new DrawingImage(VisualTreeHelper.GetDrawing(CurrentBrowser));

            FileStream stream = new FileStream(ThumbnailPath, FileMode.Create);

            DrawingVisual vis = new DrawingVisual();
            DrawingContext cont = vis.RenderOpen();
            cont.DrawImage(imgScreen.Source, new Rect(new Size(CurrentBrowser.ActualWidth, CurrentBrowser.ActualHeight)));
            cont.Close();

            RenderTargetBitmap rtb = new RenderTargetBitmap((int)imgScreen.Width,
                (int)imgScreen.Height, 96d, 96d, PixelFormats.Default);
            rtb.Render(vis);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(stream);
            stream.Close();

            return ThumbnailPath;
        }

 

      public void SaveCurrentMapToPicture()
        {
         

          Microsoft.Win32.SaveFileDialog dialogOpenFile = new Microsoft.Win32.SaveFileDialog();
          dialogOpenFile.DefaultExt = "png";//默认扩展名
          dialogOpenFile.AddExtension = true;//是否自动添加扩展名
          dialogOpenFile.Filter = "*.png|.png";
          dialogOpenFile.OverwritePrompt = true;//文件已存在是否提示覆盖
          dialogOpenFile.FileName = "文件名";//默认文件名
          dialogOpenFile.CheckPathExists = true;//提示输入的文件名无效
          dialogOpenFile.Title = "保存当前地图";

          //显示对话框
          bool? b = dialogOpenFile.ShowDialog();
          if (b == true)//点击保存
          {
              GetThumbnailImage(map, dialogOpenFile.FileName);
          }
        }

 

 

  copy from  http://nethelp.wikidot.com/save-as-image-using-drawingimage-in-wpf

thanks

阅读更多
个人分类: .net
想对作者说点什么? 我来说一句
关闭
关闭
关闭