考虑引入lcmsnet库,为CMYK色彩空间图像提供ICC校正

作者: Charltsing创建于 2026年1月31日更新于 2026年6月30日

Profile profile = null; if (info.ICCProfile != null) { try { profile = Profile.Open(info.ICCProfile); // read the ICC in the PDF } catch { } } if (profile == null) { string iccfilename = "xxxx.USWebCoatedSWOP.icc"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream(iccfilename); byte[] defaultICC = stream.ToByteArray();

profile = Profile.Open(defaultICC);

} using var rgbprofile = Profile.Create_sRGB(); using (profile) { // create a transform from cmyk to bgra // rendering intent: INTENT_PERCEPTUAL keep visual consistency (default), suitable for photo-like images // flags: none using var transform = Transform.Create(profile, Cms.TYPE_CMYK_8, rgbprofile, Cms.TYPE_BGRA_8, Intent.Perceptual, CmsFlags.None);

byte[] rgbBytes = new byte[cmykBytes.Length];
transform.DoTransform(cmykBytes, rgbBytes, info.Width * info.Height);

// write to Bitmap
bmp = new Bitmap(info.Width, info.Height, PixelFormat.Format32bppArgb);
var rect = new Rectangle(0, 0, info.Width, info.Height);
var bitmapData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);

int srcStride = info.Width * 4;          
int dstStride = Math.Abs(bitmapData.Stride); 

Parallel.For(0, info.Height, y =>
{
    IntPtr dst = bitmapData.Scan0 + y * dstStride;
    int srcOffset = y * srcStride;
    Marshal.Copy(rgbBytes, srcOffset, dst, srcStride);
});

bmp.UnlockBits(bitmapData);

}

内容来源: wmjordan/PDFPatcher