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)
Related
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.
I'm working on making a paint-esq image manipulator in VB.Net, and I'm still new to vb. I want the user to be able to upload an image and make adjustments to it, such as adding lines and text. I also want the user to be able to transfer the drawings and text they added to a different baseimage. For example, if the user draws a dog on top of a picture of a park, they can change it so the dog is on a street instead.
I've been messing with the idea of loading the image as the picturebox.backgroundImage, but running into difficulties changing the backgroundImage without reseting the drawings and with croping the image. I've also been dabling in having two pictureboxes with the one on top for drawings, but I'm running into transparency and cropping issues
Here is the code I'm using to establish my picturebox by setting the base image as .backgroundImage
Private Sub LoadImage(thisImage As Image)
'we set the picturebox size according to image, we can get image width and height with the help of Image.Width and Image.height properties.
img.BackgroundImage = thisImage 'c'
img.Image = New Bitmap(thisImage.Width, thisImage.Height) 'c'
img.BorderStyle = BorderStyle.FixedSingle
End Sub
example of the image maniputlation
Private Sub ButtonDone_Click(sender As Object, e As EventArgs) Handles ButtonDone.Click, DoneToolStripMenuItem.Click
Cursor = Cursors.Default
Select Case LCase(stateFlag)
Case "header"
'Reset stuff back to normal
ButtonHeader.Text = "Header"
stateFlag = ""
Cancel_Button.Enabled = False
'set up space to draw on the image
Dim newBm As New Bitmap(img.Image.Width, img.Image.Height)
' First we define a rectangle with the help of already calculated points
Dim newGraphics As Graphics = Graphics.FromImage(newBM) ' create graphics
newGraphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
newGraphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
newGraphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
'set image attributes
newGraphics.DrawImage(img.Image, New Rectangle(0, 0, img.Image.Width + 1, img.Image.Height + 1), _
0, 0, img.Image.Width + 1, img.Image.Height + 1, GraphicsUnit.Pixel)
'Draw Edges for header
newGraphics.DrawLine(Pens.Black, startPoint.X, borderSize - 20, startPoint.X, borderSize - 50)
newGraphics.DrawLine(Pens.Black, endPoint.X, borderSize - 20, endPoint.X, borderSize - 50)
Dim drawFont As New Font("Times New Roman", 12)
Dim drawBrush As New SolidBrush(Color.Black)
Dim stringSize As SizeF = newGraphics.MeasureString(HeaderLabel.Text, drawFont)
' Draw header label inbetween the two edges.
newGraphics.DrawString(HeaderLabel.Text, drawFont, drawBrush, (startPoint.X + endPoint.X) / 2 - (stringSize.Width / 2), borderSize - 45)
img.Image = newBm
PushUndo(img.Image.Clone)
End Sub
I would advise trying the following method to use one picturebox on top of the other, it is a lot simpler than some other methods. In your form load handler, do something like:
pctBackground.BackgroundImage = Bitmap.FromFile("park.jpg")
pctForeground.BackColor = Color.Transparent
pctForeground.Parent = pctBackground
pctForeground.Image = New Bitmap(pctForeground.ClientSize.Width, pctForeground.ClientSize.Height)
Then when you have drawn on the pctForeground, save it like:
pctForeground.Image.Save("dog_in_park.png", System.Drawing.Imaging.ImageFormat.Png)
Is it possible to add an outline, one that continues to expand outwards from the text?
Ie: the outside stroke from Photoshop is what I am looking for
I have found a way to have the outline go inside the text, but it is not what I am looking for.
I've looked around, but I haven't been able to find anyone on google who wanted an outter outline.
Thanks
Current inner outline:
Dim grp As Graphics = e.Graphics
Dim gp As New Drawing2D.GraphicsPath
Dim useFont As Font = New Font("Impact", 60, FontStyle.Regular)
grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gp.AddString("3000", useFont.FontFamily, FontStyle.Regular, 60, New Point(0, 0), StringFormat.GenericTypographic)
useFont.Dispose()
Dim orangeBrush As New SolidBrush(Color.FromArgb(226, 149, 0))
grp.FillPath(orangeBrush, gp)
'This is the stroke below
Dim blackpen As New Pen(Color.Black, 2)
grp.DrawPath(blackpen, gp)
gp.Dispose()
If you reverse the order in which you drawpath and fillpath you can get a outside border for your text.
I have problem with Graphics.RotateTransfrom() with the following code :
Dim newimage As Bitmap
newimage = System.Drawing.Image.FromFile("C:\z.jpg")
Dim gr As Graphics = Graphics.FromImage(newimage)
Dim myFontLabels As New Font("Arial", 10)
Dim myBrushLabels As New SolidBrush(Color.Black)
Dim a As String
'# last 2 number are X and Y coords.
gr.DrawString(MaskedTextBox2.Text * 1000 + 250, myFontLabels, myBrushLabels, 1146, 240)
gr.DrawString(MaskedTextBox2.Text * 1000, myFontLabels, myBrushLabels, 1146, 290)
a = Replace(Label26.Text, "[ mm ]", "")
gr.DrawString(a, myFontLabels, myBrushLabels, 620, 1509)
a = Replace(Label5.Text, "[ mm ]", "")
gr.DrawString(a, myFontLabels, myBrushLabels, 624, 548)
gr.RotateTransform(90.0F)
gr.DrawString(a, myFontLabels, myBrushLabels, 0, 0)
PictureBox1.Image = newimage
I dont know why but my image in pictureBox1 is not rotated. Someone known solution ?
The issue at hand is that the RotateTransform method does not apply to the existing image.
Instead, it applies to the transformation matrix of the graphics object. Basically, the transformation matrix modifies the coordinate system used to add new items.
Try the following :
Dim gfx = Graphics.FromImage(PictureBox1.Image)
gfx.DrawString("Test", Me.Font, Brushes.Red, New PointF(10, 10))
gfx.RotateTransform(45)
gfx.DrawString("Rotate", Me.Font, Brushes.Red, New PointF(10, 10))
The first string is drawn normally, while the second is drawn rotated.
So what you need to do is create a new graphics object, apply your rotation, draw your source image onto the graphics (graphics.DrawImage), and then draw all your text :
' Easy way to create a graphisc object
Dim gfx = Graphics.FromImage(PictureBox1.Image)
gfx.Clear(Color.Black)
gfx.RotateTransform(90) ' Rotate by 90°
gfx.DrawImage(Image.FromFile("whatever.jpg"), New PointF(0, 0))
gfx.DrawString("Test", Me.Font, Brushes.Red, New PointF(10, 10))
gfx.DrawString("Rotate", Me.Font, Brushes.Red, New PointF(10, 10))
But beware of rotation, you'll find that you need to change the coordinates at which you draw your image (Or change the RenderingOrigin property of the graphics, setting it to the center of the image makes it easier to handle rotations), otherwise your picture won't be visible (it will be drawn, but off the visible part of the graphics).
Hope that helps
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".