
WPF 截取控件
发布日期:2021-05-04 13:34:08
浏览次数:18
分类:技术文章
本文共 3164 字,大约阅读时间需要 10 分钟。
public static class ToBitmapTool { ////// 截图转换成bitmap /// /// /// 默认控件宽度 /// 默认控件高度 /// 默认0 /// 默认0 ///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; } /// /// BitmapSource转Bitmap /// /// ///public static Bitmap BitmapSourceToBitmap(this BitmapSource source) { return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight); } /// /// Convert BitmapSource to Bitmap /// /// ///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; } }
System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string outDir = folderBrowserDialog.SelectedPath; //CutScreen名称 System.Drawing.Bitmap bitmap= ToBitmapTool.ToBitmap(CutScreen); bitmap.Save(System.IO.Path.Combine(outDir, Guid.NewGuid().ToString() + ".png")); AutoCloseFrom ac = new AutoCloseFrom(); ac.Show(); }
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年03月12日 06时05分45秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
关于@Transient 注解的使用
2019-03-03
基于DFA算法实现文章敏感词过滤
2019-03-03
Git commit代码后撤销方法
2019-03-03
数据结构与算法学习1-----稀疏数组
2019-03-03
java手动实现JWT(我和别人的不一样)
2019-03-03
LetCode刷题记录--No3-无重复字符的最长子串
2019-03-03
Java转换xml格式时间 (yyyy-MM-ddTHH:mm:ss.SSSZ)
2019-03-03
Python 使用 __getstate__ 和 __setstate__ 魔法方法
2019-03-03
ts从入门到进阶—4.2类
2019-03-03
hook钩子介绍
2019-03-03
关于json
2019-03-03
字符串详解
2019-03-03
焦点事件
2019-03-03
webpack打包常见报错
2019-03-03
微信小程序--08数据与列表渲染
2019-03-03
微信小程序--11事件
2019-03-03
js版数据结构与算法—5.6最大区间
2019-03-03
css系列—水平垂直左右居中
2019-03-03
vuex—1vuex初始
2019-03-03
axios服务器通信—1axios介绍和使用mock数据
2019-03-03