VB.NET-Tipp 0065: Farbton einer Bitmap ändern
von Frank Schüler
Beschreibung
Dieses Beispiel zeigt wie per ColorMatrix der Farbton (Hue) einer Bitmap verändert werden kann.
Schwierigkeitsgrad: | Framework-Version(en): .NET Framework 1.0, .NET Framework 1.1, .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5 | .NET-Version(en): Visual Basic 2005, Visual Basic 2008 | Download: |
' Dieser Quellcode stammt von http://www.activevb.de ' und kann frei verwendet werden. Für eventuelle Schäden ' wird nicht gehaftet. ' Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum. ' Ansonsten viel Spaß und Erfolg mit diesem Source! ' Projektversion: Visual Studio 2005 ' Option Strict: An ' ' Referenzen: ' - System ' - System.Data ' - System.Deployment ' - System.Drawing ' - System.Windows.Forms ' - System.Xml ' ' Imports: ' - Microsoft.VisualBasic ' - System ' - System.Collections ' - System.Collections.Generic ' - System.Data ' - System.Drawing ' - System.Diagnostics ' - System.Windows.Forms ' ' ############################################################################## ' ################################# Form1.vb ################################### ' ############################################################################## Option Strict On Option Explicit On Imports System.Drawing Imports System.Drawing.Imaging Public Class Form1 ''' <summary> ''' Farbton eines Bitmaps ändern ''' </summary> ''' <param name="InBitmap">Bitmap-Objekt</param> ''' <param name="Hue">Hue (0 bis 360, 0 oder 360 = Normal)</param> ''' <returns>Bitmap-Objekt</returns> Private Function SetHue(ByVal InBitmap As Bitmap, _ Optional ByVal Hue As Single = 50) As Bitmap ' Min/Max If Hue < 0 Then Hue = 0 If Hue > 360 Then Hue = 360 Dim RedR As New Single Dim RedG As New Single Dim RedB As New Single Dim GreenR As New Single Dim GreenG As New Single Dim GreenB As New Single Dim BlueR As New Single Dim BlueG As New Single Dim BlueB As New Single Dim Sector As Integer = CInt(Hue) \ 60 Hue = Hue / 60 Select Case Sector Case 0 RedR = 1 : GreenR = -Hue : BlueR = Hue RedG = Hue : GreenG = 1 : BlueG = -Hue RedB = -Hue : GreenB = Hue : BlueB = 1 Case 1 RedR = 2 - Hue : GreenR = Hue - 2 : BlueR = 1 RedG = 1 : GreenG = 2 - Hue : BlueG = Hue - 2 RedB = Hue - 2 : GreenB = 1 : BlueB = 2 - Hue Case 2 RedR = 2 - Hue : GreenR = Hue - 2 : BlueR = 1 RedG = 1 : GreenG = 2 - Hue : BlueG = Hue - 2 RedB = Hue - 2 : GreenB = 1 : BlueB = 2 - Hue Case 3 RedR = Hue - 4 : GreenR = 1 : BlueR = 4 - Hue RedG = 4 - Hue : GreenG = Hue - 4 : BlueG = 1 RedB = 1 : GreenB = 4 - Hue : BlueB = Hue - 4 Case 4 RedR = Hue - 4 : GreenR = 1 : BlueR = 4 - Hue RedG = 4 - Hue : GreenG = Hue - 4 : BlueG = 1 RedB = 1 : GreenB = 4 - Hue : BlueB = Hue - 4 Case 5 RedR = 1 : GreenR = 6 - Hue : BlueR = Hue - 6 RedG = Hue - 6 : GreenG = 1 : BlueG = 6 - Hue RedB = 6 - Hue : GreenB = Hue - 6 : BlueB = 1 Case 6 RedR = 1 : GreenR = 0 : BlueR = 0 RedG = 0 : GreenG = 1 : BlueG = 0 RedB = 0 : GreenB = 0 : BlueB = 1 End Select ' ColorMatrix erstellen Dim Matrix As New Imaging.ColorMatrix(New Single()() _ {New Single() {RedR, RedG, RedB, 0, 0}, _ New Single() {GreenR, GreenG, GreenB, 0, 0}, _ New Single() {BlueR, BlueG, BlueB, 0, 0}, _ New Single() {0, 0, 0, 1, 0}, _ New Single() {0, 0, 0, 0, 1}}) ' neue Bitmap erstellen Dim NewBmp As New Bitmap(InBitmap.Width, InBitmap.Height, _ Drawing.Imaging.PixelFormat.Format24bppRgb) ' ImageAttribute-Objekt erstellen Using ImageAttr As New Imaging.ImageAttributes() ' ColorMatrix für das ImageAttribute-Objekt setzen ImageAttr.SetColorMatrix(Matrix) ' Graphics-Objekt von NewBmp erstellen Using NewBmpGra As Graphics = Graphics.FromImage(NewBmp) ' InBitmap in das Graphics-Objekt zeichnen NewBmpGra.DrawImage(InBitmap, New Rectangle(0, 0, _ InBitmap.Width, InBitmap.Height), 0, 0, _ InBitmap.Width, InBitmap.Height, _ GraphicsUnit.Pixel, ImageAttr) 'Graphics-Objekt löschen End Using ' ImageAttribute-Objekt löschen End Using Return NewBmp End Function Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ' Bild aus der Ressource laden und anzeigen PictureBox1.Image = My.Resources.City005 PictureBox2.Image = My.Resources.City005 End Sub Private Sub HScrollBar1_Scroll( _ ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ScrollEventArgs) _ Handles HScrollBar1.Scroll ' ist ein Bild in der PictureBox vorhanden If Not PictureBox2.Image Is Nothing Then ' Bild löschen PictureBox2.Image.Dispose() End If PictureBox2.Image = SetHue( _ CType(PictureBox1.Image, Bitmap), HScrollBar1.Value) End Sub End Class
Ihre Meinung
Falls Sie Fragen zu diesem Artikel haben oder Ihre Erfahrung mit anderen Nutzern austauschen möchten, dann teilen Sie uns diese bitte in einem der unten vorhandenen Themen oder über einen neuen Beitrag mit. Hierzu können sie einfach einen Beitrag in einem zum Thema passenden Forum anlegen, welcher automatisch mit dieser Seite verknüpft wird.