How can I change the hitbox of a PictureBox where the image is circular? By hitbox, I mean where the ClickEvent registers.
You can use the Control.Region property, to create a Region using a circular GraphicsPath. This Region is used for the Click event, as well as the MouseEnter, MouseHover, and MouseLeave events.
Example with a PictureBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim path As New Drawing2D.GraphicsPath()
path.AddArc(0, 0, PictureBox1.Width, PictureBox1.Height, 0, 360) 'A circle at 0,0 relative to the PictureBox, sharing its Width and Height
PictureBox1.Region = New Region(path)
End Sub
If you want a circle in the middle (instead of an oval), then use something like:
Dim path As New Drawing2D.GraphicsPath()
Dim radius As Integer = Math.Min(PictureBox1.Width - 1, PictureBox1.Height - 1) / 2
Dim rc As New Rectangle(New Point(PictureBox1.Width / 2, PictureBox1.Height / 2), New Size(1, 1))
rc.Inflate(radius, radius)
path.AddEllipse(rc)
PictureBox1.Region = New Region(path)
Related
I'm trying to position a form in a specific spot on the screen when opening. I used Form1.Location = New Point(200, 1200) which worked on my screen but on another screen it placed the form outside the viewing area. Is there a way to check the screen size first and position the form with a percent of screen position? Something like this Me.Location = New Point(Height / 2, Width / 2) but not in the center?
Do you mean something like that?
Me.Location = New Point(CInt((My.Computer.Screen.WorkingArea.Width - Me.Width) * 0.5),
CInt((My.Computer.Screen.WorkingArea.Height - Me.Height) * 0.5))
Well it's actually quite simple. There is something built into the Form called System.Windows.Forms.StartPosition. This one line Me.StartPosition = FormStartPosition.CenterScreen must be put in the constructor.
Sub New()
InitializeComponent()
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
If you are doing it manually, perhaps because you don't want to modify the constructor for any reason, here is code to use. I know you have accepted an answer but there is no My namespace in c# so I usually try to avoid it because I frequently switch between the languages. I know it's not a requirement but it's just a recommendation. Using System.Windows.Forms.Screen,
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x = CInt((Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2)
Dim y = CInt((Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2)
Me.Location = New System.Drawing.Point(x, y)
End Sub
I'm trying to create a simple program that will allow you to enter a degree and have a dial fill to that exact degree, and then also display the degree inside of the dial. The project builds without errors, and will display correctly until a value is entered into the textbox, and then I will get the error on the line
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
If I comment out that line it will go down to the line below it. This is my first time using vb.net, so I'm guessing there is something small I'm just doing incorrectly. My full code is:
Public Class Form1
Dim degree As Double
Dim ge As Graphics
Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
ge = e.Graphics
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
Private Sub DrawProgress(g As Graphics, rect As Rectangle, degree As Double)
'work out the angles for each arc
Dim progressAngle = CSng(degree)
Dim remainderAngle = 360 - progressAngle
'create pens to use for the arcs
Using progressPen As New Pen(Color.LightSeaGreen, 2), remainderPen As New Pen(Color.LightGray, 2)
'set the smoothing to high quality for better output
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'draw the blue and white arcs
g.DrawArc(progressPen, rect, -90, progressAngle)
g.DrawArc(remainderPen, rect, progressAngle - 90, remainderAngle)
End Using
'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
Using fnt As New Font(Me.Font.FontFamily, 50)
Dim text As String = degree.ToString
Dim textSize = g.MeasureString(text, fnt)
Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
'now we have all the values draw the text
g.DrawString(text, fnt, Brushes.Black, textPoint)
End Using
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim input_s As String
input_s = TextBox1.Text
If (String.IsNullOrEmpty(input_s)) Then
degree = 0
Else
degree = CDec(input_s)
End If
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
End Class
So I am making a documentation program for a physical therapy environment, and am trying to encorporate the ability to export the form with the data on it to a pdf or image of some sort. My form looks like this:
I have tried using the following code to create an image from it
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
' Take a screenshot of the entire Form1
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot
bmpScreenshot.Save("C:\Student.jpg", ImageFormat.Jpeg)
End Sub
But every time it comes back as only part of the form.
Any help would be appreciated!
You can use this code:
Private Function GetFormImage(ByVal include_borders As Boolean) As Bitmap
' Make the bitmap.
Dim wid As Integer = Me.Width
Dim hgt As Integer = Me.Height
Dim bm As New Bitmap(wid, hgt)
' Draw the form onto the bitmap.
Me.DrawToBitmap(bm, New Rectangle(0, 0, wid, hgt))
' Make a smaller bitmap without borders.
wid = Me.ClientSize.Width
hgt = Me.ClientSize.Height
Dim bm2 As New Bitmap(wid, hgt)
' Get the offset from the window's corner to its client
' area's corner.
Dim pt As New Point(0, 0)
pt = PointToScreen(pt)
Dim dx As Integer = pt.X - Me.Left
Dim dy As Integer = pt.Y - Me.Top
' Copy the part of the original bitmap that we want
' into the bitmap.
Dim gr As Graphics = Graphics.FromImage(bm2)
gr.DrawImage(bm, 0, 0, New Rectangle(dx, dy, wid, hgt), GraphicsUnit.Pixel)
Return bm
End Function
From https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d258c2b-64b9-431f-9df8-398a7866de40/vbnet-save-windows-form-as-an-image-getformimage?forum=vbgeneral
And then call the function as so:
GetFormImage(*True to include the borders*).Save("C:\Student.jpg", ImageFormat.Jpeg)
I am creating a program where the user can command a turtle which moves around on a a white panel named panel 1. I have made the turtle rotate usingrotatefliptype
I am now in the process of making a line follow behind it. I've had a few ideas including placing pixels in places that meet the requirement. My one problem is the location. Is it possible to make the location relative to a certain point?
My current code is:
Sub imageCloner(clonedImage As Image, clonedWidth As Int16, clonedHeight As Int16, clonedLocation As Point)
'clone image
Dim dotImage As New PictureBox()
dotImage.Image = clonedImage
dotImage.Location = clonedLocation
dotImage.Width = clonedWidth
dotImage.Height = clonedHeight
dotImage.SizeMode = picBoxTurtle.SizeMode
panel1.Controls.Add(dotImage)
End Sub
Sub findGradient()
'gradient = rise / run
turtleMovementGradient = (turtleYLocation - turtleOriginalYLocation) / (turtleXLocation - turtleOriginalXLocation)
End Sub
Sub drawLine()
find the gradient
findGradient()
create variables
Dim xcounter As Int16 = 0
Dim ycounter As Int16 = 0
For ycounter = 1 To panel1.Height
For xcounter = 1 To panel1.Width
If ycounter / xcounter = turtleMovementGradient Then
imageCloner(blackDotOnePixel.Image, 1, 1, New Point(panel1.Width - xcounter, panel1.Height - ycounter))
End If
Next
Next
End Sub
The drawLine() subroutine is run first.
I NEED HELP WITH THE DRAWING OF THE LINE
This should give you some idea.
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' Create a local version of the graphics object for the Panel.
Dim g As Graphics = e.Graphics
' Draw a string on the Panel.
g.DrawString("This is a diagonal line drawn on the control", New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
' Draw a line in the Panel.
g.DrawLine(System.Drawing.Pens.Red, Panel11.Left, Panel1.Top, Panel1.Right, Panel1.Bottom)
End Sub
I want a webbrowser control on the background and a picturebox above it where I can draw and then it will appear above the webbrowser control. It's like I am writing on some paper which already has something written on it. I have placed a webbrowser control and a picture box above it both with same dimensions.
I know similar question has been asked a lot of time on this website in different forms but none of the solutions mentioned are working for me.
The solutions mentioned are usually for picturebox over picturebox not picturebox over webbrowser control. simply putting webbrowser control instead of picture does not work. Here is the code I used. The square that should have been formed was not formed.
Public Class Form1
Dim BMP As New Drawing.Bitmap(640, 480)
Dim GFX As Graphics = Graphics.FromImage(BMP)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Controls.Add(WebBrowser1)
PictureBox1.Location = New Point(0, 0)
PictureBox1.BackColor = Color.Transparent
Dim blackPen As New Pen(Color.Black, 3)
Dim point1 As New Point(100, 100)
Dim point2 As New Point(100, 200)
Dim point3 As New Point(200, 200)
Dim point4 As New Point(200, 100)
Dim curvePoints As Point() = {point1, point2, point3, point4}
GFX.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, PictureBox1.Height)
GFX.DrawPolygon(blackPen, curvePoints)
PictureBox1.Image = BMP
End Sub
End Class
It did not work the other way around either, I mean drawing first and than making the picturebox transparent.