ImageAttributes::SetColorKey 方法 (gdiplusimageattributes.h)

ImageAttributes::SetColorKey 方法會為指定的類別設定色彩索引鍵 (透明度範圍) 。

語法

Status SetColorKey(
  [in, ref]      const Color &   colorLow,
  [in, ref]      const Color &   colorHigh,
  [in, optional] ColorAdjustType type
);

參數

[in, ref] colorLow

類型: const Color

指定低色彩索引鍵值的 Color 物件參考。

[in, ref] colorHigh

類型: const Color

指定高色彩索引鍵值的 Color 物件參考。

[in, optional] type

類型: ColorAdjustType

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

傳回值

類型: 狀態

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

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

備註

這個方法會設定高低色彩索引鍵值,讓色彩範圍變成透明。 在高低色索引鍵的對應元件之間,具有其三個元件 (紅色、綠色、藍色) 的任何色彩都會變成透明。

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

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

一旦您指定特定類別的色彩或灰階調整設定,預設調整設定就不再套用至該類別。 例如,假設您指定預設類別的調整設定集合。 如果您藉由將 ColorAdjustTypePen 傳遞給 ImageAttributes::SetColorKey 方法來設定畫筆類別的色彩索引鍵,則不會將任何預設調整設定套用至手寫筆。

範例

下列範例會從 .bmp 檔案建立 Image 物件。 程序代碼也會建立 ImageAttributes 物件。 呼叫 ImageAttributes::SetColorKey 會 設定 ImageAttributes 物件的點陣圖色彩索引鍵,讓符合下列三個條件的任何色彩變成透明:

  • 紅色元件位於 100 到 250 的範圍內。
  • 綠色元件位於95到245的範圍內。
  • 藍色元件位於範圍 30 到 60。

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

   // Create an Image object based on a BMP file.
   // The image has three horizontal stripes.
   // The color of the top stripe has RGB components (90, 90, 20).
   // The color of the middle stripe has RGB components (150, 150, 150).
   // The color of the bottom stripe has RGB components (130, 130, 40).
   Image image(L"ColorKeyTest.bmp");

   // Create an ImageAttributes object, and set its color key.
   ImageAttributes imAtt;
   imAtt.SetColorKey(
      Color(100, 95, 30),
      Color(250, 245, 60),
      ColorAdjustTypeBitmap);

   // Draw the image. Apply the color key.
   // The bottom stripe of the image will be transparent because
   // 100 <= 130 <= 250 and
   // 95  <= 130 <= 245 and
   // 30  <= 40  <= 60.
   graphics.DrawImage(
      &image, 
      Rect(20, 20, image.GetWidth(), image.GetHeight()),  // dest rect
      0, 0, image.GetWidth(), image.GetHeight(),          // source rect
      UnitPixel,
      &imAtt);
}
				

規格需求

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

另請參閱

點陣圖

色彩

ColorAdjustType

映像

ImageAttributes

ImageAttributes::ClearColorKey

ImageAttributes::ClearThreshold

ImageAttributes::SetThreshold

元檔

重新著色