【问题标题】:Render bound wpf controls off screen to bitmap将绑定的 wpf 控件从屏幕上渲染到位图
【发布时间】:2010-01-09 21:44:31
【问题描述】:

我有一个自定义 wpf 控件来执行任意反向图像扭曲,它使用 Viewport3D 和自定义矩阵,以及带有 ImageBrush 的网格......

我正在尝试将其渲染为完全脱离屏幕的位图。我能够很好地渲染位图(我发现在渲染之前调用 .Measure() .Arrange(new Rectangle(...)) 和 .UpdateLayout()) ,但是绑定尚未完成,因此网格是不可见的(图片刷源尚未加载)...

我错过了什么?是否有等待绑定完成的阻塞方式?

[我处理“Loaded”事件的尝试没有成功...]

更新,示例代码:

示例代码如下。预期的输出是一个 300x300 的蓝色矩形,其中堆栈溢出徽标被挤压/旋转成菱形。 (尝试打开 DiamondControl.xaml 设计器,并更改代码以对图像 url 进行硬编码......有趣的是,这种变化仍然不在呈现的位图中,这表明我的问题与绑定的图像 url 无关,但是图片尚未加载)

Program.cs(添加了对 System.Windows.Presentation 的引用的控制台应用程序)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ConsoleApplication3 {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            // try exporting a bitmap...
            var content = new DiamondControl();
            content.DataContext = new Uri("http://sstatic.net/so/img/logo.png");
            //content.ApplyTemplate(); // this doesn't seem to work

            int width = 300, height = 300;

            var rect = new Rect(0, 0, width, height);
            content.Measure(new Size(width, height));
            content.Arrange(rect);
            //content.UpdateLayout(); // not required in this case, seems to be required if I render the control more than once

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            RenderTargetBitmap render = new RenderTargetBitmap(
                width,
                height,
                96,
                96,
                PixelFormats.Pbgra32);

            render.Render(content);
            encoder.Frames.Add(BitmapFrame.Create(render));
            using (Stream s = File.Open("outputfile.png", FileMode.Create)) {
                encoder.Save(s);
            }
        }
    }
}

DiamondControl.xaml:

<UserControl x:Class="ConsoleApplication3.DiamondControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <Rectangle Fill="CornflowerBlue" />
        <Viewport3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <!-- simple diamond -->
                                <MeshGeometry3D x:Name="mesh"
                            Positions="-.5 0 0, 0 .5 0, 0 -.5 0, .5 0 0"
                            TextureCoordinates="0 1, 0 0, 1 1, 1 0" 
                            TriangleIndices="0 2 1, 2 3 1" />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <DiffuseMaterial>
                                    <DiffuseMaterial.Brush>
                                        <!--<SolidColorBrush Color="Red" />-->
                                        <ImageBrush ImageSource="{Binding}" />
                                        <!--<ImageBrush ImageSource="http://sstatic.net/so/img/logo.png" />-->
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>

                        <!-- Light source. -->
                        <AmbientLight Color="White" />
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>

            <!-- Camera. -->
            <Viewport3D.Camera>
                <OrthographicCamera Position="0 0 1"
                        LookDirection="0 0 -1"
                        UpDirection="0 1 0"
                        Width="1" />
            </Viewport3D.Camera>
        </Viewport3D>
    </Grid>
</UserControl>

【问题讨论】:

标签: wpf data-binding bitmap


【解决方案1】:

如果你的位图对应一个网址,则使用How to render an <image> in a background WPF process?中的解决方案

此外,在您的控件中包含一个 元素(即使大小为 0x0)。我不知道为什么这会奏效,而且看起来完全是一团糟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多