首页 新闻 会员 周边

WPF图像

0
悬赏园豆:20 [已关闭问题]

 WPF中如何将界面上的对象元素生成图像文件,比如将一个Button控件生成为图像文件保存。

 请高手指点指点,谢谢!。

Newis的主页 Newis | 初学一级 | 园豆:25
提问于:2009-08-10 16:57
< >
分享
其他回答(1)
0

我也很想知道如何能够直接保存XAML元素为图像。

目前我这里的解决办法只能是通过局部打印,而打印到虚拟打印机(Snagit附带的图片打印机),继而输出图片。

以下方法首先弹出打印设置对话框,然后全幅打印出x:Key为B1的元素:

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog pDialog = new PrintDialog();

            if ((bool)pDialog.ShowDialog().GetValueOrDefault())
            {
                B1.Height = pDialog.PrintableAreaHeight-4;
                B1.Width = pDialog.PrintableAreaWidth-4;
                pDialog.PrintVisual(B1, "Hello, world!");
            }
        }

斯克迪亚 | 园豆:4124 (老鸟四级) | 2009-08-10 23:39
0

private void SaveToImage(FrameworkElement ui, string fileName)
{
System.IO.FileStream fs
= new System.IO.FileStream(fileName, System.IO.FileMode.Create);
RenderTargetBitmap bmp
= new RenderTargetBitmap((int)ui.Width, (int)ui.Height, 96d, 96d, PixelFormats.Pbgra32);
bmp.Render(ui);
BitmapEncoder encoder
= new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
}

 

stam | 园豆:100 (初学一级) | 2010-10-13 15:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册