日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

wpf 对控件进行截图,获取快照

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 wpf 对控件进行截图,获取快照 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時候我們項目,在執行某個操作后,會生成一些數據結果,如報表一類的東西,我們需要對結果進行保存,甚至是生成word文檔。

那么首先獲取到控件快照就最基本的條件。

生成快照的靜態方法類

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;namespace MvvmFuncationApp.Model
{public static class ToBitmapTool{/// <summary>/// 截圖轉換成bitmap/// </summary>/// <param name="element"></param>/// <param name="width">默認控件寬度</param>/// <param name="height">默認控件高度</param>/// <param name="x">默認0</param>/// <param name="y">默認0</param>/// <returns></returns>public static Bitmap ToBitmap(this FrameworkElement element, int width = 0, int height = 0, int x = 0, int y = 0){if (width == 0) width = (int)element.ActualWidth;if (height == 0) height = (int)element.ActualHeight;var rtb = new RenderTargetBitmap(width, height, x, y, System.Windows.Media.PixelFormats.Default);rtb.Render(element);var bit = BitmapSourceToBitmap(rtb);//測試代碼DirectoryInfo d = new DirectoryInfo(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Cache"));if (!d.Exists) d.Create();bit.Save(System.IO.Path.Combine(d.FullName, "控件截圖.png"));return bit;}/// <summary>/// BitmapSource轉Bitmap/// </summary>/// <param name="source"></param>/// <returns></returns>public static Bitmap BitmapSourceToBitmap(this BitmapSource source){return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight);}/// <summary>/// Convert BitmapSource to Bitmap/// </summary>/// <param name="source"></param>/// <returns></returns>public static Bitmap BitmapSourceToBitmap(this BitmapSource source, int width, int height){Bitmap bmp = null;try{PixelFormat format = PixelFormat.Format24bppRgb;/*set the translate type according to the in param(source)*/switch (source.Format.ToString()){case "Rgb24":case "Bgr24": format = PixelFormat.Format24bppRgb; break;case "Bgra32": format = PixelFormat.Format32bppPArgb; break;case "Bgr32": format = PixelFormat.Format32bppRgb; break;case "Pbgra32": format = PixelFormat.Format32bppArgb; break;}bmp = new Bitmap(width, height, format);BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),ImageLockMode.WriteOnly,format);source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);bmp.UnlockBits(data);}catch{if (bmp != null){bmp.Dispose();bmp = null;}}return bmp;}}
}

?

添加按鈕事件:

        private void BitMapBtn_Click(object sender, RoutedEventArgs e){
       //
StudentInfoGrid =======>控件的名稱
        ToBitmapTool.ToBitmap(StudentInfoGrid); }
     }

前臺代碼:

 <Grid Background="Gray" Name="StudentInfoGrid"><Label Content="學號" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" /><Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" /><Label Content="年齡" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" /><Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" /><Label Content="性別" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" /><Button Command="{Binding ShowCommand}" Content="顯示" Height="23" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75" /><Button Content="控件截圖" Name="BitMapBtn" Click="BitMapBtn_Click"Width="60" Height="30" Margin="0,30"></Button></Grid>

運行程序 ==》進入項目的debug目錄下

?

?

?

轉載于:https://www.cnblogs.com/likui-bookHouse/p/11114924.html

總結

以上是生活随笔為你收集整理的wpf 对控件进行截图,获取快照的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。