6

黄色の「後ろ」にあるものが透けて見えるようにしたい.

編集1:しかし、「白」で描いている場合は、マーカーの色が純粋な黄色を保持するようにします。

EDIT 2: @Kevin の回答はおそらく正しいので、コードを作成していませんが、正しいとマークしました。私のコードでは、Color.FromArgb を使用して、@Guffa の回答に落ち着いています。

編集 3: まともなパフォーマンスで動作するコードを投稿しました。はい、青を減算するのが基本的な考え方ですが、高レベルの API では実行できず、SetPixel は遅すぎます。パフォーマンスの良いソリューションでは、Bitmap.LockBits、UnlockBits を使用します。

4

5 に答える 5

9

蛍光ペンは顔料なので、基本的に減法です。白を黄色に変えたいのですが、黒を黄色に変えたいわけではありません。.NET はわかりませんが、必要なのはデフォルト以外の「ブレンド モード」、具体的には減算です。具体的には、ブレンドモードを減色に設定し、色を純粋な(減色したい色で、黄色を残します) に設定します。減算する青がないため、黒はそのまま残り、白は黄色になります。

残念ながら、最新の描画インターフェイスの多くは、アルファ以外のブレンド モードを除外しており、これはその 1 つと思われます。ビットマップにアクセスできる場合は、自分で実装できます — 各ピクセル値を取得し、青のコンポーネントをゼロに設定します。または、ハイライトしたい領域が複雑な場合は、画像のコピーを作成し、コピーのハイライトされた領域に黒を描画してから、元の赤と緑のチャンネルとコピーの青のチャンネルを組み合わせて最終的なものにします結果画像。

于 2010-02-13T14:21:48.243 に答える
3

たとえば、アルファ不透明度が 50% の半透明の色を使用します。

Color.FromArgb(128, Color.Yellow)

アルファ値が低いほど、背景が透けて見えます。

于 2010-02-13T14:02:47.327 に答える
2

必要なコードは次のとおりです。

    protected override void OnPaint( PaintEventArgs e )
    {
        using ( var bmp = new Bitmap( 100, 100 ) )
        using ( var g = Graphics.FromImage( bmp ) )
        using ( var ia = new ImageAttributes() )
        {
            // 1. create a sample bitmap
            g.Clear( Color.White );
            var p = Point.Empty;
            foreach ( var color in new Color[] { Color.Black, Color.Gray, Color.LightBlue, Color.Green, Color.Red, Color.Magenta } )
                using ( var brush = new SolidBrush( color ) )
                {
                    g.DrawString( "Some sample text", SystemFonts.DefaultFont, brush, p );
                    p.Offset( 0, 16 );
                }
            // 2. transfer the bitmap on screen
            e.Graphics.DrawImage( bmp, Point.Empty );
            // 3. transfer a part of the bitmap on screen again, this time removing all blue
            ia.SetColorMatrix( new ColorMatrix( new float[][] {
                        new float[] {1, 0, 0, 0, 0},
                        new float[] {0, 1, 0, 0, 0},
                        new float[] {0, 0, 0, 0, 0},
                        new float[] {0, 0, 0, 1, 0},
                        new float[] {0, 0, 0, 0, 1}} ) );
            e.Graphics.DrawImage(
                bmp,
                new Rectangle( 30, 0, 40, 100 ),
                30, 0, 40, 100,
                GraphicsUnit.Pixel,
                ia );
        }
    }
于 2010-02-14T18:10:08.020 に答える
1

このコードは機能します。黄色が必要なすべての RGB の青色コンポーネントを一掃します。最初は Bitmap.GetPixel/SetPixel を使ってみましたが、痛々しいほど遅かったです。Lock/Unlock を使用して raw ビットを取得することは、十分に高速に実行されました。

                using (Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height))
                {
                    using (Graphics tempG = Graphics.FromImage(tempBitmap))
                    {

                        tempG.DrawLines(penYellowHighlighter, stroke.points.ToArray());

                        // get the raw bits of the source and target and remove the blue from every
                        // bit of the target where there is a yellow bit of the source
                        Rectangle rect = new Rectangle(0, 0, bitmapWithStrokes.Width, bitmapWithStrokes.Height);

                        // lock
                        System.Drawing.Imaging.BitmapData sourceData =
                            tempBitmap.LockBits(
                                rect,
                                System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                tempBitmap.PixelFormat);

                        System.Drawing.Imaging.BitmapData targetData =
                            bitmapWithStrokes.LockBits(
                                rect,
                                System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                bitmapWithStrokes.PixelFormat);

                        // Get the address of the first line.
                        IntPtr sourcePtr = sourceData.Scan0;
                        IntPtr targetPtr = targetData.Scan0;

                        // Declare an array to hold the bytes of the bitmap.
                        int numberOfBytes = Math.Abs(sourceData.Stride) * tempBitmap.Height;

                        byte[] sourceRgbValues = new byte[numberOfBytes];
                        byte[] targetRgbValues = new byte[numberOfBytes];

                        // Copy the RGB values into the array.
                        System.Runtime.InteropServices.Marshal.Copy(sourcePtr, sourceRgbValues, 0, numberOfBytes);
                        System.Runtime.InteropServices.Marshal.Copy(targetPtr, targetRgbValues, 0, numberOfBytes);

                        for (int p = 0; p < numberOfBytes; p += 4)
                        {
                            // if the source's red is yellows's red
                            if (sourceRgbValues[p + 2] == yellowsRedComponent)
                            {
                                // wipe out the target's blue
                                targetRgbValues[p] = 0;
                            }
                        }

                        // Copy the RGB values back to the bitmap
                        System.Runtime.InteropServices.Marshal.Copy(targetRgbValues, 0, targetPtr, numberOfBytes);

                        // Unlock the bits.
                        tempBitmap.UnlockBits(sourceData);
                        bitmapWithStrokes.UnlockBits(targetData);
于 2010-12-19T06:32:14.200 に答える
0

SolidBrushアルファが 255 未満の値で初期化されたを使用してみてくださいColor。これにより、使用する色の半透明のブラシが作成されます。

于 2010-02-13T14:03:07.307 に答える