Infinity angles on sharp corners in graphics - vb.net

I am having some trouble with some drawing in an application I'm working on. Whenever i draw a sharp corner with a line wider that 1 pixel my line runs past the point where its supposed to end.
I haven't been able to find anything to help. The drawings are graphics paths, and are polygons and pies mostly. I've double checked my math and that is correct. My code is as follows.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim g As Graphics = CreateGraphics()
g.SmoothingMode = SmoothingMode.AntiAlias
Dim pens As Pen = New Pen(Color.Black, 1)
Dim pen As Pen = New Pen(Color.Red, 1)
g.DrawLine(pens, 350, 0, 350, 800)
g.DrawPie(pen, 100, 0, 500, 500, 0, 90)
g.DrawPie(pen, 100, 300, 500, 500, 0, 10)
pen = New Pen(Color.Red, 2)
g.DrawLine(pens, 700, 0, 700, 800)
g.DrawPie(pen, 450, 0, 500, 500, 0, 90)
g.DrawPie(pen, 450, 300, 500, 500, 0, 10)
End Sub

Try playing with the LineJoin property of the pen and see if that helps you. You could also experiment with the Alignment, EndCap, and MitreLimit properties of the pen.
For example
pen.LineJoin = LineJoin.Bevel
seems to clip off that corner:
As other commentators noted, you need to call Dispose on your Graphics and Pen objects. For the Graphics object, it is best do do all drawing in the OnPaint override of the form. Use a Using statement when create a pen so it will automatically get disposed.

Related

Create Heatmap in vb.net fade between circles

I am trying to create a heatmap in vb.net, what I've done so far is to create gray circles, and map the bitmap with a mapping scheme to a colourized bitmap, as you can see here:
But how am I able to fade between the gray circles? Is there an easy way available?
This is how I am drawing my circles at the moment:
Dim tmpBitmap As New Bitmap(300, 300)
With Graphics.FromImage(tmpBitmap)
'.DrawLine(...)
'.DrawString(...)
Dim pth As New GraphicsPath()
pth.AddEllipse(0, 0, 150, 150)
Dim pgb As New PathGradientBrush(pth)
pgb.SurroundColors = New Color() {Color.DarkGray}
pgb.CenterColor = Color.Black
.FillRectangle(pgb, 0, 0, 150, 150)
Dim pth2 As New GraphicsPath()
pth2.AddEllipse(100, 0, 150, 150)
Dim pgb2 As New PathGradientBrush(pth2)
pgb2.SurroundColors = New Color() {Color.DarkGray}
pgb2.CenterColor = Color.Black
.FillRectangle(pgb2, 100, 0, 150, 150)
End With
Thank you!
Edit:
I need it for an application very similar to what netspot does (www.netspotapp.com) so I need to set points with measurements, and I want it to look like a heatmap. Like this:
Edit2:
Sample with LinearGradientBrush.
If I use LinearGradientBrush (because it's easier for testing)
I set two points:
.DrawEllipse(Pens.Black, 45, 45, 10, 10)
.DrawEllipse(Pens.Black, 145, 145, 10, 10)
After that I create a LinearGradientBrush with the origin of the circles.
Dim tmpBrush As New Drawing2D.LinearGradientBrush(New Point(50, 50), New Point(150, 150), Color.FromArgb(0, 0, 0), Color.FromArgb(100, 100, 100))
.FillRectangle(tmpBrush, 0, 0, 500, 500)
This is the result, but I don't understand how to add a third point for example.

Draw zoomed pixelated image to picturebox

I have an png image 120x120. I want to take a part of it (10x10) and zoom it by a factor of x32 and show it to a picturebox pixelated.
what i have done:
bmp = New Bitmap(320, 320, PixelFormat.Format32bppArgb) 'create a bitmap x32
Dim g As Graphics = Graphics.FromImage(bmp)
'draw the part in that bitmap
g.DrawImage(My.Resources.MyImage, New Rectangle(0, 0, 320, 320), New Rectangle(50, 50, 10, 10), GraphicsUnit.Pixel)
PictureBox1.Image = bmp
g.Dispose()
The image is not pixelated. What can I do to fix it?
You have to specify in your graphics:
g.InterpolationMode = InterpolationMode.NearestNeighbor
and change the rectangles to:
g.DrawImage(My.Resources.MyImage, New RectangleF(0, 0, 320, 320), New RectangleF(49.5, 49.5, 10, 10), GraphicsUnit.Pixel)
so you will not lose half a pixel.

VB.NET - Drawing upscaled graphics bleeds

When I try to upscale a portion of an image in VB.NET, the source rectangle seems to bleed over.
BMP1 = Bitmap.FromFile(Application.StartupPath & "\TST.png")
Dim G As Graphics = PictureBox3.CreateGraphics
G.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
G.DrawImage(BMP1, New Point(0, 0))
G.Dispose()
G = PictureBox1.CreateGraphics
G.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
G.DrawImage(BMP1, New Rectangle(0, 0, 256, 256),
New Rectangle(0, 32, 32, 32), GraphicsUnit.Pixel)
G.Dispose()
G = PictureBox2.CreateGraphics
G.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
G.DrawImage(BMP1, New Rectangle(0, 0, 256, 256),
New Rectangle(0, 32, 31, 32), GraphicsUnit.Pixel)
G.Dispose()
VB.NET Test Image Results:
As you can see, when upscaled to 8x, the image borrows red pixels from the neighboring cell. When I tried to adjust the source box by 1 pixel, the upscaled image seemed to lose two pixels instead of one.
My question is, how can I reproduce the stretching behavior from VB6?
VB6 Test Image Results:
Turns out the answer was to call DrawImage with RectangleFs instead of Rectangles, and then to subtract a magic number of 0.5 from the X and Y value of the source rectangle. This scales the image perfectly.
G.DrawImage(BMP1, New RectangleF(0, 0, 256, 256),
New RectangleF(-0.5, 31.5, 32, 32), GraphicsUnit.Pixel)

Draw Line figure in Windows form with all the sides variable

I know how to draw rectangle, line and ovals on windows form but I am not sure how to draw this type of ........figure........
in windows form. I want all three sides to be variable so I can change its size and also the thickness of line should be variable.
I can draw line like this
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 0))
e.Graphics.DrawLine(pen, 20, 10, 300, 100)
but how do I manage to draw the above shown picture?
Can I group these lines showhow?
Any help will be highly appreciated
Cheers
Mak
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 0))
' Create array of points that define lines to draw.
Dim points As Point() = {New Point(10, 100), New Point(10, 10), New Point(100, 10), New Point(100, 100)}
'Draw lines to screen.
e.Graphics.DrawLines(Pen, points)

Coloring in shapes in Visual Basic.Net?

I have a set of shapes created in visual basic that need to be colored in:
Public Class Form1
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
Dim world As New Pen(Color.Black, 3)
formSurface.DrawRectangle(world, 250, 50, 300, 300) 'world
Dim roof As New Pen(Color.Black, 3)
Dim roof1 As New Point(325, 200)
Dim roof2 As New Point(475, 200)
Dim roof3 As New Point(400, 100)
Dim roof4 As New Point(400, 100)
Dim curvePoints As Point() = {roof1, roof2, roof3, roof4}
formSurface.DrawPolygon(roof, curvePoints) 'triangle roof
Dim body As New Pen(Color.Black, 3)
formSurface.DrawRectangle(body, 325, 200, 150, 150) 'square body
Dim sun As New Pen(Color.Black, 3)
formSurface.DrawEllipse(sun, 450, 75, 50, 50) 'sun
Dim door As New Pen(Color.Black, 3)
formSurface.DrawRectangle(door, 387, 300, 25, 50) 'door
End Sub
End Class
Everything works perfectly; the shapes are all generated where I want them to be. (I'm trying to draw a house) However, how do I color them in? Also, if I do, will the colors overlap (I want that to happen)?
Should I use some function like image.fill? I know that isn't right but I'm looking for something like that.
Thanks!
You want to use the formSurface.FillPolygon, FillRectangle, and FillEllipse commands. You can still use the Draw commands to create an outline with a different color than the fill.
The "Fill" commands need Brushes instead of pens. (Pens draw lines, brushes fill space.) The easiest type of brush to use would be like "Brushes.AliceBlue".