我正在使用 WPF 对象在内存中生成位图图像。执行此操作的程序驻留在 WCF Web 服务中。当我在 IISExpress 和测试 IIS 7 服务器上本地运行时,图像可以正确呈现。但是,在 QA 使用的服务器上运行时,图像无法正确呈现。更具体地说,仅渲染 250px 高度图像的顶部 22px 行。测试服务器和 QA 服务器上的设置应该是相同的(在此处插入怀疑的面孔)。
问题:IIS 中的哪些可能设置会影响此图像渲染?另外,我认为可能存在线程问题,因为 RenderTargetBitmap 是异步渲染的,并且我确实得到了部分图像。
这是我正在使用的代码:
private byte[] RenderGauge(ViewData viewData)
{
double resolution = 4 * ReSize;
double dpi = 96 * resolution;
var view = new Gauge();
var vm = new GuageViewModel(viewData);
view.Measure(new Size(350, 70));
view.Arrange(new Rect(new Size(350, 70)));
var bounds = VisualTreeHelper.GetDescendantBounds(view);
if (bounds != Rect.Empty)
{
height = (int)(Math.Floor(bounds.Height) + 1);
width = (int)(Math.Floor(bounds.Width) + 1);
size = new Size(width, height);
}
var bitmap = new RenderTargetBitmap((int)(width * resolution), (int)(height * resolution), dpi, dpi, PixelFormats.Pbgra32);
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
var brush = new VisualBrush(view);
context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
}
bitmap.Render(visual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
byte[] img;
using (var MS = new MemoryStream())
{
encoder.Save(MS);
img = MS.ToArray();
}
img = img == null ? new byte[0] : img;
return img;
}