473,779 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Graphics.FillRe ctangle / TransformPoints Bug??

Hello All,

I am attempting to learn a bit about the GDI+ transforms and charting data and I
feel like I'm getting a handle on how the transforms work. My chart object has a
large "canvas" bitmap that all data is scaled to and rendered on, and I display
a smaller "window" of the canvas that the user can page through left and right.

I've been able to scale my data (for the purposes of this discussion, x values
are 0 - 2PI and y values are -1 to 1; I'm charting Cosine) to the canvas bitmap
using the transforms applied to the graphics object and to a transformation
matrix, which I then useto transform the points and draw them on a graphics
object that has no transforms applied. I used both methods because I wanted to
learn how each works. So far, so good.

Next, I attempted to draw rectangles to highlight particular regions of
interest. This is where I get confused. I first worked with the transformed
graphics object and was able to get the rectangle to draw, but the values that I
had to use for its starting location do not make sense to me. Let me show the
transforms necessary to scale and fit my data to the canvas.

The canvas is 4096 x 100 pixels; the window is 512 x 100 pixels. Data is 0 - 2PI
for x values and -1 to 1 for y values. The transforms are as follows:

o Scale(1, -1) ; flip y coordinates
o Translate(0, -1) ; move ymin to origin
o Scale((4096/2PI), (100/(1 - -1)))
o Translate(0, 100)

This works out to be:

o Scale(1, -1)
o Translate(0, ymin)
o Scale((canvas_w idth/(xmax - xmmin)), (canvas_height/(ymax - ymin)))
o Translate(0, canvas_height)

I believe the first Translate call works because the ymin of -1 is first scaled
by -1 and becomes 1, 1 - 1 = 0 so we are back at the origin.

The crux of this is that I can draw rectangles using the graphics object that
has transforms applied to it, and it works as expected (i.e. if I want to draw a
rectangle that covers half of my window, I say:
FillRectangle(0 , -1, PI/8.0, 2)

Two questions: How does the y-value of -1 work, because when you apply all of
the transforms, -1 = 100 (-1 * -1 = 1 -1 = 0 * (100/2.0) = 0 + 100 = 100). If
you were to attempt to fill a rectangle at location 0,100 with width of 256 and
height of 100 on a bitmap of size (512, 100), nothing would be displayed. Unless
I am misunderstandin g how the transforms work, this call should not work, but it
does.

Why must the height of the rectangle be given as 2 (i.e. 1 - -1)
or ymax-ymin? If the other coordinates and measurements can be given in their
appropriate units, why must the height be different?

However, if I attempt to create the points and transform them with a
transformation matrix, the location of (0, -1) (i.e. xmin, ymin) does not work.
I must use *ymax* as the y-value of the location.

Is the transformed FillRectangle call (the one with the Cornsilk brush) doing
something strange to the coordinates? Does Matrix.Transfor mPoints call not work
properly?

I apologize that this is such a long post. If you need more info or my source
code, feel free to email me: jwbiagio at sbcglobal dot net.

Regards,
JBiagio

---------------------------------------
Here are the functions that I am using:

Public Function CreateMatrix(By Val W As Single, ByVal H As Single, _
ByVal Xmin As Single, ByVal Xmax As Single, _
ByVal Ymin As Single, ByVal Ymax As Single) _
As Matrix
Dim m As New Matrix()
m.Translate(0, H) '4
m.Scale(W / (Xmax - Xmin), H / (Ymax - Ymin)) '3
m.Translate(-Xmin, Ymin) '1
m.Scale(1, -1) '2
'2134
Dim o(5) As Object
m.Elements.Copy To(o, 0)
Console.WriteLi ne("{0}, {1}, {2}, {3}, {4}, {5}", o)
Return m
End Function

Protected Sub DrawCanvas()

'points range x: 0 - 2PI, y: -1 - 1
Dim g As Graphics = Graphics.FromIm age(canvas)

'move the negative y-coord up one (1)
g.TranslateTran sform(0, -1, MatrixOrder.Pre pend)
g.ScaleTransfor m(1, -1, MatrixOrder.Pre pend) 'flip y coordinate (2)
'append because we want to move before we scale (3)
g.ScaleTransfor m(4096 / (Math.PI * 2.0), 50, MatrixOrder.App end)

'move negative y coord up (i.e. 1 * -1 = -1 + -1 = -2 * (100/2.0) = -100) (4)
g.TranslateTran sform(0, 100, MatrixOrder.App end)
'2134
Dim pt() As PointF = GeneratePoints( )
Dim m As Matrix = CreateMatrix(40 96, 100, 0, 2 * Math.PI, -1, 1)
m.TransformPoin ts(pt)
'g.TransformPoi nts(CoordinateS pace.Page, CoordinateSpace .World, pt)

Dim r As New RectangleF(0, -1, CSng(Math.PI / 8.0), 2)

'Draw rectangle with transformed graphics object
g.FillRectangle (Brushes.Cornsi lk, r)

g.ResetTransfor m()

'set up a location that will be next to the rectangle drawn by the
'transformed graphics object
Dim pt3() As PointF = {New PointF(CSng(Mat h.PI / 8.0), 1)}
m.TransformPoin ts(pt3)

'note that height and width are in page coordinates because the graphics
'object is no longer transformed
g.FillRectangle (Brushes.Indigo , pt3(0).X, pt3(0).Y, 256, 100)

Dim p As New Pen(Color.Black , 1.0)

g.DrawLines(p, pt)

p.Dispose()
g.Dispose()

End Sub

Protected Function GeneratePoints( ) As PointF()
'This function will generate 20,000 points for Cosine(10x) from 0-2pi.
Dim pt(19999) As PointF
Dim i As Integer
Dim d As Double

For i = 0 To 19999
d = (i / 19999.0) * 2 * Math.PI
pt(i) = New PointF(d, Math.Cos(10 * d))
Next

Return pt
End Function

Protected Overrides Sub OnPaint(ByVal e As System.Windows. Forms.PaintEven tArgs)
'This is a cobbled-together test. I need to create a much-larger bitmap and
'then scale some dummy data to it.

'Then the paint method will get a "slice" of the canvas bitmap and draw it
'on the form.

Dim bmp As Bitmap = canvas.Clone(Ne w Rectangle(posit ion * 512, 0, 512, 100), _
Imaging.PixelFo rmat.DontCare)

Dim font As New Font("Arial Bold", 13)

e.Graphics.Draw String(position .ToString, font, Brushes.Blue, 10, 10)

e.Graphics.Draw ImageUnscaled(b mp, 50, 50)
End Sub
Jul 21 '05 #1
2 3378
http://www.vbdotnetheaven.com/
JBiagio wrote:
Hello All,

I am attempting to learn a bit about the GDI+ transforms and charting data and I
feel like I'm getting a handle on how the transforms work. My chart object has a
large "canvas" bitmap that all data is scaled to and rendered on, and I display
a smaller "window" of the canvas that the user can page through left and right.

I've been able to scale my data (for the purposes of this discussion, x values
are 0 - 2PI and y values are -1 to 1; I'm charting Cosine) to the canvas bitmap
using the transforms applied to the graphics object and to a transformation
matrix, which I then useto transform the points and draw them on a graphics
object that has no transforms applied. I used both methods because I wanted to
learn how each works. So far, so good.

Next, I attempted to draw rectangles to highlight particular regions of
interest. This is where I get confused. I first worked with the transformed
graphics object and was able to get the rectangle to draw, but the values that I
had to use for its starting location do not make sense to me. Let me show the
transforms necessary to scale and fit my data to the canvas.

The canvas is 4096 x 100 pixels; the window is 512 x 100 pixels. Data is 0 - 2PI
for x values and -1 to 1 for y values. The transforms are as follows:

o Scale(1, -1) ; flip y coordinates
o Translate(0, -1) ; move ymin to origin
o Scale((4096/2PI), (100/(1 - -1)))
o Translate(0, 100)

This works out to be:

o Scale(1, -1)
o Translate(0, ymin)
o Scale((canvas_w idth/(xmax - xmmin)), (canvas_height/(ymax - ymin)))
o Translate(0, canvas_height)

I believe the first Translate call works because the ymin of -1 is first scaled
by -1 and becomes 1, 1 - 1 = 0 so we are back at the origin.

The crux of this is that I can draw rectangles using the graphics object that
has transforms applied to it, and it works as expected (i.e. if I want to draw a
rectangle that covers half of my window, I say:
FillRectangle(0 , -1, PI/8.0, 2)

Two questions: How does the y-value of -1 work, because when you apply all of
the transforms, -1 = 100 (-1 * -1 = 1 -1 = 0 * (100/2.0) = 0 + 100 = 100). If
you were to attempt to fill a rectangle at location 0,100 with width of 256 and
height of 100 on a bitmap of size (512, 100), nothing would be displayed. Unless
I am misunderstandin g how the transforms work, this call should not work, but it
does.

Why must the height of the rectangle be given as 2 (i.e. 1 - -1)
or ymax-ymin? If the other coordinates and measurements can be given in their
appropriate units, why must the height be different?

However, if I attempt to create the points and transform them with a
transformati on matrix, the location of (0, -1) (i.e. xmin, ymin) does not work.
I must use *ymax* as the y-value of the location.

Is the transformed FillRectangle call (the one with the Cornsilk brush) doing
something strange to the coordinates? Does Matrix.Transfor mPoints call not work
properly?

I apologize that this is such a long post. If you need more info or my source
code, feel free to email me: jwbiagio at sbcglobal dot net.

Regards,
JBiagio

---------------------------------------
Here are the functions that I am using:

Public Function CreateMatrix(By Val W As Single, ByVal H As Single, _
ByVal Xmin As Single, ByVal Xmax As Single, _
ByVal Ymin As Single, ByVal Ymax As Single) _
As Matrix
Dim m As New Matrix()
m.Translate(0, H) '4
m.Scale(W / (Xmax - Xmin), H / (Ymax - Ymin)) '3
m.Translate(-Xmin, Ymin) '1
m.Scale(1, -1) '2
'2134
Dim o(5) As Object
m.Elements.Copy To(o, 0)
Console.WriteLi ne("{0}, {1}, {2}, {3}, {4}, {5}", o)
Return m
End Function

Protected Sub DrawCanvas()

'points range x: 0 - 2PI, y: -1 - 1
Dim g As Graphics = Graphics.FromIm age(canvas)

'move the negative y-coord up one (1)
g.TranslateTran sform(0, -1, MatrixOrder.Pre pend)
g.ScaleTransfor m(1, -1, MatrixOrder.Pre pend) 'flip y coordinate (2)
'append because we want to move before we scale (3)
g.ScaleTransfor m(4096 / (Math.PI * 2.0), 50, MatrixOrder.App end)

'move negative y coord up (i.e. 1 * -1 = -1 + -1 = -2 * (100/2.0) = -100) (4)
g.TranslateTran sform(0, 100, MatrixOrder.App end)
'2134
Dim pt() As PointF = GeneratePoints( )
Dim m As Matrix = CreateMatrix(40 96, 100, 0, 2 * Math.PI, -1, 1)
m.TransformPoin ts(pt)
'g.TransformPoi nts(CoordinateS pace.Page, CoordinateSpace .World, pt)

Dim r As New RectangleF(0, -1, CSng(Math.PI / 8.0), 2)

'Draw rectangle with transformed graphics object
g.FillRectangle (Brushes.Cornsi lk, r)

g.ResetTransfor m()

'set up a location that will be next to the rectangle drawn by the
'transformed graphics object
Dim pt3() As PointF = {New PointF(CSng(Mat h.PI / 8.0), 1)}
m.TransformPoin ts(pt3)

'note that height and width are in page coordinates because the graphics
'object is no longer transformed
g.FillRectangle (Brushes.Indigo , pt3(0).X, pt3(0).Y, 256, 100)

Dim p As New Pen(Color.Black , 1.0)

g.DrawLines(p, pt)

p.Dispose()
g.Dispose()

End Sub

Protected Function GeneratePoints( ) As PointF()
'This function will generate 20,000 points for Cosine(10x) from 0-2pi.
Dim pt(19999) As PointF
Dim i As Integer
Dim d As Double

For i = 0 To 19999
d = (i / 19999.0) * 2 * Math.PI
pt(i) = New PointF(d, Math.Cos(10 * d))
Next

Return pt
End Function

Protected Overrides Sub OnPaint(ByVal e As System.Windows. Forms.PaintEven tArgs)
'This is a cobbled-together test. I need to create a much-larger bitmap and
'then scale some dummy data to it.

'Then the paint method will get a "slice" of the canvas bitmap and draw it
'on the form.

Dim bmp As Bitmap = canvas.Clone(Ne w Rectangle(posit ion * 512, 0, 512, 100), _
Imaging.PixelFo rmat.DontCare)

Dim font As New Font("Arial Bold", 13)

e.Graphics.Draw String(position .ToString, font, Brushes.Blue, 10, 10)

e.Graphics.Draw ImageUnscaled(b mp, 50, 50)
End Sub


Jul 21 '05 #2
JBiagio,

Did you know you have the most change on an answer for your question in the
newsgroup.

microsoft.publi c.dotnet.framew ork.drawing
with probably as second best
microsoft.publi c.dotnet.langua ges.vb

You can also crosspost it to those newsgroups, (sending one message to both
newsgroups at once). However in this case I would first try it with a single
post to the first.

And as advice do make a snippet of your problem. It is very rare in this
newsgroups that questions with so much code are answered.

I hope this helps?

Cor
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3601
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph = new Graphics //SC // first of all there is no constructor for Graphics // so in the Form_Paint I use instead:
2
510
by: JBiagio | last post by:
Hello All, I am attempting to learn a bit about the GDI+ transforms and charting data and I feel like I'm getting a handle on how the transforms work. My chart object has a large "canvas" bitmap that all data is scaled to and rendered on, and I display a smaller "window" of the canvas that the user can page through left and right. I've been able to scale my data (for the purposes of this discussion, x values are 0 - 2PI and y values...
0
1010
by: matko | last post by:
When I call the following function, the rectangle will overlap the graphics that is drawn _afterwards_. Why is that? How can I make sure my graphics is drawn in correct order? (Note: If I comment the FillRectangle-line, the other graphics is drawn correctly.) private void PaintBox(Graphics g, Brush b, Pen p) { // The background rectangle...
2
1526
by: rushikesh.joshi | last post by:
Hi All, I want to create a webcontrol which will generate a bar (bar chart). I have done some graphics code in my ASPX page and it's working fine, but how do i create a Custom Control or User Control for same (custom control is prefarable). Below is my code of ASPX page which is working fine.... Here i have used Response, to generate the bar but how do i use it in the webcontrols...
1
2349
by: rushikesh.joshi | last post by:
Hi All, I want some charting functionality in my ASP.NET application. I want to show a multiple bar on my web page. It's based on down time of different servers. like server1: down betn 4 AM to 5 AM and 6 PM to 7 PM server2: down betn 7 AM to 7:30 AM server3: down betn 3 AM to 5 AM and 2 Pm to 3 PM
2
7227
by: Mark Ingram | last post by:
Hi, I would like to know which is the faster method to call, FillRectangle or FillPath. I have a path which is essentially a square, except for rounded corners, so I can end up with 2 almost identical lines of code for the fill, but want to know which is "more correct". LinearGradientBrush backColorBrush = new
1
1406
by: gopal28 | last post by:
HI I am using the Graphics to paint to a a picture box but if i minimise my screen the picture disappears is there anyway i can fix this?? Please Help!! here is my code: PLease Help me it will be deeply appreciated!! Dim g As Graphics = Me.PictureBox3.CreateGraphics() g.Clear(Me.PictureBox3.BackColor) Dim curImage As Image = Drawing.Image.FromFile(Application.StartupPath + "\Resources\South1.png") ...
1
1340
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Email Question: When I send an email (basically a sms to a phone) from ms outlook it works fine. When I try to send it pragmatically I get an error stating that it can’t relay for <the email address> Try If Not strMailSv = "" Then If Not msgTo = "" Then
0
1221
by: =?Utf-8?B?QW5kcmV3?= | last post by:
I have a borderless form that has a transparent background. I only wanted the color that I painted the background with to be transparent and not the controls on the form or the box I draw in the middle of the form. The problem I am having is when I pass another window in front of it or drag the window, it doesn’t repaint with the transparent background. It just shows the drag marks of the window that I dragged in front of it. The other...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.