SaveJpeg造成"阴影"具有透明度的工件

本文关键字:quot 透明度 阴影 造成 SaveJpeg | 更新日期: 2023-09-27 18:14:06

只是想和你确认一件事。我正在使用WriteableBitmap来创建一个图像,我作为一个活动的瓷砖。工作很好,但我注意到,文字是得到一个阴影周围的边缘。使文本看起来有点脏或凌乱。

请看下面的图片。左边的部分是使用WriteableBitmap创建的动态磁贴,右边的部分是Windows Phone标准磁贴(Internet Explorer磁贴)。看到区别了吗?

http://img268.imageshack.us/img268/8749/unled2imo.png http://img268.imageshack.us/img268/8749/unled2imo.png

我能做点什么吗?你以前注意过这个吗?

编辑:嗯,我想我看错函数了。我想可能是wbmp。是什么导致了这个问题?我将文本和背景图像放到网格中,然后用wbmp.SaveJpeg保存。是这个原因吗?有解决方法吗?

string sIsoStorePath = @"'Shared'ShellContent'tile.png";
using (IsolatedStorageFile appStorage =     IsolatedStorageFile.GetUserStoreForApplication())
{
    //ensure directory exists
    String sDirectory = System.IO.Path.GetDirectoryName(sIsoStorePath);
    if (!appStorage.DirectoryExists(sDirectory))
    {
        appStorage.CreateDirectory(sDirectory);
    }
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(sIsoStorePath, System.IO.FileMode.Create, appStorage))
    {
        wbmp.SaveJpeg(stream, 173, 173, 0, 100);
    }
}

SaveJpeg造成"阴影"具有透明度的工件

WritableBitmap的文档。Pixels声明"Silverlight WriteableBitmap使用的格式是ARGB32(预乘RGB)"。也许live tiles需要一个非预相乘的像素格式。

我在Silverlight中找不到任何API来改变格式,但我认为本文中的方法可能是您需要的:

http://nokola.com/blog/post/2010/01/27/The-Most-Important-Silverlight-WriteableBitmap-Gotcha-Does-It-LoseChange-Colors.aspx

编辑:

从我的测试来看,问题似乎是JPEG压缩工件,因为SaveJpeg保存JPEG格式的文件,即使你用。png扩展名命名它们。

我下面的示例代码有一个注释掉的调用makenonpremultiply (bitmap.Pixels),它显示了如果你使用一些库将其保存为具有透明度和期望非预相乘格式的文件格式,你将如何调用过滤器将像素格式修改为非预相乘。

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Shell;
namespace LiveTilePlayground
{
    public partial class LiveTileGenerator
    {
        /// <summary>
        /// Renders a FrameworkElement (control) to a bitmap
        /// the size of a live tile or a custom sized square.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="size">
        /// The size of the bitmap (in each dimension).
        /// </param>
        /// <returns></returns>
        public static WriteableBitmap RenderBitmap(
            FrameworkElement element,
            double size = 173.0)
        {
            element.Measure(new Size(size, size));
            element.Arrange(new Rect(0, 0, size, size));
            return new WriteableBitmap(element, null);
        }
        /// <summary>
        /// Updates the primary tile with specific title and background image.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="backgroundImage">The background image.</param>
        public static void UpdatePrimaryTile(string title, Uri backgroundImage)
        {
            ShellTile primaryTile = ShellTile.ActiveTiles.First();
            StandardTileData newTileData = new StandardTileData
            { Title = title, BackgroundImage = backgroundImage };
            primaryTile.Update(newTileData);
        }
        /// <summary>
        /// Saves the tile bitmap with a given file name and returns the URI.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public static Uri SaveTileBitmap(
            WriteableBitmap bitmap, string fileName)
        {
            //MakeNonPremultiplied(bitmap.Pixels);
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists(@"Shared'ShellContent"))
                {
                    store.CreateDirectory(@"Shared'ShellContent");
                }
                using (
                    var stream = store.OpenFile(
                        @"Shared'ShellContent'" + fileName,
                        FileMode.OpenOrCreate))
                {
                    bitmap.SaveJpeg(stream, 173, 173, 0, 100);
                }
            }
            return new Uri(
                "isostore:/Shared/ShellContent/" + fileName, UriKind.Absolute);
        }
        /// <summary>
        /// Transforms bitmap pixels to a non-alpha premultiplied format.
        /// </summary>
        /// <param name="bitmapPixels">The bitmap pixels.</param>
        public static void MakeNonPremultiplied(int[] bitmapPixels)
        {
            int count = bitmapPixels.Length;
            // Iterate through all pixels and
            // make each semi-transparent pixel non-premultiplied
            for (int i = 0; i < count; i++)
            {
                uint pixel = unchecked((uint)bitmapPixels[i]);
                // Decompose ARGB structure from the uint into separate channels
                // Shift by 3 bytes to get Alpha
                double a = pixel >> 24;
                // If alpha is 255 (solid color) or 0 (completely transparent) -
                // skip this pixel.
                if ((a == 255) || (a == 0))
                {
                    continue;
                }
                // Shift 2 bytes and filter out the Alpha byte to get Red
                double r = (pixel >> 16) & 255;
                // Shift 1 bytes and filter out Alpha and Red bytes to get Green
                double g = (pixel >> 8) & 255;
                // Filter out Alpha, Red and Green bytes to get Blue
                double b = (pixel) & 255;
                // Divide by normalized Alpha to get non-premultiplied values
                double factor = 256 / a;
                uint newR = (uint)Math.Round(r * factor);
                uint newG = (uint)Math.Round(g * factor);
                uint newB = (uint)Math.Round(b * factor);
                // Compose back to ARGB uint
                bitmapPixels[i] =
                    unchecked((int)(
                        (pixel & 0xFF000000) |
                        (newR << 16) |
                        (newG << 8) |
                        newB));
            }
        }
    }
}

我有完全相同的问题,但对我来说简单的答案是检查所使用的透明PNG。它被设置为使用16位颜色深度。将png更改为8位颜色深度为我解决了这个问题。