如何获得SVG feColorMatrix在C#中的效果#
问题描述:
我想从C#获得相同的输出,因为我从SVG feColorMatrix效果中获得。如何获得SVG feColorMatrix在C#中的效果#
使用原来的颜色为红色(#FF0000)和下面的矩阵(矩阵的行/列转换为SVG):
0.2 0.7 0.7 0 0
0.0 0.0 0.0 0 0
0.0 0.0 0.0 0 0
0.0 0.0 0.0 1 0
0.0 0.0 0.0 0 1
这是迄今为止我已经得到的结果(左边是原始的,中间是来自C#,右边是来自feColorMatrix):
如何从C#获得与SVG feColorMatrix效果相同的输出?
我在C#代码:
// Create source bitmap
var sourceBitmap = new Bitmap(100, 100);
using (var sourceGraphics = Graphics.FromImage(sourceBitmap))
{
sourceGraphics.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0, sourceBitmap.Width, sourceBitmap.Height);
}
sourceBitmap.Save(@"C:\Temp\sourceBitmap.png", ImageFormat.Png);
// Create color matrix
float[][] colorMatrixElements =
{
new[] { 0.2f, 0.7f, 0.7f, 0, 0 },
new[] { 0.0f, 0.0f, 0.0f, 0, 0 },
new[] { 0.0f, 0.0f, 0.0f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
};
var colorMatrix = new ColorMatrix(colorMatrixElements);
// Create target bitmap
var targetBitmap = new Bitmap(sourceBitmap);
using (var targetGraphics = Graphics.FromImage(targetBitmap))
{
// Draw on target bitmap using color matrix
var imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix);
var rect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
targetGraphics.DrawImage(sourceBitmap, rect, 0, 0, sourceBitmap.Width, sourceBitmap.Height, GraphicsUnit.Pixel, imageAttributes);
}
targetBitmap.Save(@"C:\Temp\targetBitmap.png", ImageFormat.Png);
我使用的SVG(和结果图像)的代码:
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="300px"
height="100px"
viewBox="0 0 300 100" >
<defs>
<filter id="colormatrixfilter" filterUnits="objectBoundingBox" x="0%" y="0%" width="100%" height="100%">
<feColorMatrix id="colormatrix"
type="matrix"
in="SourceGraphic"
values="0.20 0.00 0.00 0 0
0.70 0.00 0.00 0 0
0.70 0.00 0.00 0 0
0.00 0.00 0.00 1 0" />
</filter>
</defs>
<image x="0" y="0" width="100" height="100" xlink:href="sourceBitmap.png" />
<image x="100" y="0" width="100" height="100" xlink:href="targetBitmap.png" />
<image x="200" y="0" width="100" height="100" xlink:href="sourceBitmap.png" filter="url(#colormatrixfilter)" />
</svg>
RGB码(使用结果颜色选择器):
Original: r=255 g=0 b=0
C#: r=51 g=178 b=178
feColorMatrix: r=124 g=218 b=218
答
感谢Robert的评论,我明白了。解决的办法是添加下列行到ImageAttributes类:
imageAttributes.SetGamma(0.45f);
伽玛值从采取:
尝试将图像转换到linearRGB颜色空间,将所述彩色矩阵滤波器和然后将结果转换回sRGB色彩空间。 –