Graphics and Multimedia Topics include: GDI+, drawing 2D/3D, printing and Managed DirectX and the XNA Framework

Go Back  Xtreme .NET Talk > .NET > Graphics and Multimedia > Draw Transparent Rectangle


Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2005, 07:43 AM
PROKA's Avatar
PROKA PROKA is offline
Junior Contributor

Preferred language:
VB.NET, C#
 
Join Date: Sep 2003
Location: Bucharest
Posts: 249
PROKA is on a distinguished road
Default Draw Transparent Rectangle



I know how to draw rectangles. The problem is that when I draw another rectangle, it doesn't show what's behind him ( the old rectangles ). How can I draw a 'transparent rectangle'

See attached files (VB.NET)
__________________
Development & Research Department @ Elven Soft
Reply With Quote
  #2  
Old 09-26-2005, 12:04 PM
Machaira Machaira is offline
Junior Contributor

Preferred language:
VB.NET, C#
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 325
Machaira is on a distinguished road
Default

Umm, there's no attached files.
Reply With Quote
  #3  
Old 09-26-2005, 02:20 PM
DiverDan's Avatar
DiverDan DiverDan is offline
Contributor

Preferred language:
vb.net
 
Join Date: Jan 2003
Location: Sacramento, CA
Posts: 645
DiverDan is on a distinguished road
Default

Hi Proka,

It sounds like you're filling the rectangle instead of just drawing the lines?
__________________
Member, in good standing, of the elite fraternity of mentally challenged programmers.

Dolphins Software
Reply With Quote
  #4  
Old 09-26-2005, 06:59 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default

I'm not sure exactly what you are looking for but you can fill rectangles with semi-transparent colors by using the Color.FromARGB function, specifying a value from 0 to 255 for opacity, red, green and blue values and then call Graphics.FillRectangle using a SolidBrush created from your ARGB color.
Code:
'I haven't tested this code
Dim MyBrush As New SolidBrush(Color.FromARGB(128, 0, 0, 255))
MyGraphics.FillRectangle(MyBrush, New Rectangle(0, 0, 100, 100))
'It should draw a 50% opaque blue rectangle 100 px by 100 px at 0,0.
 
__________________
e
Reply With Quote
  #5  
Old 09-26-2005, 07:18 PM
DiverDan's Avatar
DiverDan DiverDan is offline
Contributor

Preferred language:
vb.net
 
Join Date: Jan 2003
Location: Sacramento, CA
Posts: 645
DiverDan is on a distinguished road
Default

As a note, alpha channel "semi-transparent" colors as suggested by marble eater are only supported from Windows 2000 and up.
__________________
Member, in good standing, of the elite fraternity of mentally challenged programmers.

Dolphins Software
Reply With Quote
  #6  
Old 09-26-2005, 07:44 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default

DiverDan, not for the sake of contradicting you, but for the sake of posting accurate information, I'm going to point out that although "Layered Windows" (used for semi-transparent forms) is only supported on Windows 2000/Xp and up, other semi-transparency using Windows Forms is a feature of the GDI+ library included with the .Net Framework and is fully supported in any .Net application. Try using the Graphics object and controls with semi-transparent BackColors on the Win9x/NT platforms and you will still see the nifty semi-transparent effects.
__________________
e
Reply With Quote
  #7  
Old 09-26-2005, 08:28 PM
DiverDan's Avatar
DiverDan DiverDan is offline
Contributor

Preferred language:
vb.net
 
Join Date: Jan 2003
Location: Sacramento, CA
Posts: 645
DiverDan is on a distinguished road
Default

I didn't know that, thanks marble eater!
__________________
Member, in good standing, of the elite fraternity of mentally challenged programmers.

Dolphins Software
Reply With Quote
  #8  
Old 09-27-2005, 06:41 AM
PROKA's Avatar
PROKA PROKA is offline
Junior Contributor

Preferred language:
VB.NET, C#
 
Join Date: Sep 2003
Location: Bucharest
Posts: 249
PROKA is on a distinguished road
Default

Sorry, here's the attachment
__________________
Development & Research Department @ Elven Soft

Last edited by PROKA; 03-13-2007 at 06:44 AM.
Reply With Quote
  #9  
Old 09-29-2005, 08:02 AM
Machaira Machaira is offline
Junior Contributor

Preferred language:
VB.NET, C#
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 325
Machaira is on a distinguished road
Default

Well, that project doesn't compile for me and I don't have the time to figure out the fix. What version of VB was that done in? I'm using the most recent beta of VB.NET 2005 EE.
Reply With Quote
  #10  
Old 10-04-2005, 03:39 PM
Iceplug's Avatar
Iceplug Iceplug is offline
Contributor

Preferred language:
VB.NET, C#, J#
 
Join Date: Aug 2001
Location: California, USA
Posts: 709
Iceplug is on a distinguished road
Default

Ok, PROKA, here is what is wrong with your display.
You are drawing one rectangle in the form's Paint event. This in itself will only guarantee that one rectangle will be visible on your form.

When you create a new rectangle in MouseUp, you invalidate the region where you place the rectangle, so, even though:
you may not have a rectangle in this region and
you can successfully place it so that the old rectangle is not invalidated

The fact remains that you are only drawing one rectangle in the Paint event.

So, when the rectangles that you have drawn overlap, the one you are drawing now is the one that will dominate. Furthermore, the region of the rectangle is invalidated, therefore giving it an "opaque center".

I suggest that you create your own graphics object.
Dim GFX As Graphics

In Form_Load or somewhere on the form that runs first:
GFX = Me.CreateGraphics()

Now, instead of invalidating your display, which clears out a hole on the display,
just draw the rectangle.
GFX.DrawRectangle(Pens.Gray, RcDraw)

This solution will cause your rectangles to disappear when the form disappears or is obscured. If you want to keep the rectangles persistent, then keep an array of rectangles, go back to this solution you have already, with the Invalidate and the drawing in the Paint and, in the Pait event, use e.Graphics.DrawRectangles to draw a bunch of rectangles.
__________________

Iceplug, USN
One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Reply With Quote
  #11  
Old 11-08-2005, 04:48 PM
PiggyBank1974 PiggyBank1974 is offline
Newcomer
 
Join Date: Sep 2004
Posts: 9
PiggyBank1974 is on a distinguished road
Default

you can make a color have an alpha channel:

do something like this:

Code:
e.Graphics.FillRectangle(color.fromargb(127.5,255,255,255), New Rectangle(0, 0, 100, 100))

this will generate a filled rectangle that is 50% transparent.

the pig..
Reply With Quote
  #12  
Old 11-08-2005, 06:54 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default

A similar reply has already been posted. Also, this thread has had no new posts for over a month and should be considered inactive.

I hope that I am not out of line when I recommend refreshing yourself on the Posting Guidelines.
__________________
e
Reply With Quote
Reply

Bookmarks

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Draw Transparent Rectangle WITHOUT OnPaintBackground Pselus Graphics and Multimedia 47 09-13-2006 08:34 PM
Cursor position and draw rectangle on desktop... lidds Windows Forms 4 05-14-2006 08:34 PM
How to draw a rectangle on a webpage? LiLo ASP.NET 0 05-04-2006 04:15 AM
Does anyone knows how to draw a Black outlined Rectangle around a Button, piscis Graphics and Multimedia 2 09-25-2003 08:10 AM
Draw Rectangle on top of screen/Desktop cheng_sh General 4 05-28-2003 12:36 AM

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