我有一个旋转图像的viewport3D。我必须在代码后面构建它。如果我将视口放在网格中,一切正常。但是,如果我尝试使用RenderTargetBitmap渲染它,则会出现一些问题,并且我总是得到一个空图像。
这是我的代码:
Viewport3D vp = new Viewport3D();
PerspectiveCamera came = new PerspectiveCamera();
came.Position = new Point3D(0,0,4);
vp.Camera = came;
ModelVisual3D light = new ModelVisual3D();
AmbientLight ambLight = new AmbientLight(Colors.White);
light.Content = ambLight;
vp.Children.Add(light);
Viewport2DVisual3D v2d = new Viewport2DVisual3D();
MeshGeometry3D geom3d = new MeshGeometry3D();
geom3d.Positions = new Point3DCollection() {new Point3D(-1,1,0), new Point3D(-1,-1,0), new Point3D(1,-1,0), new Point3D(1,1,0)};
geom3d.TextureCoordinates = new PointCollection(){new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0)};
geom3d.TriangleIndices = new Int32Collection(){0,1,2,0,2,3};
v2d.Geometry=geom3d;
DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Black));
Viewport2DVisual3D.SetIsVisualHostMaterial(mat, true);
v2d.Material = mat;
v2d.Transform = new RotateTransform3D( new AxisAngleRotation3D( new Vector3D(0, 1, 0), 45));
Image img = new Image();
BitmapImage bmpRend = new BitmapImage();
bmpRend.BeginInit();
bmpRend.UriSource = new Uri(@"/WpfProva;component/Images/06_09_2012_09_57_46.jpg", UriKind.Relative);
bmpRend.EndInit();
img.Source = bmpRend; //cam.CAM1_Image_act;
img.Width=200;
img.Height=200;
//Button btn = new Button();
//btn.Content = "Bottone 3D";
//btn.Width = 50;
//btn.Height = 50;
v2d.Visual = img;
vp.Children.Add(v2d);
//grdContainer.Children.Add(vp);
int wd = 500; //(int)vp.ActualWidth;
int ht = 500; //(int)vp.ActualHeight;
vp.Width = wd;
vp.Height = ht;
vp.Measure(new Size(wd, ht));
vp.Arrange(new Rect(0, 0, wd, ht));
//Viewbox viewbox = new Viewbox();
//viewbox.Child = vp; //control to render
//viewbox.Measure(new System.Windows.Size(wd/20, ht/20));
//viewbox.Arrange(new Rect(0, 0, wd/5, ht/5));
//viewbox.UpdateLayout();
RenderTargetBitmap bmpRender = new RenderTargetBitmap(wd, ht, 300, 300, PixelFormats.Pbgra32);
bmpRender.Render(vp);
using (FileStream outStream = new FileStream(@"C:\mycanvas.png", FileMode.Create))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmpRender));
enc.Save(outStream);
}
我在InitializeComponent()之后调用此函数。
答案 0 :(得分:0)
您必须将其添加到visual并让它渲染,然后才能使用RenderTargetBitmap。
所以在xaml中添加viewport3d
<Grid x:Name="baseGd">
<Viewport3D x:Name="vp"/>
<Button Content="Click" Click="Render_click" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Content="save" Click="Save_click" HorizontalAlignment="Center" VerticalAlignment="Top"/>
</Grid>
然后在render_click渲染它
private void Render_click(object sender, RoutedEventArgs e)
{
PerspectiveCamera came = new PerspectiveCamera();
came.Position = new Point3D(0, 0, 4);
vp.Camera = came;
ModelVisual3D light = new ModelVisual3D();
AmbientLight ambLight = new AmbientLight(Colors.White);
light.Content = ambLight;
vp.Children.Add(light);
Viewport2DVisual3D v2d = new Viewport2DVisual3D();
MeshGeometry3D geom3d = new MeshGeometry3D();
geom3d.Positions = new Point3DCollection() { new Point3D(-1, 1, 0), new Point3D(-1, -1, 0), new Point3D(1, -1, 0), new Point3D(1, 1, 0) };
geom3d.TextureCoordinates = new PointCollection() { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0) };
geom3d.TriangleIndices = new Int32Collection() { 0, 1, 2, 0, 2, 3 };
v2d.Geometry = geom3d;
DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Black));
Viewport2DVisual3D.SetIsVisualHostMaterial(mat, true);
v2d.Material = mat;
v2d.Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 45));
Image img = new Image();
BitmapImage bmpRend = new BitmapImage();
bmpRend.BeginInit();
bmpRend.UriSource = new Uri(@"image-1.jpg", UriKind.Relative);
bmpRend.EndInit();
img.Source = bmpRend;
img.Width = 200;
img.Height = 200;
img.Source = bmpRend;
v2d.Visual = img;
vp.Children.Add(v2d);
int wd = 500;
int ht = 500;
vp.Width = wd;
vp.Height = ht;
vp.Measure(new Size(wd, ht));
vp.Arrange(new Rect(0, 0, wd, ht));
}
并保存
private void Save_click(object sender, RoutedEventArgs e)
{
RenderTargetBitmap rtb = new RenderTargetBitmap((int)vp.ActualWidth, (int)vp.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(vp);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
string tempFilename = @"g:\mycanvas1.png";
using (Stream stm = File.Create(tempFilename))
{
png.Save(stm);
}
}
也请点击此link获取更多帮助。
解决方法:在render_click
中添加 DispatcherTimer tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromMilliseconds(10);
tmr.Tick += (snd,ee) =>
{
Save_click(null,null);
tmr.Stop();
};
tmr.Start();