【发布时间】: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>
【问题讨论】:
-
我想知道stackoverflow.com/questions/426645/…是否相关。尽管我遇到此问题的应用程序将 ImageSource 绑定到从相对 URI 手动创建的 BitmapImage ......在这种情况下,我可能会创建一个自定义转换器来“强制”立即加载图像。跨度>
-
对于任何寻找 sphereinabox 提供的链接的原始内容的人 - 它已移动到 lordzoltan.blogspot.com/2010/09/… - 我已经移动了我的博客,所以现在正在浏览网站管理员工具以尝试更新任何入站链接:)
标签: wpf data-binding bitmap