Nasıl yapılır: Yol Gradyanı Oluşturma
PathGradientBrushSınıfı, bir şekli aşamalı olarak değişen renklerle doldurmanıza olanak sağlar. Örneğin, bir yolun ortası için bir renk ve yolun sınırı için başka bir renk belirtebilirsiniz. Ayrıca, bir yolun sınırı boyunca birkaç noktadan her biri için ayrı renkler belirtebilirsiniz.
Not
GDI+, bir yol bir nesne tarafından tutulan çizgiler ve eğrilerden oluşan bir dizidir GraphicsPath . GDI+ yolları hakkında daha fazla bilgi için bkz. GDI+ içindeki grafik yolları ve yolları oluşturma ve çizme.
Bu makaledeki örnekler, bir denetimin Paint olay işleyicisinden çağrılan yöntemlerdir.
Bir elipsi yol degradeyle doldurmanız için
Aşağıdaki örnek, bir elipsi yol gradyan fırçası ile doldurur. Merkez rengi mavi olarak ayarlanır ve sınır rengi deniz mavisi olarak ayarlanır. Aşağıdaki çizimde doldurulmuş elips gösterilmektedir.

Varsayılan olarak, bir yol gradyan fırçası yolun sınırının dışına genişlemez. Yolun sınırının ötesine geçen bir şekli doldurmak için yol gradyan fırçası ' nı kullanırsanız, yolun dışındaki ekranın alanı doldurulmaz.
Aşağıdaki resimde aşağıdaki kodda yapılan çağrıyı değiştirirseniz ne olacağı gösterilmektedir Graphics.FillEllipse
e.Graphics.FillRectangle(pthGrBrush, 0, 10, 200, 40):
public void FillEllipseWithPathGradient(PaintEventArgs e) { // Create a path that consists of a single ellipse. GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 140, 70); // Use the path to construct a brush. PathGradientBrush pthGrBrush = new PathGradientBrush(path); // Set the color at the center of the path to blue. pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255); // Set the color along the entire boundary // of the path to aqua. Color[] colors = { Color.FromArgb(255, 0, 255, 255) }; pthGrBrush.SurroundColors = colors; e.Graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70); }' Create a path that consists of a single ellipse. Dim path As New GraphicsPath() path.AddEllipse(0, 0, 140, 70) ' Use the path to construct a brush. Dim pthGrBrush As New PathGradientBrush(path) ' Set the color at the center of the path to blue. pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255) ' Set the color along the entire boundary ' of the path to aqua. Dim colors As Color() = {Color.FromArgb(255, 0, 255, 255)} pthGrBrush.SurroundColors = colors e.Graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70)yukarıdaki kod örneği, Windows Forms ile kullanım için tasarlanmıştır ve PaintEventArgs parametresi olan e 'yi gerektirir PaintEventHandler .
Sınır üzerine noktaları belirtmek için
Aşağıdaki örnek, yıldız şeklindeki bir yoldan yol gradyan fırçası oluşturur. Kod, CenterColor yıldızın centroıd kısmındaki rengi kırmızı olarak ayarlayan özelliği ayarlar. Daha sonra kod, SurroundColors dizideki tek noktalarda çeşitli renkler (dizide depolanır) belirtmek için özelliğini ayarlar
colorspoints. Son kod ifadesinde, yıldız şeklindeki yol gradyan fırçası ile doldurulur.public void ConstructBrushFromStarShapedPath(PaintEventArgs e) { // Put the points of a polygon in an array. Point[] points = { new Point(75, 0), new Point(100, 50), new Point(150, 50), new Point(112, 75), new Point(150, 150), new Point(75, 100), new Point(0, 150), new Point(37, 75), new Point(0, 50), new Point(50, 50)}; // Use the array of points to construct a path. GraphicsPath path = new GraphicsPath(); path.AddLines(points); // Use the path to construct a path gradient brush. PathGradientBrush pthGrBrush = new PathGradientBrush(path); // Set the color at the center of the path to red. pthGrBrush.CenterColor = Color.FromArgb(255, 255, 0, 0); // Set the colors of the points in the array. Color[] colors = { Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 0, 255, 0), Color.FromArgb(255, 0, 0, 255), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 0, 255, 0), Color.FromArgb(255, 0, 0, 255), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 0, 255, 0)}; pthGrBrush.SurroundColors = colors; // Fill the path with the path gradient brush. e.Graphics.FillPath(pthGrBrush, path); }' Put the points of a polygon in an array. Dim points As Point() = { _ New Point(75, 0), _ New Point(100, 50), _ New Point(150, 50), _ New Point(112, 75), _ New Point(150, 150), _ New Point(75, 100), _ New Point(0, 150), _ New Point(37, 75), _ New Point(0, 50), _ New Point(50, 50)} ' Use the array of points to construct a path. Dim path As New GraphicsPath() path.AddLines(points) ' Use the path to construct a path gradient brush. Dim pthGrBrush As New PathGradientBrush(path) ' Set the color at the center of the path to red. pthGrBrush.CenterColor = Color.FromArgb(255, 255, 0, 0) ' Set the colors of the points in the array. Dim colors As Color() = { _ Color.FromArgb(255, 0, 0, 0), _ Color.FromArgb(255, 0, 255, 0), _ Color.FromArgb(255, 0, 0, 255), _ Color.FromArgb(255, 255, 255, 255), _ Color.FromArgb(255, 0, 0, 0), _ Color.FromArgb(255, 0, 255, 0), _ Color.FromArgb(255, 0, 0, 255), _ Color.FromArgb(255, 255, 255, 255), _ Color.FromArgb(255, 0, 0, 0), _ Color.FromArgb(255, 0, 255, 0)} pthGrBrush.SurroundColors = colors ' Fill the path with the path gradient brush. e.Graphics.FillPath(pthGrBrush, path)Aşağıdaki örnek, koddaki bir nesne olmadan bir yol gradyanı çizer GraphicsPath . Örnekteki belirli PathGradientBrush Oluşturucu bir dizi işaret alır, ancak bir GraphicsPath nesne gerektirmez. Ayrıca, bir PathGradientBrush yolu değil dikdörtgeni dolduriçin kullanıldığını unutmayın. Dikdörtgen, fırçayı tanımlamak için kullanılan kapalı yoldan daha büyükse, dikdörtgenden bazıları fırça tarafından boyanmaz. Aşağıdaki çizimde, dikdörtgenin (noktalı çizgi) ve yolun gradyan fırçası tarafından boyanmış olan kısmı gösterilmektedir:

public void DrawPathGradentWthoutGraphicsPath(PaintEventArgs e) { // Construct a path gradient brush based on an array of points. PointF[] ptsF = { new PointF(0, 0), new PointF(160, 0), new PointF(160, 200), new PointF(80, 150), new PointF(0, 200)}; PathGradientBrush pBrush = new PathGradientBrush(ptsF); // An array of five points was used to construct the path gradient // brush. Set the color of each point in that array. Color[] colors = { Color.FromArgb(255, 255, 0, 0), // (0, 0) red Color.FromArgb(255, 0, 255, 0), // (160, 0) green Color.FromArgb(255, 0, 255, 0), // (160, 200) green Color.FromArgb(255, 0, 0, 255), // (80, 150) blue Color.FromArgb(255, 255, 0, 0)}; // (0, 200) red pBrush.SurroundColors = colors; // Set the center color to white. pBrush.CenterColor = Color.White; // Use the path gradient brush to fill a rectangle. e.Graphics.FillRectangle(pBrush, new Rectangle(0, 0, 160, 200)); }' Construct a path gradient brush based on an array of points. Dim ptsF As PointF() = { _ New PointF(0, 0), _ New PointF(160, 0), _ New PointF(160, 200), _ New PointF(80, 150), _ New PointF(0, 200)} Dim pBrush As New PathGradientBrush(ptsF) ' An array of five points was used to construct the path gradient ' brush. Set the color of each point in that array. 'Point (0, 0) is red 'Point (160, 0) is green 'Point (160, 200) is green 'Point (80, 150) is blue 'Point (0, 200) is red Dim colors As Color() = { _ Color.FromArgb(255, 255, 0, 0), _ Color.FromArgb(255, 0, 255, 0), _ Color.FromArgb(255, 0, 255, 0), _ Color.FromArgb(255, 0, 0, 255), _ Color.FromArgb(255, 255, 0, 0)} pBrush.SurroundColors = colors ' Set the center color to white. pBrush.CenterColor = Color.White ' Use the path gradient brush to fill a rectangle. e.Graphics.FillRectangle(pBrush, New Rectangle(0, 0, 160, 200))
Yol degradesini özelleştirmek için
Yol gradyan fırçası 'nı özelleştirmenin bir yolu, özelliğini ayarlamaya yönelik bir yoldur FocusScales . Odak ölçeği, ana yolun içinde yer alan bir iç yol belirtir. Merkez rengi, yalnızca orta nokta yerine bu iç yolun içinde her yerde görüntülenir.
Aşağıdaki örnek, elips yolunu temel alan bir yol gradyan fırçası oluşturur. Kod, sınır rengini mavi olarak ayarlar, orta rengi deniz mavisi olarak ayarlar ve ardından elips yolunu dolduracak şekilde yol gradyan fırçası kullanır.
Daha sonra kod, yol gradyan fırçasının odak ölçeklerini ayarlar. X odak Ölçeği 0,3 olarak ayarlanır ve y odak Ölçeği 0,8 olarak ayarlanır. Kod, TranslateTransformGraphics sonraki çağrının FillPath ilk elipsin sağına oturan bir elipsi doldurması için bir nesnenin yöntemini çağırır.
Odak ölçeklerinin etkisini görmek için ana elips ile merkezini paylaşan küçük bir elips düşünün. Küçük (iç) elips, 0,3 faktörü ile yatay olarak ölçeklendirilen ana elips (merkeziyle yaklaşık olarak) ve 0,8 faktörüyle dikey olarak ayarlanır. Dış elips sınırından iç elipsin sınırına geçtiğinizde, renk mavi ile deniz mavisi arasında değişir. İç elipsin sınırından paylaşılan merkezine geçtiğinizde, renk deniz mavisi kalır.
Aşağıdaki çizimde aşağıdaki kodun çıkışı gösterilmektedir. Sol taraftaki elips yalnızca merkez noktada açık olur. Sağdaki elips, iç yolun içindeki her yerde açık bir şekilde bulunur.

public void CustomizePathGradientBrush(PaintEventArgs e)
{
// Create a path that consists of a single ellipse.
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0, 0, 200, 100);
// Create a path gradient brush based on the elliptical path.
PathGradientBrush pthGrBrush = new PathGradientBrush(path);
// Set the color along the entire boundary to blue.
Color[] color = { Color.Blue };
pthGrBrush.SurroundColors = color;
// Set the center color to aqua.
pthGrBrush.CenterColor = Color.Aqua;
// Use the path gradient brush to fill the ellipse.
e.Graphics.FillPath(pthGrBrush, path);
// Set the focus scales for the path gradient brush.
pthGrBrush.FocusScales = new PointF(0.3f, 0.8f);
// Use the path gradient brush to fill the ellipse again.
// Show this filled ellipse to the right of the first filled ellipse.
e.Graphics.TranslateTransform(220.0f, 0.0f);
e.Graphics.FillPath(pthGrBrush, path);
}
' Create a path that consists of a single ellipse.
Dim path As New GraphicsPath()
path.AddEllipse(0, 0, 200, 100)
' Create a path gradient brush based on the elliptical path.
Dim pthGrBrush As New PathGradientBrush(path)
' Set the color along the entire boundary to blue.
' Changed variable name from color
Dim blueColor As Color() = {Color.Blue}
pthGrBrush.SurroundColors = blueColor
' Set the center color to aqua.
pthGrBrush.CenterColor = Color.Aqua
' Use the path gradient brush to fill the ellipse.
e.Graphics.FillPath(pthGrBrush, path)
' Set the focus scales for the path gradient brush.
pthGrBrush.FocusScales = New PointF(0.3F, 0.8F)
' Use the path gradient brush to fill the ellipse again.
' Show this filled ellipse to the right of the first filled ellipse.
e.Graphics.TranslateTransform(220.0F, 0.0F)
e.Graphics.FillPath(pthGrBrush, path)
İlişkilendirme ile özelleştirmek için
Yol gradyan fırçası 'nı özelleştirmenin başka bir yolu da enterpolasyon renkleri dizisi ve ilişkilendirme konumları dizisi belirtmektir.
Aşağıdaki örnek, bir üçgeni temel alan bir yol gradyan fırçası oluşturur. Kod, InterpolationColors yol gradyanı fırçasının özelliğini, bir enterpolasyon renkleri dizisi (koyu yeşil, deniz mavisi, mavi) ve ilişkilendirme konumları dizisi (0, 0,25, 1) belirtmek için ayarlar. Üçgenin sınırından orta noktaya geçtiğinizde, renk koyu yeşil ve sonra deniz mavisi ile mavi arasında değişir. Koyu yeşil ile deniz mavisi arasındaki değişiklik, koyu yeşil ile mavi arasındaki uzaklığın yüzde 25 ' i oranında olur.
Aşağıdaki çizimde, özel yol gradyan fırçası ile doldurulmuş üçgen gösterilmektedir.

public void CustomizeWithInterpolation(PaintEventArgs e) { // Vertices of the outer triangle Point[] points = { new Point(100, 0), new Point(200, 200), new Point(0, 200)}; // No GraphicsPath object is created. The PathGradientBrush // object is constructed directly from the array of points. PathGradientBrush pthGrBrush = new PathGradientBrush(points); Color[] colors = { Color.FromArgb(255, 0, 128, 0), // dark green Color.FromArgb(255, 0, 255, 255), // aqua Color.FromArgb(255, 0, 0, 255)}; // blue float[] relativePositions = { 0f, // Dark green is at the boundary of the triangle. 0.4f, // Aqua is 40 percent of the way from the boundary // to the center point. 1.0f}; // Blue is at the center point. ColorBlend colorBlend = new ColorBlend(); colorBlend.Colors = colors; colorBlend.Positions = relativePositions; pthGrBrush.InterpolationColors = colorBlend; // Fill a rectangle that is larger than the triangle // specified in the Point array. The portion of the // rectangle outside the triangle will not be painted. e.Graphics.FillRectangle(pthGrBrush, 0, 0, 200, 200); }' Vertices of the outer triangle Dim points As Point() = { _ New Point(100, 0), _ New Point(200, 200), _ New Point(0, 200)} ' No GraphicsPath object is created. The PathGradientBrush ' object is constructed directly from the array of points. Dim pthGrBrush As New PathGradientBrush(points) ' Create an array of colors containing dark green, aqua, and blue. Dim colors As Color() = { _ Color.FromArgb(255, 0, 128, 0), _ Color.FromArgb(255, 0, 255, 255), _ Color.FromArgb(255, 0, 0, 255)} ' Dark green is at the boundary of the triangle. ' Aqua is 40 percent of the way from the boundary to the center point. ' Blue is at the center point. Dim relativePositions As Single() = { _ 0.0F, _ 0.4F, _ 1.0F} Dim colorBlend As New ColorBlend() colorBlend.Colors = colors colorBlend.Positions = relativePositions pthGrBrush.InterpolationColors = colorBlend ' Fill a rectangle that is larger than the triangle ' specified in the Point array. The portion of the ' rectangle outside the triangle will not be painted. e.Graphics.FillRectangle(pthGrBrush, 0, 0, 200, 200)
Merkez noktasını ayarlamak için
Varsayılan olarak, bir yol gradyan fırçasının orta noktası, fırçayı oluşturmak için kullanılan yolun centroıd 'si olur. Sınıfın özelliğini ayarlayarak merkez noktasının konumunu değiştirebilirsiniz CenterPointPathGradientBrush .
Aşağıdaki örnek, bir elips temelinde yol gradyan fırçası oluşturur. Elips Merkezi (70, 35), ancak yol gradyan fırçasının orta noktası (120, 40) olarak ayarlanır.
public void SetCenterPoint(PaintEventArgs e) { // Create a path that consists of a single ellipse. GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 140, 70); // Use the path to construct a brush. PathGradientBrush pthGrBrush = new PathGradientBrush(path); // Set the center point to a location that is not // the centroid of the path. pthGrBrush.CenterPoint = new PointF(120, 40); // Set the color at the center of the path to blue. pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255); // Set the color along the entire boundary // of the path to aqua. Color[] colors = { Color.FromArgb(255, 0, 255, 255) }; pthGrBrush.SurroundColors = colors; e.Graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70); }' Create a path that consists of a single ellipse. Dim path As New GraphicsPath() path.AddEllipse(0, 0, 140, 70) ' Use the path to construct a brush. Dim pthGrBrush As New PathGradientBrush(path) ' Set the center point to a location that is not ' the centroid of the path. pthGrBrush.CenterPoint = New PointF(120, 40) ' Set the color at the center of the path to blue. pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255) ' Set the color along the entire boundary ' of the path to aqua. Dim colors As Color() = {Color.FromArgb(255, 0, 255, 255)} pthGrBrush.SurroundColors = colors e.Graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70)Aşağıdaki çizimde, yol gradyan fırçasının doldurulmuş elips ve orta noktası gösterilmektedir:

Bir yol gradyanı fırçasının merkez noktasını, fırçayı oluşturmak için kullanılan yolun dışında bir konuma ayarlayabilirsiniz. Aşağıdaki örnek, önceki kodda özelliğini ayarlamak için çağrısının yerini alır CenterPoint .
public void SetCenterPointOutsidePath(PaintEventArgs e) { // Create a path that consists of a single ellipse. GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 140, 70); // Use the path to construct a brush. PathGradientBrush pthGrBrush = new PathGradientBrush(path); // Set the center point to a location that is not // the centroid of the path. pthGrBrush.CenterPoint = new PointF(145, 35); // Set the color at the center of the path to blue. pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255); // Set the color along the entire boundary // of the path to aqua. Color[] colors = { Color.FromArgb(255, 0, 255, 255) }; pthGrBrush.SurroundColors = colors; e.Graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70); }pthGrBrush.CenterPoint = New PointF(145, 35)Aşağıdaki çizim, bu değişikliğe sahip çıktıyı göstermektedir:

Yukarıdaki çizimde, elipsin en sağdaki noktaları saf mavi değildir (çok yakın olsa da). Gradyandaki renkler, rengin saf mavi (0, 0, 255) olacağı noktaya (145, 35) ulaşmış gibi konumlandırılır. Ancak, bir yol gradyan fırçası yalnızca yolunun içinde boyaydığı için Fill hiçbir an (145, 35) öğesine ulaşmaz.
Kod Derleniyor
yukarıdaki örnekler, Windows Forms kullanımı için tasarlanmıştır ve PaintEventArgse olay işleyicisinin bir parametresi olan gerektirir Paint .