Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB classic graphics 2

Ken01

Programmer
Joined
Aug 8, 2014
Messages
71
Location
GB
What is the purpose of masks in VB6 graphics and how easy is it to create them ?
 
Were VB6 to have masks, then the purpose would be the same as in any graphics environment - a technique to conceal or alter parts of an image for aesthetic purposes, to remove unwanted sections, or to reshape it. It includes two types: bitmasking, which hides pixels based on their color values, and alpha masking, which conceals pixels based on their opacity.

VBs various built-in graphics primitives do not include any sort of masking. The image control, however, does; it supports bitmasking, and allows you to define which colour in an image will be transparent. But that's about all the control you have.

This isn't to say you can't do more complex bitmasking or alpha masking in VB - but to do so you have to work with GDI or GDI+ APIs (GDI doesn't support alpha masking at all). Some of the GDI+ API is encapsulated in the Windows Image Acquisition library, but to use the rest you need a tool library (which I believe I have previously pointed you to) as VB6 cannot work with GDI+ directly
 
Thank-you once again.

Masking along with other features such as Blitting (Bitblt) seem to have disappeared from VB.Net.
 
Bitblt is a Win32 GDI API call, and thus is perfectly usable in VB.NET (although using syst4em.drawing's methods for rendering bitmaps may often prove more useful, albeit slower)

And bitmasking is also pretty trivial

Alpha masking is slightly harder, but only slightly.

This isn't a vb.net forum, and I'm probably teaching you how to suck eggs, but here's some example code (requires a form with a button):

Rich (BB code):
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim graphicsObject As Graphics
        graphicsObject = Me.CreateGraphics()

        Dim myBitmap = New Bitmap("D:\Downloads\demo.png") ' my source is a simple green box drawn on a red background

        ' Draw myBitmap to the screen.
        graphicsObject.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height)

        ' Make red transparent
        Dim BackColor As Color
        BackColor = Color.FromArgb(255, 0, 0)
        myBitmap.MakeTransparent(BackColor) ' and you can add additional transparent colours if you want.
        ' Draw the bitmasked bitmap to the form.
        graphicsObject.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height)

        ' Simple Alpha masking
        Dim rc As New Rectangle(0, myBitmap.Height, myBitmap.Width, myBitmap.Height)
        Dim cm As New ColorMatrix()
        Dim ia As New ImageAttributes()

        myBitmap = New Bitmap("D:\Downloads\demo.png") ' just original bitmap again since MakeTransparent modifies the in memory bitmap
        cm.Matrix33 = 0.5 ' set our alpha to 50%
        ia.SetColorMatrix(cm)
        graphicsObject.DrawImage(myBitmap, rc, 0, 0, myBitmap.Width, myBitmap.Height, GraphicsUnit.Pixel, ia) ' use slightly different DrawImage in order to use alpha
    End Sub
End Class
 

Part and Inventory Search

Sponsor

Back
Top