1

Windows 7 (および新しい画像コーデック: WIC) の前に、次の (非常に高速ですが汚い) メソッドを使用して、透明色として白の GIF エンコードされた画像を作成しました。

MemoryStream target = new memoryStream(4096);
image.Save(target, imageFormat.Gif);
byte[] data = target.ToArray();

// Set transparency
// Check Graphic Control Extension signature (0x21 0xF9)
if (data[0x30D] == 0x21 && data[0x30E] == 0xF9)
   data[0x313] = 0xFF; // Set palette index 255 (=white) as transparent

.NET は、インデックス 255 が白色である標準パレットを使用して Gif をエンコードしていたため、この方法が機能しました。

ただし、Windows 7 では、この方法は機能しなくなりました。標準パレットが変更され、インデックス 251 が白になったようです。しかし、よくわかりません。おそらく、新しい Gif エンコーダーは、使用されている色に基づいてパレットを動的に生成していますか?

私の質問: Windows 7 の新しい Gif エンコーダーについて洞察を持っている人はいますか?また、白を透明にするための適切で迅速な方法は何ですか?

4

2 に答える 2

3

gifでエンコードされた画像の透明色として白を設定するためのより良い方法を見つけました。GDI +とWIC(Windows 7)の両方のエンコーダーでエンコードされたGifで機能するようです。次のコードは、Gifのグローバルイメージテーブルで白のインデックスを検索し、このインデックスを使用して、GraphicControlExtensionブロックに透明色を設定します。

 byte[] data;

// Save image to byte array
using (MemoryStream target = new MemoryStream(4096))
{
    image.Save(target, imageFormat.Gif);
    data = target.ToArray();
}

// Find the index of the color white in the Global Color Table and set this index as the transparent color
byte packedFields = data[0x0A]; // <packed fields> of the logical screen descriptor
if ((packedFields & 80) != 0 && (packedFields & 0x07) == 0x07) // Global color table is present and has 3 bytes per color
{
    int whiteIndex = -1;
    // Start at last entry of Global Color Table (bigger chance to find white?)
    for (int index = 0x0D + (3 * 255); index > 0x0D; index -= 3)
    {
        if (data[index] == 0xFF && data[index + 1] == 0xFF && data[index + 2] == 0xFF)
        {
            whiteIndex = (int) ((index - 0xD) / 3);
            break;
        }
    }

    if (whiteIndex != -1)
    {
        // Set transparency
        // Check Graphic Control Extension signature (0x21 0xF9)
        if (data[0x30D] == 0x21 && data[0x30E] == 0xF9)
            data[0x313] = (byte)whiteIndex;
    }
}

// Now the byte array contains a Gif image with white as the transparent color
于 2009-11-25T14:12:32.173 に答える
0

これは Windows 7 の問題であり、コードの他の部分の問題ではありませんか?

GIF 仕様では、透過性のために任意のインデックスを使用できることが示唆されています。画像をチェックして、透過性を有効にする適切なビットがオンに設定されていることを確認することをお勧めします。そうでない場合、選択したパレット インデックスは無視されます。

于 2009-11-25T11:52:27.150 に答える