我正在屏幕外创建一个 Viewport3D (CurViewport3D) 并仅用一个平面填充它(用于测试目的)。
通过 RenderTargetBitmap 和 BitmapEncoder 创建此视口内容(作为 jpg)的 MemoryStream。整个功能都放在一个库 (dll) 中,因为我想在 Web 服务器上使用这个库来创建一个要在网站上显示的图像。
下面的代码被简化为基础,但它正在工作。
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Threading;
namespace RenderTargetImage_Test
{
public class RenderTargetImage01
{
public System.IO.MemoryStream stream = new System.IO.MemoryStream();
Int32 width = 100;
Int32 height = 100;
Viewport3D CurViewport3D;
public RenderTargetImage01()
{
CurViewport3D = new Viewport3D();
Model3DGroup myModel3DGroup = new Model3DGroup();
ModelVisual3D myModelVisual3D = new ModelVisual3D();
PerspectiveCamera myPCamera = new PerspectiveCamera();
myPCamera.Position = new Point3D(3500, 1500, -5000);
myPCamera.LookDirection = new Vector3D(-0.5, 0, 1);
myPCamera.FieldOfView = 70;
CurViewport3D.Camera = myPCamera;
AmbientLight myAmbientLight = new AmbientLight();
myAmbientLight.Color = Colors.White;
myModel3DGroup.Children.Add(myAmbientLight);
MeshGeometry3D meshS2 = new MeshGeometry3D();
GeometryModel3D modelS2 = new GeometryModel3D();
meshS2.Positions.Add(new Point3D(500, 500, -500));
meshS2.Positions.Add(new Point3D(500, 3000, -500));
meshS2.Positions.Add(new Point3D(3000, 500, -500));
meshS2.Positions.Add(new Point3D(3000, 3000, -500));
meshS2.TriangleIndices.Add(0);
meshS2.TriangleIndices.Add(1);
meshS2.TriangleIndices.Add(2);
meshS2.TriangleIndices.Add(3);
meshS2.TriangleIndices.Add(2);
meshS2.TriangleIndices.Add(1);
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.Normals.Add(new Vector3D(0, 0, 1));
meshS2.TextureCoordinates.Add(new System.Windows.Point(0, 0));
meshS2.TextureCoordinates.Add(new System.Windows.Point(1, 0));
meshS2.TextureCoordinates.Add(new System.Windows.Point(0, 1));
meshS2.TextureCoordinates.Add(new System.Windows.Point(1, 1));
modelS2.Geometry = meshS2;
DiffuseMaterial DiffMS2 = new DiffuseMaterial(System.Windows.Media.Brushes.Red);
modelS2.Material = DiffMS2;
myModel3DGroup.Children.Add(modelS2);
myModelVisual3D.Content = myModel3DGroup;
CurViewport3D.Children.Add(myModelVisual3D);
// render
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
CurViewport3D.Width = width;
CurViewport3D.Height = height;
CurViewport3D.Measure(new System.Windows.Size(width, height));
CurViewport3D.Arrange(new Rect(0, 0, width, height));
CurViewport3D.Dispatcher.Invoke(((Action)(() => renderTargetBitmap.Render(CurViewport3D))), DispatcherPriority.Render);
BitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
bitmapEncoder.Save(stream);
}
}
}
这是网站的代码:
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
以及背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using RenderTargetImage_Test;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RenderTargetImage01 rti = new RenderTargetImage01();
rti.stream.WriteTo(Response.OutputStream);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
}
}
很简单!
现在的问题是:本地一切正常。图像显示在浏览器中 - 完美。一旦我尝试在 Web 服务器(Microsoft Server 2008)上运行它,浏览器中显示的图像是空的,没有内容。没有错误。
有谁知道如何让它在网络服务器上运行?