ImageAttributes::ClearColorKey 方法 (gdiplusimageattributes.h)

ImageAttributes::ClearColorKey方法會清除指定類別的色彩索引鍵 (透明度範圍) 。

語法

Status ClearColorKey(
  [in, optional] ColorAdjustType type
);

參數

[in, optional] type

類型: ColorAdjustType

ColorAdjustType列舉的元素,指定清除色彩索引鍵的類別。 預設值為 ColorAdjustTypeDefault

傳回值

類型: 狀態

如果方法成功,它會傳回 Ok,這是 Status 列舉的元素。

如果方法失敗,它會傳回 Status 列舉的其他其中一個專案。

備註

ImageAttributes物件會維護五個調整類別的色彩和灰階設定:預設、點陣圖、筆刷、畫筆和文字。 例如,您可以指定預設類別的一個色彩索引鍵、點陣圖類別的不同色彩索引鍵,以及畫筆類別的色彩索引鍵。

預設色彩和灰階調整設定會套用至沒有自己調整設定的所有類別。 例如,如果您從未指定畫筆類別的任何調整設定,則預設設定會套用至畫筆類別。

一旦您指定特定類別的色彩或灰階調整設定,預設調整設定就不再套用至該類別。 例如,假設您指定預設色彩索引鍵,讓 200 到 255 透明具有紅色元件的任何色彩,並指定預設 gamma 值為 1.8。 如果您藉由呼叫 ImageAttributes::SetColorKey來設定畫筆類別的色彩索引鍵,則預設色彩索引鍵和預設 Gamma 值將不會套用至畫筆。 如果您稍後藉由呼叫 ImageAttributes::ClearColorKey來清除畫筆色彩索引鍵,則畫筆類別不會還原為預設色彩索引鍵;相反地,畫筆類別不會有色彩索引鍵。 同樣地,畫筆類別不會還原為預設 Gamma 值;相反地,畫筆類別不會有 gamma 值。

範例

下列範例會從 .emf 檔案建立 Image 物件。 程式碼也會建立 ImageAttributes 物件。 第一次呼叫 ImageAttributes::SetColorKey 會 設定 ImageAttributes 物件的預設色彩索引鍵,讓 80 到 120 的紅色元件色彩是透明的。 第二次呼叫 ImageAttributes::SetColorKey 會 設定 ImageAttributes 物件的畫筆色彩索引鍵,讓 135 到 175 之間具有紅色元件的所有色彩都是透明的。

程式碼會呼叫 DrawImage 一次,以繪製沒有色彩調整的影像。 然後程式碼會再呼叫 DrawImage 三次,每次傳遞 Image 物件的位址和 ImageAttributes 物件的位址。 第二次在設定預設色彩索引鍵的呼叫) 之後繪製影像 (,所有從 80 到 120 的紅色都是透明的。 第三次在設定畫筆色彩索引鍵的呼叫) 之後 (繪製影像時,使用畫筆繪製的所有紅色都是透明的,從 135 到 175。 此外,不是以畫筆繪製的所有 80 到 120 紅色都是透明的。 第四次在呼叫 ImageAttributes::ClearColorKey) 之後 (繪製影像時,不會使用畫筆繪製任何紅色。 此外,不是以畫筆繪製的所有 80 到 120 紅色都是透明的。


VOID Example_SetClearColorKey(HDC hdc)
{
   Graphics graphics(hdc);

   Image image(L"TestMetafile5.emf");
   ImageAttributes imAtt;

   // Draw the image (metafile) using no color adjustment.
   graphics.DrawImage(
      &image,
      Rect(0, 0, image.GetWidth(), image.GetHeight()),  // dest rect
      0, 0, image.GetWidth(), image.GetHeight(),        // source rect
      UnitPixel);

   // Set the default color key.
   imAtt.SetColorKey(
      Color(0, 80, 0, 0),
      Color(255, 120, 255, 255),
      ColorAdjustTypeDefault);

   // Draw the image (metafile) using default color adjustment.
   // Colors with red components from 80 through 120 are transparent.
   graphics.DrawImage(
      &image,
      Rect(0, 100, image.GetWidth(), image.GetHeight()),  // dest rect
      0, 0, image.GetWidth(), image.GetHeight(),          // source rect
      UnitPixel,
      &imAtt);

   // Set the pen color key.
   imAtt.SetColorKey(
      Color(0, 135, 0, 0),
      Color(255, 175, 255, 255),
      ColorAdjustTypePen);

   // Draw the image (metafile) using default and pen adjustment.
   // Colors drawn with a pen that have red components from 135 through 175
   // are transparent. Colors not drawn with a pen that have red components
   // from 80 to 120 are transparent.
   graphics.DrawImage(
      &image,
      Rect(0, 200, image.GetWidth(), image.GetHeight()),  // dest rect
      0, 0, image.GetWidth(), image.GetHeight(),          // source rect
      UnitPixel,
      &imAtt);

   // Clear the pen color key.
   imAtt.ClearColorKey(ColorAdjustTypePen);

   // Draw the image (metafile) using only default color adjustment.
   // No colors drawn with a pen are transparent. Colors not drawn with 
   // a pen that have red components from 80 to 120 are transparent.
   graphics.DrawImage(
      &image,
      Rect(0, 300, image.GetWidth(), image.GetHeight()),  // dest rect
      0, 0, image.GetWidth(), image.GetHeight(),          // source rect
      UnitPixel,
      &imAtt); 
}
				

上述程式碼以及特定檔案 TestMetafile5.png 會產生下列輸出。 左側資料行中的橫條是使用畫筆繪製,而右欄中的橫條則填入筆刷。 預設色彩索引鍵會套用至以筆刷填滿的橫條。 套用至以畫筆繪製之橫條的色彩索引鍵會根據 ImageAttributes::SetColorKeyImageAttributes::ClearColorKey 呼叫而有所不同。

圖例顯示兩個數據行四列的橫條;最後兩個數據列中的橫條數目不相等

需求

   
最低支援的用戶端 Windows XP、Windows 2000 Professional [僅限桌面應用程式]
最低支援的伺服器 Windows 2000 Server [僅限桌面應用程式]
目標平台 Windows
標頭 gdiplusimageattributes.h (包含 Gdiplus.h)
程式庫 Gdiplus.lib
Dll Gdiplus.dll

另請參閱

點陣圖

Color

ColorAdjustType

映像

ImageAttributes

ImageAttributes::ClearThreshold

ImageAttributes::SetColorKey

ImageAttributes::SetThreshold

中繼檔

重新著色