0

我正在使用 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;
}
4

1 回答 1

0

所以,我在做同样的事情,我在渲染文件时遇到了很多问题。我发现在 XAML 中使用到位图的绑定会有所帮助。我的视图模型中返回图像源的代码是:

    public Uri ImageUri
    {
        get { return new Uri(ImagePath, UriKind.Absolute); }
    }

    public BitmapImage ImageSource
    {
        get
        {
            try
            {
                if (string.IsNullOrEmpty(ImagePath) || !File.Exists(ImagePath))
                    return null;

                var image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.UriSource = ImageUri;
                image.EndInit();

                return image;
            }
            catch (Exception e)
            {
                var logger = LogManager.GetLogger(typeof(ImageDetails));
                ExceptionHelper.LogExceptionMessage(logger, e);
            }

            return null;
        }
    }

然后在 XAML 中我绑定到 ImageSource 属性。

我认为 RenderTargetBitmap 的大多数问题都与 XAML 中的异步绑定有关,因为渲染方法是同步的。

于 2018-04-19T14:59:54.617 回答