Option Explicit On Option Strict Off Option Compare Text '============================================================================================================= ' ' modBitmap.vb ' ------------ ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .NET ) ' ' Created On : September 16, 2002 ' Last Update : January 16, 2003 ' ' VB Versions : VB.NET 1.1 (VS.NET 2003) ' (Reference System.Drawing.dll) ' (Reference System.Windows.Forms.dll) ' ' Requires : The .NET Framework v1.1 ' ' Description : This module is a collection of very useful functions designed for use with graphics ' manipulation. You can use it to find out information about pictures, or to ' render such pictures on to specified controls. ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Imports System.Windows.Forms Imports Microsoft.VisualBasic Module modBitmap #Region "Public Functions" '============================================================================================================= ' ' CopyPicture ' ' This function takes the picture passed in via the "PictureToCopy" parameter and makes a copy of it... ' returning it via the return of the function ' ' Parameter: Use: ' -------------------------------------------------- ' PictureToCopy Specifies the Image, Bitmap, Icon, Cursor, or Metafile to copy. ' ' Return: ' ------- ' If the function succeeds, it returns a copy of the picture specified. If an Image object is passed in, ' an Image object is passed back via the return of the funciton. If a Bitmap object is passed in, a ' Bitmap object is passed back... and so on. ' '============================================================================================================= Public Overloads Function CopyPicture(ByRef PictureToCopy As Image) As Image On Error Resume Next If PictureToCopy Is Nothing Then Exit Function CopyPicture = PictureToCopy.Clone Err.Clear() End Function ' (See the first overload for documentation) Public Overloads Function CopyPicture(ByRef PictureToCopy As Bitmap) As Bitmap On Error Resume Next If PictureToCopy Is Nothing Then Exit Function CopyPicture = New Bitmap(PictureToCopy) 'CopyPicture = PictureToCopy.Clone Err.Clear() End Function ' (See the first overload for documentation) Public Overloads Function CopyPicture(ByRef PictureToCopy As Icon) As Icon On Error Resume Next If PictureToCopy Is Nothing Then Exit Function CopyPicture = New Icon(PictureToCopy, PictureToCopy.Size) 'CopyPicture = PictureToCopy.Clone Err.Clear() End Function ' (See the first overload for documentation) Public Overloads Function CopyPicture(ByRef PictureToCopy As Cursor) As Cursor On Error Resume Next If PictureToCopy Is Nothing Then Exit Function CopyPicture = New Cursor(PictureToCopy.Handle) Err.Clear() End Function ' (See the first overload for documentation) Public Overloads Function CopyPicture(ByRef PictureToCopy As Metafile) As Metafile On Error Resume Next If PictureToCopy Is Nothing Then Exit Function CopyPicture = New Metafile(PictureToCopy.GetHenhmetafile, True) 'CopyPicture = PictureToCopy.Clone Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function CreateCursorFromBitmap(ByRef objBitmap As Bitmap) As Cursor On Error Resume Next If objBitmap Is Nothing Then Exit Function CreateCursorFromBitmap = New Cursor(objBitmap.GetHicon) Err.Clear() End Function '============================================================================================================= ' ' CreateCursorFromBitmap ' ' This function takes the Bitmap object to convert and creates a new CURSOR object from it. ' ' Parameter: Use: ' -------------------------------------------------- ' objBitmap Specifies the Bitmap object to create a cursor from. ' TransparentColor Specifies the transparent color of the cursor. ' ' Return: ' ------- ' If the function succeeds, it returns a new CURSOR from the Bitmap specified. ' '============================================================================================================= Public Overloads Function CreateCursorFromBitmap(ByRef objBitmap As Bitmap, ByVal TransparentColor As Color) As Cursor On Error Resume Next Dim objTemp As Bitmap If objBitmap Is Nothing Then Exit Function objTemp = New Bitmap(CType(objBitmap, System.Drawing.Image)) objTemp.MakeTransparent(TransparentColor) CreateCursorFromBitmap = New Cursor(objTemp.GetHicon) Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function CreateIconFromBitmap(ByRef objBitmap As Bitmap) As Icon On Error Resume Next If objBitmap Is Nothing Then Exit Function CreateIconFromBitmap = Icon.FromHandle(objBitmap.GetHicon) Err.Clear() End Function '============================================================================================================= ' ' CreateIconFromBitmap ' ' This function takes the Bitmap object to convert and creates a new ICON object from it. ' ' Parameter: Use: ' -------------------------------------------------- ' objBitmap Specifies the Bitmap object to create a icon from. ' TransparentColor Specifies the transparent color of the icon. ' ' Return: ' ------- ' If the function succeeds, it returns a new ICON from the Bitmap specified. ' '============================================================================================================= Public Overloads Function CreateIconFromBitmap(ByRef objBitmap As Bitmap, ByVal TransparentColor As Color) As Icon On Error Resume Next Dim objTemp As Bitmap If objBitmap Is Nothing Then Exit Function objTemp = New Bitmap(CType(objBitmap, System.Drawing.Image)) objTemp.MakeTransparent(TransparentColor) CreateIconFromBitmap = Icon.FromHandle(objBitmap.GetHicon) Err.Clear() End Function '============================================================================================================= ' ' CreateImageFromBitmap ' ' This function takes the Image object and converts it to a BITMAP object. ' ' Parameter: Use: ' -------------------------------------------------- ' objImage Specifies the Image object to convert ' ' Return: ' ------- ' If the function succeeds, it returns a new BITMAP from the Image specified. ' '============================================================================================================= Public Function CreateImageFromBitmap(ByRef objImage As Image) As Bitmap On Error Resume Next If objImage Is Nothing Then Exit Function CreateImageFromBitmap = New Bitmap(objImage) Err.Clear() End Function '============================================================================================================= ' ' CreateBitmapFromImage ' ' This function takes the Bitmap object and converts it to an IMAGE object. ' ' Parameter: Use: ' -------------------------------------------------- ' objBitamp Specifies the Bitmap object to convert ' ' Return: ' ------- ' If the function succeeds, it returns a new IMAGE from the Bitmap specified. ' '============================================================================================================= Public Function CreateBitmapFromImage(ByRef objBitamp As Bitmap) As Image On Error Resume Next If objBitamp Is Nothing Then Exit Function CreateBitmapFromImage.FromHbitmap(objBitamp.GetHbitmap) Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function TransparentBitmap(ByRef objImage As Object, ByVal TransparentColor_R As Integer, ByVal TransparentColor_G As Integer, ByVal TransparentColor_B As Integer) As Bitmap Return TransparentBitmap(objImage, Color.FromArgb(TransparentColor_R, TransparentColor_G, TransparentColor_B)) End Function ' (See the last overload for documentation) Public Overloads Function TransparentBitmap(ByRef objImage As Object, ByVal TransparentColor As Integer) As Bitmap Return TransparentBitmap(objImage, Color.FromArgb(TransparentColor)) End Function '============================================================================================================= ' ' TransparentBitmap ' ' This function takes the Image or Bitmap object and returns a transparent version of it based on the ' transparent color specified. ' ' Parameter: Use: ' -------------------------------------------------- ' objImage Specifies the picture to make transparent ' TransparentColor Specifies the color to make transparent ' ' Return: ' ------- ' If the function succeeds, it returns a new transparent BITMAP ' '============================================================================================================= Public Overloads Function TransparentBitmap(ByRef objImage As Object, ByVal TransparentColor As Color) As Bitmap On Error Resume Next Dim objBitmap As Bitmap If objImage Is Nothing Then Exit Function objBitmap = New Bitmap(CType(objImage, System.Drawing.Image)) objBitmap.MakeTransparent(TransparentColor) objImage = objBitmap Err.Clear() End Function '============================================================================================================= ' ' GetBitmapInfo ' ' This function takes the bitmap image specified and returns all kinds of information on it. ' ' Parameter: Use: ' -------------------------------------------------- ' objBitmap Specifies the Bitmap to get the information for ' Return_Height Optional. Returns the height of the specified bitmap ' Return_Width Optional. Returns the width of the specified bitmap ' Return_HorizontalResolution Optional. Returns the horizontal resolution, in pixels-per-inch, of the specified bitmap ' Return_VerticalResolution Optional. Returns the vertical resolution, in pixels-per-inch, of the specified bitmap ' Return_BitsPerPixel Optional. Returns the bits per pixel (color depth) of the specified bitmap (4, 8, 16, 24, 32, 48, 64, or -1 if unknown) ' Return_PixelFormat Optional. Returns the pixel format for the specified bitmap ' Return_ColorPalette Optional. Returns the color palette of the specified bitmap ' Return_ImageFormat Optional. Returns the image format (BMP, EMF, Exif, GIF, JPEG, PNG, etc.) of the specified bitmap ' Return_Size Optional. If the bitmap is a standard 24-bit bitmap (most common), this returns the size in bytes of the bitmap. Otherwise returns -1. ' ' Return: ' ------- ' Nothing ' '============================================================================================================= Public Sub GetBitmapInfo(ByVal objBitmap As Bitmap, Optional ByRef Return_Height As Integer = 0, Optional ByRef Return_Width As Integer = 0, Optional ByRef Return_HorizontalResolution As Single = 0, Optional ByRef Return_VerticalResolution As Single = 0, Optional ByRef Return_BitsPerPixel As Short = 0, Optional ByRef Return_PixelFormat As PixelFormat = PixelFormat.DontCare, Optional ByRef Return_ColorPalette As ColorPalette = Nothing, Optional ByRef Return_ImageFormat As ImageFormat = Nothing, Optional ByRef Return_Size As Double = 0) On Error Resume Next If objBitmap Is Nothing Then Exit Sub With objBitmap Return_Height = .Height Return_Width = .Width Return_PixelFormat = .PixelFormat Return_ColorPalette = .Palette Return_ImageFormat = .RawFormat Return_HorizontalResolution = .HorizontalResolution Return_VerticalResolution = .VerticalResolution End With Select Case objBitmap.PixelFormat Case PixelFormat.Format4bppIndexed Return_BitsPerPixel = 4 Return_Size = -1 Case PixelFormat.Format8bppIndexed Return_BitsPerPixel = 8 Return_Size = -1 Case PixelFormat.Format16bppArgb1555, PixelFormat.Format16bppGrayScale, PixelFormat.Format16bppRgb555, PixelFormat.Format16bppRgb565, PixelFormat.Format1bppIndexed Return_BitsPerPixel = 16 Return_Size = -1 Case PixelFormat.Format24bppRgb Return_BitsPerPixel = 24 Return_Size = ((objBitmap.Width * 3 + 3) And &HFFFFFFFC) * objBitmap.Height Case PixelFormat.Format32bppArgb, PixelFormat.Format32bppPArgb, PixelFormat.Format32bppRgb Return_BitsPerPixel = 32 Return_Size = -1 Case PixelFormat.Format48bppRgb Return_BitsPerPixel = 48 Return_Size = -1 Case PixelFormat.Format64bppArgb, PixelFormat.Format64bppPArgb Return_BitsPerPixel = 64 Return_Size = -1 Case Else Return_BitsPerPixel = -1 Return_Size = -1 End Select Err.Clear() End Sub '============================================================================================================= ' ' GetDisplayInfo ' ' This function gets the dimentions of the primary display area and the primary work area (display area ' excluding taskbars, docked windows, and docked tool bars). ' ' Parameter: Use: ' -------------------------------------------------- ' ScreenWidth Optional. Returns the width of the primary display area. ' ScreenHeight Optional. Returns the height of the primary display area. ' ScreenTop Optional. Returns the top location of the primary display area. ' ScreenLeft Optional. Returns the left location of the primary display area. ' WorkingAreaWidth Optional. Returns the width of the primary display work area. ' WorkingAreaHeight Optional. Returns the height of the primary display work area. ' WorkingAreaTop Optional. Returns the top location of the primary display work area. ' WorkingAreaLeft Optional. Returns the left location of the primary display work area. ' ' Return: ' ------- ' Nothing ' '============================================================================================================= Public Sub GetDisplayInfo(Optional ByRef ScreenWidth As Integer = 0, Optional ByRef ScreenHeight As Integer = 0, Optional ByRef ScreenTop As Integer = 0, Optional ByRef ScreenLeft As Integer = 0, Optional ByRef WorkingAreaWidth As Integer = 0, Optional ByRef WorkingAreaHeight As Integer = 0, Optional ByRef WorkingAreaTop As Integer = 0, Optional ByRef WorkingAreaLeft As Integer = 0) On Error Resume Next With Screen.PrimaryScreen.Bounds ScreenTop = .Y ScreenLeft = .X ScreenWidth = .Right - .Left ScreenHeight = .Bottom - .Top End With With Screen.PrimaryScreen.WorkingArea WorkingAreaTop = .Y WorkingAreaLeft = .X WorkingAreaWidth = .Width WorkingAreaHeight = .Height End With Err.Clear() End Sub ' (See the last overload for documentation) Public Overloads Function RenderBitmap(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap) As Boolean Return RenderBitmap(DestinationGraphics, objBitmap, 0, 0) End Function Public Overloads Function RenderBitmap(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean On Error GoTo ErrorTrap Dim objImage As Image If objBitmap Is Nothing Then Exit Function objImage.FromHbitmap(objBitmap.GetHbitmap) If objImage Is Nothing Then Exit Function With objImage If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height DestinationGraphics.DrawImage(objImage, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), New Rectangle(Srce_X, Srce_Y, Srce_Width, Srce_Height), GraphicsUnit.Pixel) End With RenderBitmap = True Exit Function ErrorTrap: Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmap(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap) As Boolean Return RenderBitmap(DestinationControl, objBitmap, 0, 0) End Function '============================================================================================================= ' ' RenderBitmap ' ' This function takes the specified bitmap object and renders it onto the specified control (PictureBox, ' Label, Button, Panel, Form, etc.) in the specified location. ' ' NOTE: Remember that VB.NET does not support the "AutoRedraw" property like VB5 and VB6 did. It's ' STUPID that it's not supported, but what can you do?! ' ' Parameter: Use: ' -------------------------------------------------- ' DestinationControl Specifies the control to render the bitmap to. This can be ANY control or ' interface that supports the "CreateGraphics" method. ' objBitmap Specifies the bitmap to render to the control ' Dest_X Specifies the X coordinate of where to render the bitmap ' Dest_Y Specifies the Y coordinate of where to render the bitmap ' Dest_Width Optional. Specifies the width of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Dest_Height Optional. Specifies the height of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Srce_X Optional. Specifies the X location on the source image of what should be rendered. ' Srce_Y Optional. Specifies the Y location on the source image of what should be rendered. ' Srce_Width Optional. Specifies the width of the area on the source image to render (if you ' do not wish to render the entire image). Leave the default value of -1 to render ' the whole image. ' Srce_Height Optional. Specifies the height of the area on the source image to render (if ' you do not wish to render the entire image). Leave the default value of -1 to ' render the whole image. ' ' Return: ' ------- ' Returns TRUE if the function executes successfully ' Returns FALSE if the function failed to execute successfully ' '============================================================================================================= Public Overloads Function RenderBitmap(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean On Error GoTo ErrorTrap Dim objGraphics As Graphics Dim objImage As Image If DestinationControl Is Nothing Then Exit Function If objBitmap Is Nothing Then Exit Function objGraphics = DestinationControl.CreateGraphics objImage.FromHbitmap(objBitmap.GetHbitmap) If objGraphics Is Nothing Then Exit Function If objImage Is Nothing Then Exit Function With objImage If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height objGraphics.DrawImage(objImage, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), New Rectangle(Srce_X, Srce_Y, Srce_Width, Srce_Height), GraphicsUnit.Pixel) End With RenderBitmap = True Exit Function ErrorTrap: Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapGrayscale(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap) As Boolean Return RenderBitmapGrayscale(DestinationGraphics, objBitmap, 0, 0) End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapGrayscale(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean Dim objNewBMP As Bitmap Dim objImgAttrib As ImageAttributes = New ImageAttributes Dim objColorMatrix As ColorMatrix = New ColorMatrix If DestinationGraphics Is Nothing Then Exit Function If objBitmap Is Nothing Then Exit Function objNewBMP = New Bitmap(objBitmap) If objNewBMP Is Nothing Then Exit Function ' 1/3 on the top 3 rows and 3 columns With objColorMatrix .Matrix00 = 1 / 3 .Matrix01 = 1 / 3 .Matrix02 = 1 / 3 .Matrix10 = 1 / 3 .Matrix11 = 1 / 3 .Matrix12 = 1 / 3 .Matrix20 = 1 / 3 .Matrix21 = 1 / 3 .Matrix22 = 1 / 3 End With objImgAttrib.SetColorMatrix(objColorMatrix) With objNewBMP If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height DestinationGraphics.DrawImage(objNewBMP, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), Srce_X, Srce_Y, Srce_Width, Srce_Height, GraphicsUnit.Pixel, objImgAttrib) End With End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapGrayscale(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap) As Boolean Return RenderBitmapGrayscale(DestinationControl, objBitmap, 0, 0) End Function '============================================================================================================= ' ' RenderBitmapGrayscale ' ' This function takes the specified bitmap object, applies a grayscale filter to it, and renders it onto the ' specified control (PictureBox, Label, Button, Panel, Form, etc.) in the specified location. ' ' NOTE: Remember that VB.NET does not support the "AutoRedraw" property like VB5 and VB6 did. It's ' STUPID that it's not supported, but what can you do?! ' ' Parameter: Use: ' -------------------------------------------------- ' DestinationControl Specifies the control to render the grayscale bitmap to. This can be ANY control ' or interface that supports the "CreateGraphics" method. ' objBitmap Specifies the bitmap to render to the control ' Dest_X Specifies the X coordinate of where to render the bitmap ' Dest_Y Specifies the Y coordinate of where to render the bitmap ' Dest_Width Optional. Specifies the width of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Dest_Height Optional. Specifies the height of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Srce_X Optional. Specifies the X location on the source image of what should be rendered. ' Srce_Y Optional. Specifies the Y location on the source image of what should be rendered. ' Srce_Width Optional. Specifies the width of the area on the source image to render (if you ' do not wish to render the entire image). Leave the default value of -1 to render ' the whole image. ' Srce_Height Optional. Specifies the height of the area on the source image to render (if ' you do not wish to render the entire image). Leave the default value of -1 to ' render the whole image. ' ' Return: ' ------- ' Returns TRUE if the function executes successfully ' Returns FALSE if the function failed to execute successfully ' '============================================================================================================= Public Overloads Function RenderBitmapGrayscale(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean Dim objGraphics As Graphics Dim objNewBMP As Bitmap Dim objImgAttrib As ImageAttributes = New ImageAttributes Dim objColorMatrix As ColorMatrix = New ColorMatrix If DestinationControl Is Nothing Then Exit Function If objBitmap Is Nothing Then Exit Function objGraphics = DestinationControl.CreateGraphics objNewBMP = New Bitmap(objBitmap) If objGraphics Is Nothing Then Exit Function If objNewBMP Is Nothing Then Exit Function ' 1/3 on the top 3 rows and 3 columns With objColorMatrix .Matrix00 = 1 / 3 .Matrix01 = 1 / 3 .Matrix02 = 1 / 3 .Matrix10 = 1 / 3 .Matrix11 = 1 / 3 .Matrix12 = 1 / 3 .Matrix20 = 1 / 3 .Matrix21 = 1 / 3 .Matrix22 = 1 / 3 End With objImgAttrib.SetColorMatrix(objColorMatrix) With objNewBMP If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height objGraphics.DrawImage(objNewBMP, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), Srce_X, Srce_Y, Srce_Width, Srce_Height, GraphicsUnit.Pixel, objImgAttrib) End With End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapTransparent(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap, _ ByVal TransparentColor As Color) As Boolean Return RenderBitmapTransparent(DestinationGraphics, objBitmap, TransparentColor, 0, 0) End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapTransparent(ByVal DestinationGraphics As Graphics, _ ByVal objBitmap As Bitmap, _ ByVal TransparentColor As Color, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean On Error GoTo ErrorTrap Dim objImage As Image Dim objBMP As Bitmap If objBitmap Is Nothing Then Exit Function objBMP = New Bitmap(objBitmap) objBMP.MakeTransparent(TransparentColor) objImage.FromHbitmap(objBMP.GetHbitmap) If objImage Is Nothing Then Exit Function With objImage If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height DestinationGraphics.DrawImage(objImage, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), New Rectangle(Srce_X, Srce_Y, Srce_Width, Srce_Height), GraphicsUnit.Pixel) End With RenderBitmapTransparent = True Exit Function ErrorTrap: Err.Clear() End Function ' (See the last overload for documentation) Public Overloads Function RenderBitmapTransparent(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap, _ ByVal TransparentColor As Color) As Boolean Return RenderBitmapTransparent(DestinationControl, objBitmap, TransparentColor, 0, 0) End Function '============================================================================================================= ' ' RenderBitmapTransparent ' ' This function takes the specified bitmap object and renders it transparently (using the specified ' transparent color) onto the specified control (PictureBox, Label, Button, Panel, Form, etc.) in ' the specified location. ' ' NOTE: Remember that VB.NET does not support the "AutoRedraw" property like VB5 and VB6 did. It's ' STUPID that it's not supported, but what can you do?! ' ' Parameter: Use: ' -------------------------------------------------- ' DestinationControl Specifies the control to render the bitmap to. This can be ANY control or ' interface that supports the "CreateGraphics" method. ' objBitmap Specifies the bitmap to render to the control ' TransparentColor Specifies the transparent color to use ' Dest_X Specifies the X coordinate of where to render the bitmap ' Dest_Y Specifies the Y coordinate of where to render the bitmap ' Dest_Width Optional. Specifies the width of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Dest_Height Optional. Specifies the height of the bitmap to render if you wish to stretch ' the image. Leave the default value of -1 if you do not wish to stretch the image. ' Srce_X Optional. Specifies the X location on the source image of what should be rendered. ' Srce_Y Optional. Specifies the Y location on the source image of what should be rendered. ' Srce_Width Optional. Specifies the width of the area on the source image to render (if you ' do not wish to render the entire image). Leave the default value of -1 to render ' the whole image. ' Srce_Height Optional. Specifies the height of the area on the source image to render (if ' you do not wish to render the entire image). Leave the default value of -1 to ' render the whole image. ' ' Return: ' ------- ' Returns TRUE if the function executes successfully ' Returns FALSE if the function failed to execute successfully ' '============================================================================================================= Public Overloads Function RenderBitmapTransparent(ByVal DestinationControl As Object, _ ByVal objBitmap As Bitmap, _ ByVal TransparentColor As Color, _ ByVal Dest_X As Integer, _ ByVal Dest_Y As Integer, _ Optional ByVal Dest_Width As Integer = -1, _ Optional ByVal Dest_Height As Integer = -1, _ Optional ByVal Srce_X As Integer = 0, _ Optional ByVal Srce_Y As Integer = 0, _ Optional ByVal Srce_Width As Integer = -1, _ Optional ByVal Srce_Height As Integer = -1) As Boolean On Error GoTo ErrorTrap Dim objGraphics As Graphics Dim objImage As Image Dim objBMP As Bitmap If DestinationControl Is Nothing Then Exit Function If objBitmap Is Nothing Then Exit Function objGraphics = DestinationControl.CreateGraphics objBMP = New Bitmap(objBitmap) objBMP.MakeTransparent(TransparentColor) objImage.FromHbitmap(objBMP.GetHbitmap) If objGraphics Is Nothing Then Exit Function If objImage Is Nothing Then Exit Function With objImage If Dest_Width = -1 Then Dest_Width = .Width If Dest_Height = -1 Then Dest_Height = .Height If Srce_Width = -1 Then Srce_Width = .Width If Srce_Height = -1 Then Srce_Height = .Height objGraphics.DrawImage(objImage, New Rectangle(Dest_X, Dest_Y, Dest_Width, Dest_Height), New Rectangle(Srce_X, Srce_Y, Srce_Width, Srce_Height), GraphicsUnit.Pixel) End With RenderBitmapTransparent = True Exit Function ErrorTrap: Err.Clear() End Function #End Region End Module