VB.Net Tutorial/2D Graphics/Color — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:55, 26 мая 2010
Содержание
- 1 Alpha blending color
- 2 Color Alpha
- 3 Color.FromArgb
- 4 Color.FromArgb(-5)
- 5 Color.FromHtml
- 6 Color.FromName
- 7 Color.FromWin32
- 8 Color: Red, Green, Blue, Hue, Saturation, Brightness, Sector
- 9 ColorTranslator.FromHtml("#0000ff")
- 10 Convert color from string (FF00FF)
- 11 Create Color from R G B value and Alpha
- 12 Create Color from R G B values
- 13 Create Color from SystemColors
- 14 Get Color from static fields defined in Color
- 15 Get Hue, Saturation, Brightness from a color
- 16 KnownColor.ActiveBorder
- 17 System.Drawing.ColorTranslator.FromOle
- 18 Transparent Color
- 19 Using different colors in Visual Basic
Alpha blending color
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AlphaBlending
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() g.Clear(Me.BackColor) Dim curImage As Image = Image.FromFile("yourfile.jpg") g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height) Dim opqPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10) Dim transPen As New Pen(Color.FromArgb(128, 0, 255, 0), 10) Dim totTransPen As New Pen(Color.FromArgb(40, 0, 255, 0), 10) g.DrawLine(opqPen, 10, 10, 200, 10) g.DrawLine(transPen, 10, 30, 200, 30) g.DrawLine(totTransPen, 10, 50, 200, 50) Dim semiTransBrush As New SolidBrush(Color.FromArgb(60, 0, 255, 0)) g.FillRectangle(semiTransBrush, 20, 100, 200, 100) g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Color Alpha
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TransparentColor
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick Me.Refresh() End Sub Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles Me.Paint Static currentSetting As Integer = 0 Static changeFactor As Integer = 1 Dim transparentGreen As Color Dim canvas As Graphics = e.Graphics currentSetting += changeFactor If (currentSetting = 0) Or (currentSetting = 255) Then changeFactor = -changeFactor End If transparentGreen = Color.FromArgb(currentSetting, 0, 255, 0) canvas.FillRectangle(New SolidBrush(transparentGreen), 50, 50, 250, 100) End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "Timer1 " Me.Timer1.Enabled = True Me.Timer1.Interval = 10 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(491, 384) Me.DoubleBuffered = True Me.Name = "Form1" Me.Text = "Animating with Transparency" Me.ResumeLayout(False) End Sub Friend WithEvents Timer1 As System.Windows.Forms.Timer
End Class</source>
Color.FromArgb
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorCreation
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() Dim redColor As Color = Color.FromArgb(120, 255, 0, 0) Dim blueColor As Color = Color.FromName("Blue") Dim greenColor As Color = Color.FromKnownColor(KnownColor.Green) Dim tstColor As Color = Color.Empty If greenColor.IsEmpty Then tstColor = Color.DarkGoldenrod End If Dim redBrush As New SolidBrush(redColor) Dim blueBrush As New SolidBrush(blueColor) Dim greenBrush As New SolidBrush(greenColor) Dim greenPen As New Pen(greenBrush, 4) g.FillEllipse(redBrush, 10, 10, 50, 50) g.FillRectangle(blueBrush, 60, 10, 50, 50) g.DrawLine(greenPen, 20, 60, 200, 60) Console.WriteLine("Color Name :" + blueColor.Name + ", A:" + blueColor.A.ToString() + ", R:" + blueColor.R.ToString() + ", B:" + blueColor.B.ToString() + ", G:" + blueColor.G.ToString()) redBrush.Dispose() blueBrush.Dispose() greenBrush.Dispose() greenPen.Dispose() g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Color.FromArgb(-5)
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorFromArgb1090518785
public Shared Sub Main Application.Run(New DrawingForm) End Sub
End class
Public Class DrawingForm
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents drawEllipseButton As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.drawEllipseButton = New System.Windows.Forms.Button() Me.SuspendLayout() " "drawEllipseButton " Me.drawEllipseButton.Location = New System.Drawing.Point(43, 32) Me.drawEllipseButton.Name = "drawEllipseButton" Me.drawEllipseButton.TabIndex = 1 Me.drawEllipseButton.Text = "Draw Ellipse" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(160, 86) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.drawEllipseButton}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub DrawingForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint Dim blue75PercentOpaque As Color = Color.FromArgb(-1090518785) Dim g As Graphics = e.Graphics Dim b As Brush = New SolidBrush(blue75PercentOpaque) g.FillEllipse(b, Me.ClientRectangle) End Sub
End Class</source>
Color.FromHtml
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class FromHtml
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() " Translate colors Dim win32Color As Color = ColorTranslator.FromWin32(&HFF0033) Dim htmlColor As Color = ColorTranslator.FromHtml("#00AAFF") " Using colors Dim redBrush As New SolidBrush(win32Color) Dim blueBrush As New SolidBrush(htmlColor) " Drawing GDI+ objects g.FillEllipse(redBrush, 10, 10, 50, 50) g.FillRectangle(blueBrush, 60, 10, 50, 50) "Dispose redBrush.Dispose() blueBrush.Dispose() g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Color.FromName
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorCreation
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() Dim redColor As Color = Color.FromArgb(120, 255, 0, 0) Dim blueColor As Color = Color.FromName("Blue") Dim greenColor As Color = Color.FromKnownColor(KnownColor.Green) Dim tstColor As Color = Color.Empty If greenColor.IsEmpty Then tstColor = Color.DarkGoldenrod End If Dim redBrush As New SolidBrush(redColor) Dim blueBrush As New SolidBrush(blueColor) Dim greenBrush As New SolidBrush(greenColor) Dim greenPen As New Pen(greenBrush, 4) g.FillEllipse(redBrush, 10, 10, 50, 50) g.FillRectangle(blueBrush, 60, 10, 50, 50) g.DrawLine(greenPen, 20, 60, 200, 60) Console.WriteLine("Color Name :" + blueColor.Name + ", A:" + blueColor.A.ToString() + ", R:" + blueColor.R.ToString() + ", B:" + blueColor.B.ToString() + ", G:" + blueColor.G.ToString()) redBrush.Dispose() blueBrush.Dispose() greenBrush.Dispose() greenPen.Dispose() g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Color.FromWin32
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class FromHtml
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() " Translate colors Dim win32Color As Color = ColorTranslator.FromWin32(&HFF0033) Dim htmlColor As Color = ColorTranslator.FromHtml("#00AAFF") " Using colors Dim redBrush As New SolidBrush(win32Color) Dim blueBrush As New SolidBrush(htmlColor) " Drawing GDI+ objects g.FillEllipse(redBrush, 10, 10, 50, 50) g.FillRectangle(blueBrush, 60, 10, 50, 50) "Dispose redBrush.Dispose() blueBrush.Dispose() g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Color: Red, Green, Blue, Hue, Saturation, Brightness, Sector
<source lang="vbnet">" Quote from "Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers "by Tim Patrick (Author), John Craig (Author) "# Publisher: O"Reilly Media, Inc. (September 21, 2006) "# Language: English "# ISBN-10: 0596101775 "# ISBN-13: 978-0596101770
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorRGBSH
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Private Sub RBG_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) _ Handles ValueRed.Scroll, ValueGreen.Scroll, ValueBlue.Scroll Dim rgbColor As Color rgbColor = Color.FromArgb(0, ValueRed.Value, ValueGreen.Value, ValueBlue.Value) ValueHue.Value = CInt(rgbColor.GetHue()) ValueSaturation.Value = CInt(rgbColor.GetSaturation() * 100.0F) ValueBrightness.Value = CInt(rgbColor.GetBrightness() * 100.0F) RefreshDisplay() End Sub Private Sub ValueHue_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) _ Handles ValueHue.Scroll, ValueSaturation.Scroll, ValueBrightness.Scroll Dim useRed As Integer Dim useGreen As Integer Dim useBlue As Integer Dim useHue As Single Dim useSaturation As Single Dim useBrightness As Single Dim hueSector As Integer Dim factor As Single Dim target1 As Single Dim target2 As Single Dim target3 As Single useHue = CSng(ValueHue.Value) useSaturation = CSng(ValueSaturation.Value) / 100.0F useBrightness = CSng(ValueBrightness.Value) / 100.0F If (useSaturation = 0.0F) Then useRed = CInt(useBrightness * 255) useGreen = useRed useBlue = useRed Else hueSector = CInt(useHue / 60.0F) factor = Math.Abs((useHue / 60.0F) - CSng(hueSector)) target1 = useBrightness * (1 - useSaturation) target2 = useBrightness * (1 - (factor * useSaturation)) target3 = useBrightness * (1 - ((1 - factor) * useSaturation)) Select Case hueSector Case 0, 6 useRed = CInt(useBrightness * 255.0F) useGreen = CInt(target3 * 255.0F) useBlue = CInt(target1 * 255.0F) Case 1 useRed = CInt(target2 * 255.0F) useGreen = CInt(useBrightness * 255.0F) useBlue = CInt(target1 * 255.0F) Case 2 useRed = CInt(target1 * 255.0F) useGreen = CInt(useBrightness * 255.0F) useBlue = CInt(target3 * 255.0F) Case 3 useRed = CInt(target1 * 255.0F) useGreen = CInt(target2 * 255.0F) useBlue = CInt(useBrightness * 255.0F) Case 4 useRed = CInt(target3 * 255.0F) useGreen = CInt(target1 * 255.0F) useBlue = CInt(useBrightness * 255.0F) Case 5 useRed = CInt(useBrightness * 255.0F) useGreen = CInt(target1 * 255.0F) useBlue = CInt(target2 * 255.0F) End Select End If ValueRed.Value = useRed ValueGreen.Value = useGreen ValueBlue.Value = useBlue RefreshDisplay() End Sub Private Sub RefreshDisplay() NumberRed.Text = CStr(ValueRed.Value) NumberGreen.Text = CStr(ValueGreen.Value) NumberBlue.Text = CStr(ValueBlue.Value) NumberHue.Text = CStr(ValueHue.Value) NumberSaturation.Text = Format(CDec(ValueSaturation.Value) / 100@, "0.00") NumberBrightness.Text = Format(CDec(ValueBrightness.Value) / 100@, "0.00") ShowColor.BackColor = Color.FromArgb(255, ValueRed.Value, _ ValueGreen.Value, ValueBlue.Value) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load RBG_Scroll(ValueRed, New Windows.Forms.ScrollEventArgs(ScrollEventType.EndScroll, 0)) End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.ShowColor = New System.Windows.Forms.PictureBox Me.LabelRed = New System.Windows.Forms.Label Me.ValueRed = New System.Windows.Forms.HScrollBar Me.NumberRed = New System.Windows.Forms.Label Me.NumberGreen = New System.Windows.Forms.Label Me.ValueGreen = New System.Windows.Forms.HScrollBar Me.LabelGreen = New System.Windows.Forms.Label Me.NumberBlue = New System.Windows.Forms.Label Me.ValueBlue = New System.Windows.Forms.HScrollBar Me.LabelBlue = New System.Windows.Forms.Label Me.NumberBrightness = New System.Windows.Forms.Label Me.ValueBrightness = New System.Windows.Forms.HScrollBar Me.LabelBrightness = New System.Windows.Forms.Label Me.NumberSaturation = New System.Windows.Forms.Label Me.ValueSaturation = New System.Windows.Forms.HScrollBar Me.LabelSaturation = New System.Windows.Forms.Label Me.NumberHue = New System.Windows.Forms.Label Me.ValueHue = New System.Windows.Forms.HScrollBar Me.LabelHue = New System.Windows.Forms.Label CType(Me.ShowColor, System.ruponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() " "ShowColor " Me.ShowColor.BackColor = System.Drawing.SystemColors.Control Me.ShowColor.Location = New System.Drawing.Point(8, 80) Me.ShowColor.Name = "ShowColor" Me.ShowColor.Size = New System.Drawing.Size(336, 32) Me.ShowColor.TabIndex = 1 Me.ShowColor.TabStop = False " "LabelRed " Me.LabelRed.AutoSize = True Me.LabelRed.Location = New System.Drawing.Point(8, 10) Me.LabelRed.Name = "LabelRed" Me.LabelRed.Size = New System.Drawing.Size(27, 13) Me.LabelRed.TabIndex = 0 Me.LabelRed.Text = "Red" " "ValueRed " Me.ValueRed.LargeChange = 1 Me.ValueRed.Location = New System.Drawing.Point(72, 8) Me.ValueRed.Maximum = 255 Me.ValueRed.Name = "ValueRed" Me.ValueRed.Size = New System.Drawing.Size(224, 16) Me.ValueRed.TabIndex = 1 " "NumberRed " Me.NumberRed.AutoSize = True Me.NumberRed.Location = New System.Drawing.Point(304, 10) Me.NumberRed.Name = "NumberRed" Me.NumberRed.Size = New System.Drawing.Size(13, 13) Me.NumberRed.TabIndex = 2 Me.NumberRed.Text = "0" " "NumberGreen " Me.NumberGreen.AutoSize = True Me.NumberGreen.Location = New System.Drawing.Point(304, 34) Me.NumberGreen.Name = "NumberGreen" Me.NumberGreen.Size = New System.Drawing.Size(13, 13) Me.NumberGreen.TabIndex = 5 Me.NumberGreen.Text = "0" " "ValueGreen " Me.ValueGreen.LargeChange = 1 Me.ValueGreen.Location = New System.Drawing.Point(72, 32) Me.ValueGreen.Maximum = 255 Me.ValueGreen.Name = "ValueGreen" Me.ValueGreen.Size = New System.Drawing.Size(224, 16) Me.ValueGreen.TabIndex = 4 " "LabelGreen " Me.LabelGreen.AutoSize = True Me.LabelGreen.Location = New System.Drawing.Point(8, 34) Me.LabelGreen.Name = "LabelGreen" Me.LabelGreen.Size = New System.Drawing.Size(36, 13) Me.LabelGreen.TabIndex = 3 Me.LabelGreen.Text = "Green" " "NumberBlue " Me.NumberBlue.AutoSize = True Me.NumberBlue.Location = New System.Drawing.Point(304, 58) Me.NumberBlue.Name = "NumberBlue" Me.NumberBlue.Size = New System.Drawing.Size(13, 13) Me.NumberBlue.TabIndex = 8 Me.NumberBlue.Text = "0" " "ValueBlue " Me.ValueBlue.LargeChange = 1 Me.ValueBlue.Location = New System.Drawing.Point(72, 56) Me.ValueBlue.Maximum = 255 Me.ValueBlue.Name = "ValueBlue" Me.ValueBlue.Size = New System.Drawing.Size(224, 16) Me.ValueBlue.TabIndex = 7 " "LabelBlue " Me.LabelBlue.AutoSize = True Me.LabelBlue.Location = New System.Drawing.Point(8, 58) Me.LabelBlue.Name = "LabelBlue" Me.LabelBlue.Size = New System.Drawing.Size(28, 13) Me.LabelBlue.TabIndex = 6 Me.LabelBlue.Text = "Blue" " "NumberBrightness " Me.NumberBrightness.AutoSize = True Me.NumberBrightness.Location = New System.Drawing.Point(304, 170) Me.NumberBrightness.Name = "NumberBrightness" Me.NumberBrightness.Size = New System.Drawing.Size(13, 13) Me.NumberBrightness.TabIndex = 17 Me.NumberBrightness.Text = "0" " "ValueBrightness " Me.ValueBrightness.LargeChange = 1 Me.ValueBrightness.Location = New System.Drawing.Point(72, 168) Me.ValueBrightness.Name = "ValueBrightness" Me.ValueBrightness.Size = New System.Drawing.Size(224, 16) Me.ValueBrightness.TabIndex = 16 " "LabelBrightness " Me.LabelBrightness.AutoSize = True Me.LabelBrightness.Location = New System.Drawing.Point(8, 170) Me.LabelBrightness.Name = "LabelBrightness" Me.LabelBrightness.Size = New System.Drawing.Size(56, 13) Me.LabelBrightness.TabIndex = 15 Me.LabelBrightness.Text = "Brightness" " "NumberSaturation " Me.NumberSaturation.AutoSize = True Me.NumberSaturation.Location = New System.Drawing.Point(304, 146) Me.NumberSaturation.Name = "NumberSaturation" Me.NumberSaturation.Size = New System.Drawing.Size(13, 13) Me.NumberSaturation.TabIndex = 14 Me.NumberSaturation.Text = "0" " "ValueSaturation " Me.ValueSaturation.LargeChange = 1 Me.ValueSaturation.Location = New System.Drawing.Point(72, 144) Me.ValueSaturation.Name = "ValueSaturation" Me.ValueSaturation.Size = New System.Drawing.Size(224, 16) Me.ValueSaturation.TabIndex = 13 " "LabelSaturation " Me.LabelSaturation.AutoSize = True Me.LabelSaturation.Location = New System.Drawing.Point(8, 146) Me.LabelSaturation.Name = "LabelSaturation" Me.LabelSaturation.Size = New System.Drawing.Size(55, 13) Me.LabelSaturation.TabIndex = 12 Me.LabelSaturation.Text = "Saturation" " "NumberHue " Me.NumberHue.AutoSize = True Me.NumberHue.Location = New System.Drawing.Point(304, 122) Me.NumberHue.Name = "NumberHue" Me.NumberHue.Size = New System.Drawing.Size(13, 13) Me.NumberHue.TabIndex = 11 Me.NumberHue.Text = "0" " "ValueHue " Me.ValueHue.LargeChange = 1 Me.ValueHue.Location = New System.Drawing.Point(72, 120) Me.ValueHue.Maximum = 360 Me.ValueHue.Name = "ValueHue" Me.ValueHue.Size = New System.Drawing.Size(224, 16) Me.ValueHue.TabIndex = 10 " "LabelHue " Me.LabelHue.AutoSize = True Me.LabelHue.Location = New System.Drawing.Point(8, 122) Me.LabelHue.Name = "LabelHue" Me.LabelHue.Size = New System.Drawing.Size(27, 13) Me.LabelHue.TabIndex = 9 Me.LabelHue.Text = "Hue" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(354, 195) Me.Controls.Add(Me.NumberBrightness) Me.Controls.Add(Me.ValueBrightness) Me.Controls.Add(Me.LabelBrightness) Me.Controls.Add(Me.NumberSaturation) Me.Controls.Add(Me.ValueSaturation) Me.Controls.Add(Me.LabelSaturation) Me.Controls.Add(Me.NumberHue) Me.Controls.Add(Me.ValueHue) Me.Controls.Add(Me.LabelHue) Me.Controls.Add(Me.NumberBlue) Me.Controls.Add(Me.ValueBlue) Me.Controls.Add(Me.LabelBlue) Me.Controls.Add(Me.NumberGreen) Me.Controls.Add(Me.ValueGreen) Me.Controls.Add(Me.LabelGreen) Me.Controls.Add(Me.NumberRed) Me.Controls.Add(Me.ValueRed) Me.Controls.Add(Me.LabelRed) Me.Controls.Add(Me.ShowColor) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "RBG and HSV/HSB" CType(Me.ShowColor, System.ruponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents ShowColor As System.Windows.Forms.PictureBox Friend WithEvents LabelRed As System.Windows.Forms.Label Friend WithEvents ValueRed As System.Windows.Forms.HScrollBar Friend WithEvents NumberRed As System.Windows.Forms.Label Friend WithEvents NumberGreen As System.Windows.Forms.Label Friend WithEvents ValueGreen As System.Windows.Forms.HScrollBar Friend WithEvents LabelGreen As System.Windows.Forms.Label Friend WithEvents NumberBlue As System.Windows.Forms.Label Friend WithEvents ValueBlue As System.Windows.Forms.HScrollBar Friend WithEvents LabelBlue As System.Windows.Forms.Label Friend WithEvents NumberBrightness As System.Windows.Forms.Label Friend WithEvents ValueBrightness As System.Windows.Forms.HScrollBar Friend WithEvents LabelBrightness As System.Windows.Forms.Label Friend WithEvents NumberSaturation As System.Windows.Forms.Label Friend WithEvents ValueSaturation As System.Windows.Forms.HScrollBar Friend WithEvents LabelSaturation As System.Windows.Forms.Label Friend WithEvents NumberHue As System.Windows.Forms.Label Friend WithEvents ValueHue As System.Windows.Forms.HScrollBar Friend WithEvents LabelHue As System.Windows.Forms.Label
End Class</source>
ColorTranslator.FromHtml("#0000ff")
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class KnownColorActiveBorder
public Shared Sub Main Console.WriteLine(ColorTranslator.FromHtml("#0000ff")) End Sub
End class</source>
Color [A=255, R=0, G=0, B=255]
Convert color from string (FF00FF)
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class FF00FF
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() g.Clear(Me.BackColor) Dim str As String = "#FF00FF" Dim clrConverter As New ColorConverter Dim clr1 As Color = CType(clrConverter.ConvertFromString(str), Color) Dim redBrush As New SolidBrush(clr1) Dim blueBrush As New SolidBrush(clr1) g.FillEllipse(redBrush, 10, 10, 50, 50) g.FillRectangle(blueBrush, 60, 10, 50, 50) redBrush.Dispose() blueBrush.Dispose() g.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Create Color from R G B value and Alpha
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsCreateGraphicColorFromRGBAlpha
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim canvas As Graphics = e.Graphics Dim colorBackground As Color = Color.FromArgb(127, 0, 255, 123) canvas.Clear(colorBackground) canvas = Nothing End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SuspendLayout() " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(610, 328) Me.Name = "Form1" Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)" Me.ResumeLayout(False) End Sub
End Class</source>
Create Color from R G B values
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsCreateGraphicColorFromRGB
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim canvas As Graphics = e.Graphics Dim colorBackground As Color = Color.FromArgb(255, 123, 0) canvas.Clear(colorBackground) canvas = Nothing End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SuspendLayout() " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(610, 328) Me.Name = "Form1" Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)" Me.ResumeLayout(False) End Sub
End Class</source>
Create Color from SystemColors
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsCreateGraphicColorSystemColors
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim canvas As Graphics = e.Graphics Dim colorBackground As Color = SystemColors.ControlDark canvas.Clear(colorBackground) canvas = Nothing End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SuspendLayout() " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(610, 328) Me.Name = "Form1" Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)" Me.ResumeLayout(False) End Sub
End Class</source>
Get Color from static fields defined in Color
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorBlueViolet
public Shared Sub Main Application.Run(New DrawingForm) End Sub
End class
Public Class DrawingForm
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents drawEllipseButton As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.drawEllipseButton = New System.Windows.Forms.Button() Me.SuspendLayout() " "drawEllipseButton " Me.drawEllipseButton.Location = New System.Drawing.Point(43, 32) Me.drawEllipseButton.Name = "drawEllipseButton" Me.drawEllipseButton.TabIndex = 1 Me.drawEllipseButton.Text = "Draw Ellipse" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(160, 86) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.drawEllipseButton}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub DrawingForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint Dim blue1 As Color = Color.BlueViolet Dim g As Graphics = e.Graphics Dim b As Brush = New SolidBrush(blue1) g.FillEllipse(b, Me.ClientRectangle) End Sub
End Class</source>
Get Hue, Saturation, Brightness from a color
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GetSaturationGetBrightness
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics() " Create a color Dim clr As Color = Color.FromArgb(255, 200, 0, 100) " Get hue, saturation, and brightness components Dim h As Single = clr.GetHue() Dim s As Single = clr.GetSaturation() Dim v As Single = clr.GetBrightness() Dim str As String = "Hue: " + h.ToString() + ControlChars.Lf + "Saturation: " + s.ToString() + ControlChars.Lf + "Brightness: " + v.ToString() " Display data g.DrawString(str, New Font("verdana", 12), Brushes.Blue, 50, 50) " Dispose g.Dispose()
End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
KnownColor.ActiveBorder
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class KnownColorActiveBorder
public Shared Sub Main Console.WriteLine(Color.FromKnownColor(KnownColor.ActiveBorder)) End Sub
End class</source>
Color [ActiveBorder]
System.Drawing.ColorTranslator.FromOle
<source lang="vbnet">Imports System.Windows.Forms Imports System.Drawing.Text Imports System.Drawing Imports System.Drawing.Drawing2D public class ColorFromOle
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.BackColor = System.Drawing.ColorTranslator.FromOle(QBColor(1)) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.BackColor = System.Drawing.ColorTranslator.FromOle(RGB(255, 255, 0)) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim mycolor As Color Me.BackColor = mycolor.FromArgb(1, 2, 2) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Me.BackColor = Color.Blue End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Me.BackColor = System.Drawing.ColorTranslator.FromOle(&H808080) End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(12, 184) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(75, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = "QBColor" Me.Button1.UseVisualStyleBackColor = True " "Button2 " Me.Button2.Location = New System.Drawing.Point(93, 184) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(75, 23) Me.Button2.TabIndex = 1 Me.Button2.Text = "RGB" Me.Button2.UseVisualStyleBackColor = True " "Button3 " Me.Button3.Location = New System.Drawing.Point(174, 184) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(75, 23) Me.Button3.TabIndex = 2 Me.Button3.Text = "FromARGB" Me.Button3.UseVisualStyleBackColor = True " "Button4 " Me.Button4.Location = New System.Drawing.Point(255, 184) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(94, 23) Me.Button4.TabIndex = 3 Me.Button4.Text = "Color Constant" Me.Button4.UseVisualStyleBackColor = True " "Button5 " Me.Button5.Location = New System.Drawing.Point(355, 184) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(92, 23) Me.Button5.TabIndex = 4 Me.Button5.Text = "Long Integer" Me.Button5.UseVisualStyleBackColor = True " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(459, 246) Me.Controls.Add(Me.Button5) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button
End Class</source>
Transparent Color
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TransparentColor
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics Dim curImage As Image = Image.FromFile("yourfile.jpg") g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height) Dim opqPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10) Dim transPen As New Pen(Color.FromArgb(128, 0, 255, 0), 10) Dim totTransPen As New Pen(Color.FromArgb(40, 0, 255, 0), 10) g.DrawLine(opqPen, 10, 10, 200, 10) g.DrawLine(transPen, 10, 30, 200, 30) g.DrawLine(totTransPen, 10, 50, 200, 50) Dim semiTransBrush As New SolidBrush(Color.FromArgb(60, 0, 255, 0)) g.FillRectangle(semiTransBrush, 20, 100, 200, 100) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Using different colors in Visual Basic
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ShowColorsVB
public Shared Sub Main Application.Run(New FrmColorForm) End Sub
End class
Public Class FrmColorForm
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.Container "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(304, 309) End Sub
- End Region
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim mBehindColor As Color = Color.Wheat Dim mFrontColor As Color = Color.FromArgb(100, 0, 0, 255) Dim graphicsObject As Graphics = e.Graphics Dim textBrush As SolidBrush = New SolidBrush(Color.Black) Dim brush As SolidBrush = New SolidBrush(Color.White) graphicsObject.FillRectangle(brush, 4, 4, 275, 180) graphicsObject.DrawString(mBehindColor.Name, Me.Font, textBrush, 40, 5) brush.Color = mBehindColor graphicsObject.FillRectangle(brush, 45, 20, 150, 120) graphicsObject.DrawString("Alpha: " & mFrontColor.A & _ " Red: " & mFrontColor.R & " Green: " & mFrontColor.G _ & " Blue: " & mFrontColor.B, Me.Font, textBrush, _ 55, 165) brush.Color = mFrontColor graphicsObject.FillRectangle(brush, 65, 35, 170, 130) End Sub " OnPaint
End Class</source>