2017-08-29 76 views
0

我想從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): enter image description here

如何從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 
+2

嘗試將圖像轉換到linearRGB顏色空間,將所述彩色矩陣濾波器和然後將結果轉換回sRGB色彩空間。 –

回答

相關問題