WPF将UI保存为图片和保存位图

WPF将UI保存为图片和保存位图

WPF将Ui保存为图片

private void SaveWindowContent(Window source, string fileName)
    {
        FrameworkElement elem = source.Content as FrameworkElement;
        RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
                                   (int)elem.ActualWidth,
                                   (int)elem.ActualHeight,
                                   96d,
                                   96d,
                                   PixelFormats.Default);

        targetBitmap.Render(source);
        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
        // save file to disk
        using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
        {
            encoder.Save(fs);
        }
    } 

调用:

SaveWindowContent(this, "c:\tmp.bmp");

保存位图

private void btnSave_Click(object sender, RoutedEventArgs e)
 {
 		//img 为你的图片控件
        if (img.Source != null) {

            SaveFileDialog op = new SaveFileDialog();
            op.Filter = "JPEG Files(*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp";
            if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
              
                BitmapImage bmp = img.Source as BitmapImage;
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));
                // save file to disk
                using (FileStream fs = File.Open(op.FileName, FileMode.OpenOrCreate))
                {
                    encoder.Save(fs);
                }
            }
        }
  }

原文:https://www.cnblogs.com/wgscd/articles/4918991.html

Winform保存窗体或者控件为图片:

Bitmap bit=new Bitmap(this.Width,this.Height);//实例化一个和窗体一样大的bitmap
Graphics g=Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
g.CopyFromScreen(this.Left,this.Top,0,0,new Size(this.Width,this.Height));//保存整个窗体为图片
//g.CopyFromScreen(panel.PointToScreen(Point.Empty), Point.Empty, panel.Size);//只保存某个控件(这里是panel)
bit.Save("panel.png");//默认保存格式为PNG,保存成jpg格式质量不是很好
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值