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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值