使用c#wpf保存自定义文件扩展名的文件时,拍摄内容截图并以缩略图显示为文件预览
为了保持简单快捷使用c#wpf保存自定义文件扩展名的文件时,拍摄内容截图并以缩略图显示为文件预览,c#,.net,wpf,C#,.net,Wpf,为了保持简单快捷 我启动了一个记事本(文件扩展名为.txt) 我在打开的记事本文件中写了一些东西 我正在用任何文件名(比如Test)保存记事本文件 文件扩展名[(file extension.dod)-因此文件名为- 测试,国防部] 在系统上保存为Test.dod后,将显示默认窗口 为Test.dod文件提供了图标 我的要求是-而不是默认窗口提供的图标 Test.dod,我想显示中存在的内容的屏幕截图 文件在保存之前,以文件图标或缩略图的形式显示 一个实时完美的例子是:当我们在任何文件夹的“视图
使用系统;
使用System.IO;
使用系统图;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
命名空间缩略图显示
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
private const string IMAGE_PATH=@“E:\Rahul\CSharp\displayThumbnailDisplay\IMAGE\Flower.JPG”;
private const string TITLE=“示例应用程序”;
private const string CONFIRMATION_MESSAGE=“文件以.dod格式复制!”;
private const string INPUT_PATH=@“E:\Rahul\CSharp\displayThumbnailDisplay\ThumbnailDisplay\Icons\Add.png”;
private const string OUTPUT_PATH=@“E:\Rahul\CSharp\displayThumbnailDisplay\GeneratedTextFile\”;
private const string GENERATED_ICO_FILE_PATH=@“E:\Rahul\CSharp\displayThumbnailDisplay\GeneratedTextFile\ScreenCapture.ICO”;
private const string GENERATED_PNG_FILE_PATH=@“E:\Rahul\CSharp\displayThumbnailDisplay\GeneratedTextFile\ScreenCapture.PNG”;
私有常量int最大像素=40;
private const string SCREEN_CAPTURE=“ScreenCapture”;
私有常量字符串ICO_扩展=“.ICO”;
private const string PNG_EXTENSION=“.PNG”;
私有字符串\u sampleFileLocation=@“E:\Rahul\CSharp\DisplayThumbnailDisplay\ThumbnailDisplay\SampleTextFile\Dummy.txt”;
私有字符串_generatedFileCopy=@“E:\Rahul\CSharp\displayThumbnailDisplay\GeneratedTextFile\Test.dod”;
公共主窗口()
{
初始化组件();
SetThumbnailImage();
}
私有void SetThumbnailImage()
{
这个.Title=Title;
Uri缩略图=新Uri(图像路径,UriKind.RelativeOrAbsolute);
this.Icon=BitmapFrame.Create(缩略图);
}
专用系统.Drawing.Size GetThumbnailSize(图像原始图像)
{
双重因素;
int originalWidth=originalImage.Width;
int originalHeight=originalImage.Height;
如果(原始宽度>原始高度)
{
因子=(双)最大像素/原始宽度;
}
其他的
{
因子=(双)最大像素/原始高度;
}
返回新系统.图纸.尺寸((int)(原始宽度*系数),(int)(原始高度*系数));
}
私有无效btnClickMe\u单击(对象发送方,路由目标)
{
if(File.Exists(_sampleFileLocation))
{
if(File.Exists(_generatedFileCopy))
{
文件。删除(_generatedFileCopy);
}
if(File.Exists(生成的文件路径))
{
删除(生成的文件路径);
}
UIElement=scrollViewer.Content作为UIElement;
var位图=新位图(输出路径+屏幕捕获+ICO扩展);
var iconHandle=bitmap.GetHicon();
var icon=System.Drawing.icon.FromHandle(iconHandle);
FileStream FileStream=新FileStream(生成的文件路径,FileMode.OpenOrCreate);
Uri路径=新Uri(生成的文件路径);
CaptureScreen(元素、路径);
Copy(_sampleFileLocation,_generatedFileCopy);
MessageBox.Show(确认消息);
}
}
public void CaptureScreen(UIElement源、Uri目标)
{
尝试
{
双倍高度、渲染灯光、宽度、渲染宽度;
高度=渲染灯光=source.RenderSize.Height;
宽度=renderWidth=source.RenderSize.Width;
//目标位图(如宽度/高度像素等)的规范。
RenderTargetBitmap renderTarget=新的RenderTargetBitmap((int)renderWidth,(int)renderHeight,96,96,PixelFormats.Pbgra32);
//创建元素的可视笔刷
VisualBrush VisualBrush=新的VisualBrush(来源);
DrawingVisual DrawingVisual=新建DrawingVisual();
使用(DrawingContext DrawingContext=drawingVisual.RenderOpen())
{
//绘制元素的图像
drawingContext.DrawRectangle(visualBrush,null,new Rect(new System.Windows.Point(0,0),new System.Windows.Point(Width,Height));
}
//渲染图像
renderTarget.Render(drawingVisual);
//用于创建PNG文件的PNG编码器
PngBitmapEncoder编码器=新的PngBitmapEncoder();
Add(BitmapFrame.Create(renderTarget));
using System;
using System.IO;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ThumbnailDisplay
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string IMAGE_PATH = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\Image\Flower.JPG";
private const string TITLE = "Sample Application";
private const string CONFIRMATION_MESSAGE = "File is Copied in .dod format!";
private const string INPUT_PATH = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\Icons\Add.png";
private const string OUTPUT_PATH = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\GeneratedTextFile\";
private const string GENERATED_ICO_FILE_PATH = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\GeneratedTextFile\ScreenCapture.ico";
private const string GENERATED_PNG_FILE_PATH = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\GeneratedTextFile\ScreenCapture.png";
private const int MAXIMUM_PIXEL = 40;
private const string SCREEN_CAPTURE = "ScreenCapture";
private const string ICO_EXTENSION = ".ico";
private const string PNG_EXTENSION = ".png";
private string _sampleFileLocation = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\SampleTextFile\Dummy.txt";
private string _generatedFileCopy = @"E:\Rahul\CSharp\DisplayThumbnail\ThumbnailDisplay\GeneratedTextFile\Test.dod";
public MainWindow()
{
InitializeComponent();
SetThumbnailImage();
}
private void SetThumbnailImage()
{
this.Title = TITLE;
Uri thumbNail = new Uri(IMAGE_PATH, UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(thumbNail);
}
private System.Drawing.Size GetThumbnailSize(Image originalImage)
{
double factor;
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
if (originalWidth > originalHeight)
{
factor = (double)MAXIMUM_PIXEL / originalWidth;
}
else
{
factor = (double)MAXIMUM_PIXEL / originalHeight;
}
return new System.Drawing.Size((int)(originalWidth * factor), (int)(originalHeight * factor));
}
private void btnClickMe_Click(object sender, RoutedEventArgs e)
{
if (File.Exists(_sampleFileLocation))
{
if (File.Exists(_generatedFileCopy))
{
File.Delete(_generatedFileCopy);
}
if(File.Exists(GENERATED_ICO_FILE_PATH))
{
File.Delete(GENERATED_ICO_FILE_PATH);
}
UIElement element = scrollViewer.Content as UIElement;
var bitmap = new Bitmap(OUTPUT_PATH + SCREEN_CAPTURE + ICO_EXTENSION);
var iconHandle = bitmap.GetHicon();
var icon = System.Drawing.Icon.FromHandle(iconHandle);
FileStream fileStream = new FileStream(GENERATED_ICO_FILE_PATH, FileMode.OpenOrCreate);
Uri path = new Uri(GENERATED_ICO_FILE_PATH);
CaptureScreen(element, path);
File.Copy(_sampleFileLocation, _generatedFileCopy);
MessageBox.Show(CONFIRMATION_MESSAGE);
}
}
public void CaptureScreen(UIElement source, Uri destination)
{
try
{
double Height, renderHeight, Width, renderWidth;
Height = renderHeight = source.RenderSize.Height;
Width = renderWidth = source.RenderSize.Width;
//Specification for target bitmap like width/height pixel etc.
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
//creates Visual Brush of UIElement
VisualBrush visualBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
//draws image of element
drawingContext.DrawRectangle(visualBrush, null, new Rect(new System.Windows.Point(0, 0), new System.Windows.Point(Width, Height)));
}
//renders image
renderTarget.Render(drawingVisual);
//PNG encoder for creating PNG file
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}