Winform中的SaveFileDialog保存文件

以下是实现代码:
       SaveFileDialog sfd = new SaveFileDialog();
       //设置保存文件对话框的标题
       sfd.Title = "请选择要保存的文件路径" ;
       //初始化保存目录,默认exe文件目录
       sfd.InitialDirectory = Application.StartupPath;
       //设置保存文件的类型
       sfd.Filter = "文本文件|*.txt|音频文件|*.wav|图片文件|*.jpg|所有文件|*.*" ;
       if (sfd.ShowDialog() == DialogResult.OK)
       {
         //获得保存文件的路径
         string filePath = sfd.FileName;
         //保存
         using (FileStream fsWrite = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
         {
           byte [] buffer = Encoding.Default.GetBytes(txt_FileInfo.Text.ToString().Trim());
           fsWrite.Write(buffer, 0, buffer.Length);
         }
       }
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 `PrintDocument` 和 `PrintDialog` 类来打印 WinForm 控件,并使用 `SaveFileDialog` 类来保存打印后的文件。下面是一个简单的示例代码: ```csharp private void btnPrint_Click(object sender, EventArgs e) { // 创建打印文档对象 PrintDocument pd = new PrintDocument(); // 设置打印事件处理程序 pd.PrintPage += new PrintPageEventHandler(this.PrintImage); // 创建打印对话框 PrintDialog printDialog = new PrintDialog(); // 如果用户点击了“确定”按钮 if (printDialog.ShowDialog() == DialogResult.OK) { // 设置打印文档对象的打印机名称 pd.PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName; // 打印文档 pd.Print(); } } private void PrintImage(object sender, PrintPageEventArgs e) { // 获取要打印的控件 Control control = this.Controls[0]; // 创建位图对象 Bitmap bmp = new Bitmap(control.Width, control.Height); // 将控件绘制到位图上 control.DrawToBitmap(bmp, control.ClientRectangle); // 绘制位图到打印页面上 e.Graphics.DrawImage(bmp, 0, 0); // 释放资源 bmp.Dispose(); } private void btnSave_Click(object sender, EventArgs e) { // 创建保存文件对话框 SaveFileDialog saveFileDialog = new SaveFileDialog(); // 设置文件类型 saveFileDialog.Filter = "JPEG图片|*.jpg|位图文件|*.bmp|PNG图片|*.png"; // 如果用户点击了“确定”按钮 if (saveFileDialog.ShowDialog() == DialogResult.OK) { // 获取要保存的控件 Control control = this.Controls[0]; // 创建位图对象 Bitmap bmp = new Bitmap(control.Width, control.Height); // 将控件绘制到位图上 control.DrawToBitmap(bmp, control.ClientRectangle); // 保存位图到文件 bmp.Save(saveFileDialog.FileName); // 释放资源 bmp.Dispose(); } } ``` 你需要将 `this.Controls[0]` 替换为你要打印或保存的控件。此外,你还可以根据需要修改保存文件类型的过滤器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值