Gradients
Gradients
Gradients
Gradients
Gradients
Gradients Gradients Gradients Gradients Gradients Gradients Gradients Gradients
Gradients Gradients
Gradients
Go Back  Xtreme Visual Basic Talk > > > Gradients


Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2005, 02:35 PM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default Gradients


4-Corner Gradient?

How?

Thanks

John

(er... TopLeftCol, TopRightCol, BotLeftCol, BotRightCol)
Reply With Quote
  #2  
Old 06-09-2005, 03:06 PM
snarfblam's Avatar
snarfblamGradients snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

System.Drawing.Drawing2D.PathGradientBrush.

Try it.

Any specific problems in implementation, ask.

Welcome.
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 06-10-2005, 05:52 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

I've already tried this but I can't really work it out :-(

I'm newish from VB6

I've got

Code:
Sub DrawGradient(ByVal pStart As PointF, _ ByVal pEnd As PointF, _ ByRef GDI As Graphics, _ ByVal ColTopLeft As Color, _ ByVal ColTopRight As Color, _ ByVal ColBottomLeft As Color, _ ByVal ColBottomRight As Color)

if it helps!

Last edited by reboot; 06-11-2005 at 09:34 AM.
Reply With Quote
  #4  
Old 06-11-2005, 05:15 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Anybody? I can't find anything about this anywhere and PathGradientBrush doesn't seem to do what I need and nor does LinearGradientBrush.
Reply With Quote
  #5  
Old 06-11-2005, 09:29 AM
snarfblam's Avatar
snarfblamGradients snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Quote:
Originally Posted by 3D-Reality
Anybody? I can't find anything about this anywhere and PathGradientBrush doesn't seem to do what I need and nor does LinearGradientBrush.
I knew what it did, but I didn't know how to use PathGradientBrush myself. I figured it out using MSDN and intellisense, and I'm sure you could to. (Sorry Posting Guidelines).

This will fill in a form with a four-corner gradient. There are two example because I don't know whether you want a multi-color gradient or just a two color gradient.

Code:
Sub FillMeUp(ByVal OuterColor As Color, ByVal InnerColor As Color) Dim G As Graphics = Me.CreateGraphics 'Corners of the gradient Dim Points As Point() = {New Point(0, 0), _ New Point(0, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, 0)} 'Create gradient object Dim Broosh As New Drawing2D.PathGradientBrush(Points) 'Set colors Broosh.SurroundColors = New Color() {OuterColor} Broosh.CenterColor = InnerColor 'And fill the region G.FillRectangle(Broosh, Me.ClientRectangle) End Sub Sub FillMeUp(ByVal OuterColors As Color(), ByVal InnerColor As Color) Dim G As Graphics = Me.CreateGraphics 'Corners of the gradient Dim Points As Point() = {New Point(0, 0), _ New Point(0, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, 0)} 'Create gradient object Dim Broosh As New Drawing2D.PathGradientBrush(Points) 'Set colors Broosh.SurroundColors = OuterColors Broosh.CenterColor = InnerColor 'And fill the region G.FillRectangle(Broosh, Me.ClientRectangle) End Sub

P.S. Do us all a favor and break long lines of code up with an underscore "_" like I did in the example. Otherwise it stretches the forum way out.
__________________
C# _VB.NET _
Reply With Quote
  #6  
Old 06-11-2005, 03:10 PM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Sorry for sounding stupid but how would I work out the inner colour.

I mean like I only want to specify each corner :-S

Why can't it be easy like VB!

LOL!

John
Reply With Quote
  #7  
Old 06-12-2005, 09:09 AM
snarfblam's Avatar
snarfblamGradients snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

You might want to use a different path or even multiple gradients to achive greater control over the gradient. Depending on your needs, you might be able to simply set the center color to the average of the four corner colors. If you were drawing a triangle with red, green and blue corners, the center should be gray (this is the average of red, green and blue, and it is what looks most natural). Perhaps if you posted an image of what you would like to achieve, I could give more specific advice.
__________________
C# _VB.NET _
Reply With Quote
  #8  
Old 06-12-2005, 10:09 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

http://objects.3d-reality.com/img.jpg

I only set the 4 corners here. This was done in VB with a gradient class.

The following API were used:

Code:
Private Declare Function GradientFillRect Lib "msimg32" Alias "GradientFill" (ByVal hdc As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, _ pMesh As GRADIENT_RECT, ByVal dwNumMesh As Long, ByVal dwMode As _ Long) As Long Private Declare Function GradientFill Lib "msimg32" (ByVal hdc As Long, _ pVertex As Any, ByVal dwNumVertex As Long, pMesh As Any, ByVal _ dwNumMesh As Long, ByVal dwMode As Long) As Long


I didn't write the class
Reply With Quote
  #9  
Old 06-12-2005, 10:43 AM
snarfblam's Avatar
snarfblamGradients snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

In the case of the image you provided, what I said would work perfectly: set he center to the average. Here is a rewrite of the function I provided:
Code:
'Draw a four corner gradient, automatically calculating the center color Sub FillMeUp(ByVal CornerColors As Color()) Dim G As Graphics = Me.CreateGraphics 'Corners of the gradient Dim Points As Point() = {New Point(0, 0), _ New Point(0, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _ New Point(Me.ClientRectangle.Width, 0)} 'Get the center color Dim Red, Green, Blue As Integer For Each Corner As Color In CornerColors Red += Corner.R Green += Corner.G Blue += Corner.B Next Red \= CornerColors.Length Green \= CornerColors.Length Blue \= CornerColors.Length 'Create gradient object Dim Broosh As New Drawing2D.PathGradientBrush(Points) 'Set colors Broosh.SurroundColors = CornerColors Broosh.CenterColor = Color.FromArgb(Red, Green, Blue) 'And fill the region G.FillRectangle(Broosh, Me.ClientRectangle) End Sub
__________________
C# _VB.NET _
Reply With Quote
  #10  
Old 06-12-2005, 10:48 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Cheers!

(oh and TY x 50000)

LOL!

John
Reply With Quote
  #11  
Old 06-12-2005, 11:04 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Code:
For X As Integer = 2 To Heights.GetUpperBound(0) For Y As Integer = 2 To Heights.GetUpperBound(1) Cols(0) = Color.Red 'TOP LEFT Cols(1) = Color.Blue 'BOTTOM LEFT Cols(2) = Color.Red 'BOTTOM RIGHT Cols(3) = Color.Red 'TOP RIGHT p1 = Nothing p2 = Nothing p1 = New Point(((x - 2) * xWidth), ((y - 2) * yHeight)) p2 = New Point(p1.X + xWidth - 1, p1.Y + yHeight - 1) Console.WriteLine("From " & p1.X & "," & p1.Y & " to " & p2.X & "," & p2.Y) DrawGradient(p1, p2, Cols) Next Next

That is just a test implementation but do you have any idea why it only shows the first one?

Thanks
John
Reply With Quote
  #12  
Old 06-12-2005, 11:05 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Code:
Sub DrawGradient(ByVal pStart As Point, ByVal pEnd As Point, ByVal CornerColors As Color()) Dim Points As Point() = {New Point(pStart.X, pStart.Y), _ New Point(pStart.X, Math.Abs(pEnd.Y - pStart.Y)), _ New Point(Math.Abs(pEnd.X - pStart.X), Math.Abs(pEnd.Y - pStart.Y)), _ New Point(Math.Abs(pEnd.X - pStart.X), pStart.Y)} Dim Red, Green, Blue As Integer For Each Corner As Color In CornerColors Red += Corner.R Green += Corner.G Blue += Corner.B Next Red \= CornerColors.Length Green \= CornerColors.Length Blue \= CornerColors.Length Dim Broosh As New Drawing2D.PathGradientBrush(Points) Broosh.SurroundColors = CornerColors Broosh.CenterColor = Color.FromArgb(Red, Green, Blue) Gdi.FillRectangle(Broosh, New RectangleF(pStart.X, pStart.Y, Math.Abs(pEnd.X - pStart.X), Math.Abs(pEnd.Y - pStart.Y))) End Sub

^
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Gradients
Gradients
Gradients Gradients
Gradients
Gradients
Gradients Gradients Gradients Gradients Gradients Gradients Gradients
Gradients
Gradients
 
Gradients
Gradients
 
-->