Nasıl yapılır: Alfa Karışım Kullanmayı Denetlemek için Birleştirme Modunu Kullanma

Aşağıdaki özelliklere sahip bir ekran dışı bit eşlem oluşturmak istediğiniz zamanlar olabilir:

  • Renklerin alfa değerleri 255'in altındadır.

  • Bit eşlem oluşturulurken renkler alfa karıştırmaz.

  • Bit eşlemi görüntüledikten sonra bit eşlem içinde renkler, görüntü cihazında arka plan renkleri ile alfa karıştırıldı.

Böyle bir bit eşlem oluşturmak için boş bir nesne oluşturun Bitmap ve ardından bu bit Graphics eşlemi temel alan bir nesne oluşturun. Nesnesinin oluşturma modunu Graphics olarak CompositingMode.SourceCopy ayarlayın.

Örnek

Aşağıdaki örnek, bir Graphics nesneyi temel alan bir nesnesi Bitmap oluşturur. Kod, bit eşlem üzerinde boya yapmak için iki yarı saydam fırça Graphics (alfa = 160) ile birlikte nesnesini kullanır. Kod, yarıtransparent fırçaları kullanarak kırmızı bir üç nokta ve yeşil bir üç nokta doldurur. Yeşil üç nokta kırmızı üç noktayla çakışır, ancak nesnenin birleştirme modu olarak ayarlanmış olduğundan yeşil kırmızıyla GraphicsSourceCopy karıştırlanmaz.

Kod, ekranda bit eşlemi iki kez çizer: bir kez beyaz arka planda ve bir kez de çok renkli arka planda. Bit eşlem içinde iki üç noktanın parçası olan piksellerin alfa bileşeni 160'tır, bu nedenle üç nokta ekranda arka plan renkleri ile karıştırıldı.

Aşağıdaki çizimde kod örneğinin çıkışı gösterilmiştir. Üç noktanın arka planla karıştırıldıklarını ancak bunların birbirine karıştırıldıklarını unutmayın.

Diagram showing ellipses blended with the background, not each other.

Kod örneği şu deyimi içerir:

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

Üç noktanın birbirinin yanı sıra arka plan ile karıştırnarak bu deyimi aşağıdaki şekilde değiştirebilirsiniz:

bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
bitmapGraphics.CompositingMode = CompositingMode.SourceOver

Aşağıdaki çizimde düzeltilen kodun çıkışı gösterilmiştir.

Diagram that shows ellipses blended together and with background.

// Create a blank bitmap.
Bitmap myBitmap = new Bitmap(180, 100);

// Create a Graphics object that we can use to draw on the bitmap.
Graphics bitmapGraphics = Graphics.FromImage(myBitmap);

// Create a red brush and a green brush, each with an alpha value of 160.
SolidBrush redBrush = new SolidBrush(Color.FromArgb(160, 255, 0, 0));
SolidBrush greenBrush = new SolidBrush(Color.FromArgb(160, 0, 255, 0));

// Set the compositing mode so that when we draw overlapping ellipses,
// the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

// Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70);

// Fill a second ellipse using a green brush that has an alpha value of 160.
// The green ellipse overlaps the red ellipse, but the green is not
// blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70);

// Set the compositing quality of the form's Graphics object.
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;

// Draw a multicolored background.
SolidBrush colorBrush = new SolidBrush(Color.Aqua);
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100);
colorBrush.Color = Color.Yellow;
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100);
colorBrush.Color = Color.Fuchsia;
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100);

// Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0);

// Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0);
' Create a blank bitmap.
Dim myBitmap As New Bitmap(180, 100)

' Create a Graphics object that we can use to draw on the bitmap.
Dim bitmapGraphics As Graphics = Graphics.FromImage(myBitmap)

' Create a red brush and a green brush, each with an alpha value of 160.
Dim redBrush As New SolidBrush(Color.FromArgb(160, 255, 0, 0))
Dim greenBrush As New SolidBrush(Color.FromArgb(160, 0, 255, 0))

' Set the compositing mode so that when we draw overlapping ellipses,
' the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

' Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70)

' Fill a second ellipse using a green brush that has an alpha value of 
' 160. The green ellipse overlaps the red ellipse, but the green is not 
' blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70)

'Set the compositing quality of the form's Graphics object. 
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected

' Draw a multicolored background.
Dim colorBrush As New SolidBrush(Color.Aqua)
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100)
colorBrush.Color = Color.Yellow
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100)
colorBrush.Color = Color.Fuchsia
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100)

'Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0)

' Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0)

Kod Derleniyor

Yukarıdaki örnek, Windows Forms ile birlikte kullanım için tasarlanmıştır ve parametresi PaintEventArgse olan 'i PaintEventHandler gerektirir.

Ayrıca bkz.