Coloring in shapes in Visual Basic.Net? - vb.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".

Related

VB Net Graphics DrawString construct Fail

I have NO idea what is wrong with my construct in the Second Graphics.DrawString line of code
My Question is Why is the construction of that line of code FAILING and the line above it does Not Fail
This is the FAILING line of code
e.Graphics.DrawString(strTo, 12, Brushes.Red, 45, 50)
Private Sub pdDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles pdDoc.PrintPage
Dim fontSize As Integer = 30
Dim labelFont As Font = New Font("Times New Roman", fontSize, FontStyle.Bold)
Dim strTo As String = "Kitchen"
Dim lblArray(2) As String
lblArray(0) = "Kitchen"
e.Graphics.DrawString(lblArray(0), labelFont, Brushes.Black, 500, 950)
e.Graphics.DrawString(strTo, 12, Brushes.Red, 45, 50)
After reading the two answers and Here is my updated FIX for the error's in the line of code above
Dim labelFont As Font = New Font("Times New Roman", fontSize, FontStyle.Bold)
Dim strTo As Object = "Kitchen"
e.Graphics.DrawString(strTo.ToString, labelFont, Brushes.Red, 300, 300)
I've taken the liberty of aligning your commas between the working and not:
e.Graphics.DrawString(lblArray(0), labelFont, Brushes.Black, 500, 950)
e.Graphics.DrawString(strTo , 12 , Brushes.Red , 45 , 50 )
^^^^^^^^^
What kind of Font is 12 ?
The first DrawString is OK, but the second one is incorrect.
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y, System.Drawing.StringFormat format);
#CaiusJard has already identified the error, so I am clarifying more.
Here it accepts a Font object, not Integer. By 12 I think you have confused with size. First you can declare a font object with:
Dim myFont As Font = New Font("FONT NAME",12)
Then you can use myFont instead of 12.

Why is the circle I drew larger than expected?

I drew an ellipse/circle with Graphics. It is the right size when I am in the WYSIWYG editor. However, when I run the program, the circle is larger.
correct sized circle in editor
too large sized circle
The "too large sized circle" is also of lower resolution and has jagged edges.
How do I make it the same size?
Protected Overrides Sub OnPaint(
ByVal pEvent _
As PaintEventArgs)
Me.Size = New Size(heightWidth, heightWidth)
'MyBase.OnPaint(pEvent)
Dim CenterCircle _
As New Rectangle(0, 0, heightWidth, heightWidth)
Dim colorBigCircle As Color = Color.FromArgb(218, 227, 243)
Dim colorSmallCircle As Color = Color.FromArgb(67, 99, 155)
pEvent.Graphics.FillEllipse(
New SolidBrush(
colorBigCircle
),
CenterCircle)
pEvent.Graphics.DrawEllipse(
New Pen(
colorSmallCircle, 3
),
CenterCircle)
The "too large sized circle" is also of lower resolution and has jagged edges.
pEvent.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Setting the size of the form in the paint event is a big no-no. Either set the form's dimensions in the designer with a form border that isn't sizable, or set the form's MinimumSize and MaximumSize to the same dimension.
You should dispose of your drawing objects. Setting the SmoothingMode will fix the jagged edges of the circle. Your paint code should look something like this:
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
Dim centerCircle As New Rectangle(0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
Dim colorBigCircle As Color = Color.FromArgb(218, 227, 243)
Dim colorSmallCircle As Color = Color.FromArgb(67, 99, 155)
Using br As New SolidBrush(colorBigCircle)
e.Graphics.FillEllipse(br, centerCircle)
End Using
Using p As New Pen(colorSmallCircle, 3) With {.Alignment = PenAlignment.Inset}
e.Graphics.DrawEllipse(p, centerCircle)
End Using
End Sub

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)

Saving a Drawing but not a Full Image in VB.net

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)

VB.NET Possible to add outside outline to text (stroke)

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.