如何提高BlendEffect质量? Lumia Imaging 3.0& UWP

时间:2016-09-08 05:58:38

标签: c# uwp windows-10 alphablending lumia-imaging-sdk

我正在开发一个将文本放入背景图像的应用程序。文本块作为子项放在画布控件上,我将画布渲染为PNG。然后我使用BlendEffect将它与背景图像混合。

更新 这篇文章特别讲述了Lumia Imaging SDK的链接效果。不幸的是,评论的人之一就是拥有隧道视觉,并坚持认为我必须了解有损和无损图像之间的区别。一个意见对于告诉孩子如何在我的上下文中保存Microsoft Paint中的图像非常有用。在其他一些评论中,he even arrogantly wish that I must be having tons of bugs in my app and am having some mental problem

我在这里了解Lumia Imaging,显然我遇到了一个没有使用SDK经验的人并坚持要炫耀。然后投了这篇文章,让自己感觉更好。

有了这个说法,@ Martin Liversage指出它不仅仅是JPEG文物。我尝试使用几个链接步骤和选项,同时保存为JPEG,实际上图像的显示方式不同。虽然保存为PNG确实提高了质量,但它仍然不是我所期望的。所以我在这里要求任何有个人经验的人使用SDK,我可以在代码中做些什么来改善我的结果。

这是我的代码

private async void saveChainedEffects(StorageFile file)
{
    var displayInformation = DisplayInformation.GetForCurrentView();
    var renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(Textify.CanvasControl);
    var width = renderTargetBitmap.PixelWidth;
    var height = renderTargetBitmap.PixelHeight;
    IBuffer textBuffer = await renderTargetBitmap.GetPixelsAsync();
    byte[] pixels = textBuffer.ToArray();

    using (InMemoryRandomAccessStream memoryRas = new InMemoryRandomAccessStream())
    {
        //Encode foregroundtext to PNG
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memoryRas);

        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                             BitmapAlphaMode.Straight,
                             (uint)width,
                             (uint)height,
                             displayInformation.LogicalDpi,
                             displayInformation.LogicalDpi,
                             pixels);

        await encoder.FlushAsync();

        IImageProvider effectBackground;

        if (SelectedEffect.Name == "No Effect")
        {
            effectBackground = imageProcessorRenderer.M_Source;
        }
        else
        {
            effectBackground = (SelectedEffect.GetEffectAsync(imageProcessorRenderer.M_Source, new Size(), new Size())).Result;
        }

        StreamImageSource streamForeground = new StreamImageSource(memoryRas.AsStream());

        //Sharpening the text has unintended consequences to I set to 0d
        using (SharpnessEffect sharpnessEffect = new SharpnessEffect(streamForeground, 0d) )
        using (BlendEffect blendEffect = new BlendEffect(effectBackground, sharpnessEffect, BlendFunction.Normal, 1.0f))
        {
            string errorMessage = null;
            Debug.WriteLine("M_SourceSize (Normalized) {0}", imageProcessorRenderer.M_SourceSize);
            Debug.WriteLine("PreviewSize {0}", imageProcessorRenderer.PreviewSize);

            try
            {
                using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
                using (var jpegRenderer = new JpegRenderer(blendEffect) { Size = imageProcessorRenderer.M_SourceSize, Quality = 1.0, RenderOptions = RenderOptions.Mixed })
                {
                    IBuffer jpegBuffer = await jpegRenderer.RenderAsync().AsTask().ConfigureAwait(false);
                    await filestream.WriteAsync(jpegBuffer);
                    await filestream.FlushAsync();
                }
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                var dialog = new MessageDialog(errorMessage);
                await dialog.ShowAsync();
            }
        }
    }
}

这里是我的电脑屏幕上显示的图像,然后保存为JPEG

enter image description here

然后,当图像保存到JPG时,质量明显下降,如下所示。放大图像并注意字体的边缘。

enter image description here

如果我希望得到与原始图像质量一样接近的选择,那么我的选择是什么?

1 个答案:

答案 0 :(得分:0)

从@Martin Liversage和@Hans Passant反馈,我决定避免使用JPEG并保存到PNG。字体边缘的文物大多消失了。我现在是客人,我只需要使用图像锐化来获得我想要的结果。

<强>更新 我仍然没有达到我想要的质量水平所以它不仅仅是不使用JPEG而且一切都将是完美的。因此,问题是如何提高BlendEffect质量。

这是我的代码

private async void saveChainedEffects(StorageFile file)
{
    var displayInformation = DisplayInformation.GetForCurrentView();
    var renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(Textify.CanvasControl);
    var width = renderTargetBitmap.PixelWidth;
    var height = renderTargetBitmap.PixelHeight;
    IBuffer textBuffer = await renderTargetBitmap.GetPixelsAsync();
    byte[] pixels = textBuffer.ToArray();

    using (InMemoryRandomAccessStream memoryRas = new InMemoryRandomAccessStream())
    {
        //Encode foregroundtext to PNG
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memoryRas);

        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                             BitmapAlphaMode.Straight,
                             (uint)width,
                             (uint)height,
                             displayInformation.LogicalDpi,
                             displayInformation.LogicalDpi,
                             pixels);

        await encoder.FlushAsync(); 

        IImageProvider effectBackground;

        if (SelectedEffect.Name == "No Effect")
        {
            effectBackground = imageProcessorRenderer.M_Source;
        }
        else
        {
            effectBackground = (SelectedEffect.GetEffectAsync(imageProcessorRenderer.M_Source, new Size(), new Size())).Result;
        }

        StreamImageSource streamForeground = new StreamImageSource(memoryRas.AsStream());

        //Sharpen the text
        //using (SharpnessEffect sharpnessEffect = new SharpnessEffect(streamForeground, 0.3d) )
        using (BlendEffect blendEffect = new BlendEffect(effectBackground, streamForeground, BlendFunction.Normal, 1.0f))
        using (BitmapRenderer bitmapRenderer = new BitmapRenderer(blendEffect, ColorMode.Bgra8888))
        {


            Bitmap bitmap = await bitmapRenderer.RenderAsync();
            byte[] pixelBuffer = bitmap.Buffers[0].Buffer.ToArray();

            using (var stream = new InMemoryRandomAccessStream())
            {
                var pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream).AsTask().ConfigureAwait(false);

                pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8, 
                    BitmapAlphaMode.Straight, 
                    (uint)bitmap.Dimensions.Width, 
                    (uint)bitmap.Dimensions.Height, 
                    displayInformation.LogicalDpi, 
                    displayInformation.LogicalDpi, 
                    pixelBuffer);

                await pngEncoder.FlushAsync().AsTask().ConfigureAwait(false);

                //Need an IBuffer, here is how to get one:
                using (var memoryStream = new MemoryStream())
                {
                    memoryStream.Capacity = (int)stream.Size;
                    var ibuffer = memoryStream.GetWindowsRuntimeBuffer();
                    await stream.ReadAsync(ibuffer, (uint)stream.Size, InputStreamOptions.None).AsTask().ConfigureAwait(false);
                    string errorMessage = null;

                    try
                    {
                        using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
                        {                                 
                            await filestream.WriteAsync(ibuffer);
                            await filestream.FlushAsync();
                        }
                    }
                    catch (Exception exception)
                    {
                        errorMessage = exception.Message;
                    }

                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        var dialog = new MessageDialog(errorMessage);
                        await dialog.ShowAsync();
                    }
                }
            }                
        }
    }
}

更新2

我已经解决了这个问题,并希望分享我的调查结果以防任何人处于类似情况。事实证明,保存为JPEG或PNG并非如此。虽然PNG确实改善了最终图像,但您必须注意以下内容

  1. 确保前景和背景图像具有相似的尺寸。 Lumia Imaging将默认拉伸前景以覆盖背景,并且不会尊重纵横比。

  2. 如果您需要调整前景的大小,请在renderTargetBitmap.RenderAsync中使用允许您指定大小的重载。

  3. 避免使用SoftwareBitmapRenderer调整前景的大小,尤其是在混合前使用透明图像时。对我来说,这影响了我的图像保真度,并因某种原因缩小了颜色范围。

  4. 这是预先渲染前景的方式,它与背景完美融合。

    private async Task<SoftwareBitmap> renderForeground()
    {
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    
        await renderTargetBitmap.RenderAsync(gridToRender, (int)imageProcessorRenderer.M_SourceSize.Width, (int)imageProcessorRenderer.M_SourceSize.Height);
        var width = renderTargetBitmap.PixelWidth;
        var height = renderTargetBitmap.PixelHeight;
        IBuffer textBuffer = await renderTargetBitmap.GetPixelsAsync();
    
        //Get the output into a software bitmap.
         var outputBitmap = new SoftwareBitmap(
             BitmapPixelFormat.Bgra8,
             width,
             height,
             BitmapAlphaMode.Premultiplied);
    
        outputBitmap.CopyFromBuffer(textBuffer);
    
        return outputBitmap;
    }