Mixed Blending in GDI+ using VB.Net

In this article I will explain about Mixed Blending in GDI+.
  • 3221

Mixed blending is a combination of both alpha blending and color blending. It is useful when you need to draw transparent and blended graphics shapes, for example, drawing a transparent image with transparent shapes using a blended linear gradient brush.

Listing 9.34 shows how to mix these two types of blending. Using the InterpolationColors property, we create a LinearGradientBrush object and set its Colors and Positions properties to specify the blending colors and positions. After that we create a Bitmap object and apply a color matrix using SetColorMatrix. Then we draw a rectangle and an ellipse and we call DrawImage.

LISTING 9.34: Mixed blending example

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Dim g As Graphics = Me.CreateGraphics()

        ' Create a LinearGradientBrush object
        Dim brBrush As New LinearGradientBrush(New Point(0, 0), New Point(50, 20), Color.Blue, Color.Red)
        Dim rect As New Rectangle(20, 20, 200, 100)

        ' Create color and points arrays
        Dim clrArray As Color() = {Color.Red, Color.Blue, Color.Green, Color.Pink, Color.Yellow, Color.DarkTurquoise}

        Dim posArray As Single() = {0.0F, 0.2F, 0.4F, 0.6F, 0.8F, 1.0F}

        ' Create a ColorBlend object and set its Colors and Positions properties
        Dim colorBlend As New ColorBlend()
        colorBlend.Colors = clrArray

        ' Set InterpolationColors = colorBlend;
        ' Create a Bitmap object from a file
        Dim bitmap As New Bitmap("MyPhoto.jpg")

        ' Create a points array
        Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0.5F, 0}, New Single() {0, 0, 0, 0, 1}}

        ' Create a ColorMtrix object using pts array
        Dim clrMatrix As New ColorMatrix(ptsArray)

        ' Create an ImageAttributes object
        Dim imgAttributes As New ImageAttributes()

        ' Set color matrix of ImageAttributes
        imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)

        ' Fill rectangle
        g.FillRectangle(brBrush, rect)
        rect.Y += 120

        ' Fill ellipse
        g.FillEllipse(brBrush, rect)

        ' Draw image using ImageAttributes
        g.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, _
         GraphicsUnit.Pixel, imgAttributes)

        ' Dispose of objects
    End Sub
End Class


FIGURE 9.46: A mixed blending example

Figure 9.46 shows the output from Listing 9.34. The rectangle and ellipse are blended (multicolor) and translucent (alpha-blended).


Hope the article would have helped you in understanding Mixed Blending in GDI+. Read other articles on GDI+ on the website.


More Articles

© 2019 DotNetHeaven. All rights reserved.