i need to set the transparency of a bitmap using a trackbar. how can i do this? if i change the value of the trackbar, the image's transparency will change. codes will be greatly appreciated. thanks!
kazekagerandy 0 Light Poster
Unhnd_Exception
Read the directions in the MSDN library for items your not familiar with.
Version 1 draws an image at a new opacity without affecting the original.
Version 2 changes the opacity of the original image.
Version 1
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Form1
Private RedBitmap As Bitmap
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
RedBitmap = New Bitmap(50, 50)
Dim RedGraphics As Graphics = Graphics.FromImage(RedBitmap)
RedGraphics.Clear(Color.Red)
RedGraphics.Dispose()
TrackBar1.Minimum = 1
TrackBar1.Maximum = 100
TrackBar1.Value = 1
End Sub
Private Sub TrackBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
PictureBox1.Refresh()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim Opacity As Single = CSng(1 - TrackBar1.Value / TrackBar1.Maximum)
Dim ColorVector 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, Opacity, 0}, _
New Single() {0, 0, 0, 0, 1}}
Dim ColorMatrix As New ColorMatrix(ColorVector)
Dim ImageAttributes As New ImageAttributes
ImageAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
e.Graphics.DrawImage(RedBitmap, New Rectangle(Point.Empty, RedBitmap.Size), 0, 0, RedBitmap.Width, RedBitmap.Height, GraphicsUnit.Pixel, ImageAttributes)
ImageAttributes.Dispose()
End Sub
End Class
Version 2
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form2
Private RedBitmap As Bitmap
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
RedBitmap = New Bitmap(50, 50)
Opacity = 1
Dim RedGraphics As Graphics = Graphics.FromImage(RedBitmap)
RedGraphics.Clear(Color.Red)
RedGraphics.Dispose()
PictureBox1.Image = RedBitmap
TrackBar1.Minimum = 1
TrackBar1.Maximum = 100
TrackBar1.Value = 1
End Sub
Private Sub TrackBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
SetTransparency(CType(PictureBox1.Image, Bitmap), CByte((1 - TrackBar1.Value / TrackBar1.Maximum) * Byte.MaxValue))
PictureBox1.Refresh()
End Sub
Public Sub SetTransparency(ByRef bitmap As Bitmap, ByRef alpha As Byte)
If bitmap Is Nothing OrElse bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then Exit Sub
If alpha < 0 Then alpha = 0
If alpha > Byte.MaxValue Then alpha = Byte.MaxValue
Dim BitmapRect As New Rectangle(0, 0, bitmap.Width, bitmap.Height)
Dim BitampData As BitmapData = bitmap.LockBits(BitmapRect, ImageLockMode.ReadWrite, bitmap.PixelFormat)
Dim BitmapPtr As IntPtr = BitampData.Scan0
Dim Bytes(BitampData.Stride * bitmap.Height - 1) As Byte
Marshal.Copy(BitmapPtr, Bytes, 0, Bytes.Length)
'Set every fourth alpha byte to the alpha passed in.
For i = 3 To UBound(Bytes) Step 4
Bytes(i) = alpha
Next
Marshal.Copy(Bytes, 0, BitmapPtr, Bytes.Length)
bitmap.UnlockBits(BitampData)
BitampData = Nothing
Bytes = Nothing
BitmapPtr = IntPtr.Zero
End Sub
End Class
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.