C# SkiaSharep使用ICC配置文件保存图像

C# SkiaSharep使用ICC配置文件保存图像,c#,.net-core,skiasharp,C#,.net Core,Skiasharp,在SkiaSharp中,当您从现有图像创建新图像时(例如,调整大小时),如何使用原始图像的ICC配置文件保存新图像?因此答案是:如果在源图像和目标图像之间设置并维护了颜色空间,Skia将自动应用ICC配置文件 必须在源对象和目标对象(SKBitmap、SKImage、SKSurface等)上设置颜色空间。这样Skia就可以知道如何在源对象和目标对象之间转换颜色。如果ColorSpace未在其中任何一个上设置,或者如果ColorSpace在创建新对象的过程中丢失(这在创建新对象时很容易发生),Sk

在SkiaSharp中,当您从现有图像创建新图像时(例如,调整大小时),如何使用原始图像的ICC配置文件保存新图像?

因此答案是:如果在源图像和目标图像之间设置并维护了
颜色空间,Skia将自动应用ICC配置文件

必须在源对象和目标对象(SKBitmap、SKImage、SKSurface等)上设置
颜色空间。这样Skia就可以知道如何在源对象和目标对象之间转换颜色。如果
ColorSpace
未在其中任何一个上设置,或者如果
ColorSpace
在创建新对象的过程中丢失(这在创建新对象时很容易发生),Skia将使用默认设置,这可能会扭曲颜色转换

维护颜色空间的正确方法示例: 另一个维护颜色空间的示例: 关于Skia颜色校正的其他非常有用的信息:

using (SKData origData = SKData.Create(imgStream)) // convert the stream into SKData
using (SKImage srcImg = SKImage.FromEncodedData(origData))
    // srcImg now contains the original ColorSpace (e.g. CMYK)
{
    SKImageInfo info = new SKImageInfo(resizeWidth, resizeHeight,
        SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
    // this is the important part. set the destination ColorSpace as
    // `SKColorSpace.CreateSrgb()`. Skia will then be able to automatically convert
    // the original CMYK colorspace, to this new sRGB colorspace.

    using (SKImage newImg = SKImage.Create(info)) // new image. ColorSpace set via `info`
    {
        srcImg.ScalePixels(newImg.PeekPixels(), SKFilterQuality.None);
        // now when doing this resize, Skia knows the original ColorSpace, and the
        // destination ColorSpace, and converts the colors from CMYK to sRGB.
    }
}
using (SKCodec codec = SKCodec.Create(imgStream)) // create a codec with the imgStream
{
    SKImageInfo info = new SKImageInfo(codec.Info.Width, codec.Info.Height,
        SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
    // set the destination ColorSpace via SKColorSpace.CreateSrgb()

    SKBitmap srcImg = SKBitmap.Decode(codec, info);
    // Skia creates a new bitmap, converting the codec ColorSpace (e.g. CMYK) to the
    // destination ColorSpace (sRGB)
}