弹来弹去跑马灯!

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

WPF将Ui保存为图片

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

    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);
        }
    } 

保存位图:
---------保存Image图片(原始图片)---------------------------------

//img 为你的图片控件

private void btnSave_Click(object sender, RoutedEventArgs e)
{

        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);
                }
            }

        }

  }
posted @ 2015-10-28 22:56  wgscd  阅读(640)  评论(0)    收藏  举报