vb.net how to Hide rectangle from drawing circle? - vb.net

I'm trying to create a circle but the problem is that the box appears in the background.
Is there any way to resolve this problem?
I tried:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim G As Graphics = e.Graphics
G.SmoothingMode = SmoothingMode.AntiAlias
G.FillEllipse(Pens.Red, 2, 2, Width - 4, Height - 4)
End Sub

Related

Draw rectangle with mouse while maintaining a given ratio

I have what I think is a basic problem that has me scratching my head.
I want to be able to draw a rectangle on my form while constraining it to a given ratio. Similar to how Photoshop's crop tool works.
I can scale images correctly using a ratio, but I am having trouble applying the formula to a 'live' drawn rectangle.
Here is the basic working code to draw said rectangle.
Public Class Form2
Dim mRect As Rectangle
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
mRect = New Rectangle(e.X, e.Y, 0, 0)
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
mRect = New Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top)
Me.Invalidate()
End If
End sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Using pen As New Pen(Color.Red, 3)
e.Graphics.DrawRectangle(pen, mRect)
End Using
End class
The above code works fine to draw a freeform rectangle. I'm just not sure where or how to apply the formula to ensure the drawn rectangle always adheres to a given ratio such as 1.5
Any help would be hugely appreciated. Thanks
Try this ;
Dim mRect As Rectangle
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
mRect = New Rectangle(e.X, e.Y, 0, 0)
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
mRect = New Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top)
'Replace 1.5 with the scale you want to use
Dim hgt As Integer = Convert.ToInt32(mRect.Height/1.5)
Dim wdth As Integer = Convert.ToInt32(mRect.Width/1.5)
mRect.Size = New Size(wdth*1.5, hgt*1.5)
Me.Invalidate()
End If
End sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Using pen As New Pen(Color.Red, 3)
e.Graphics.DrawRectangle(pen, mRect)
End Using
End class

Drawing event draws two object

Program has only this code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = PictureBox2.Image
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.DrawEllipse(New Pen(Color.MediumOrchid, 5), 30, 30, 30, 30)
g.Dispose()
PictureBox1.Refresh()
PictureBox2.Refresh()
End Sub
Before clicking PictureBox1 is emtpty and PictureBox2has a white image.
After clicked PictureBox1 and PictureBox2 both have ellipse.
I think program uses one image for two pictureBox'es.So when I paint they are both painted.I want to set picbox2 white image and picbox1 white image with ellipse.Any solution ?
You have to make a copy of the image, so you will use the same data, but not the same object, and so are safe from changes on the original object.
I am not a vb.net expert, but you may try this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
PictureBox1.Image = New Bitmap(PictureBox2.Image)
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.DrawEllipse(New Pen(Color.MediumOrchid, 5), 30, 30, 30, 30)
g.Dispose()
PictureBox1.Refresh()
PictureBox2.Refresh()
End Sub

Font Size too small when drawstring is called

I am loading and image onto a picturebox and drawing som text.
when i drag the text it looks big but then when i dra it or lock it in place it becomes too small. the picture box sizemode is set to Zoom. so huge images are made small. how can i set proper fontsize and preview while in picturebox?
Sub btnLock is responsible for "locking" Text to image
and
sub Picturebox1_Paint previews
how can i make preview scale down font size or make btnLock scaleup fontSIze
Dim Loc As Point
Dim Pxy As Point
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
txtButton.Text = MouseButtons.Left
If e.Button = MouseButtons.Left Then
Loc = e.Location
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim myFontSize As Integer
Integer.TryParse(ComboBox1.Text, myFontSize)
Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic)
Dim pt As Point = PictureBox1.PointToClient(Control.MousePosition)
e.Graphics.DrawString(txtA.Text, myFont, Brushes.Black, pt.X, pt.Y)
End Sub
Private Sub btnLock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLock.Click
Dim myFontSize As Integer
Integer.TryParse(ComboBox1.Text, myFontSize)
Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic)
Dim g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
' g.DrawString(txtA.Text, myFont, Brushes.Black, Loc.X, Loc.Y)
g.DrawString(txtA.Text, myFont, Brushes.Black, Pxy.X, Pxy.Y)
' Me.PictureBox1.Invalidate()
End Sub
Thank you for your help!-DB35M
I would think you are getting this behaviour as the two Graphics objects have different sizes. You object g is getting its bounds from the source image of your picturebox (without scaling) and your graphics object during PictureBox1 is getting it bounds from the picturebox container. This means they will be different sizes and therefore the myFontSize will look like it has different sizes.
Trying to "scale down" the font size according to the down-scaling of the image would be really hard to accomplish. Instead, solve the problem by changing your SizeMode to Normal and resizing the container to fit the image:
PictureBox1.SizeMode = PictureBoxSizeMode.Normal

Image zoom WITH pixelation

I am zooming image in picture box using this code on trackbar.
Private Sub tbrZoomLevel_Scroll(sender As System.Object, e As System.EventArgs) Handles tbrZoomLevel.Scroll
With pbImage
.SuspendLayout()
.Width = actualSize.Width * tbrZoomLevel.Value
.Height = actualSize.Height * tbrZoomLevel.Value
.ResumeLayout()
End With
End Sub
pbImage is a PictureBox control with sizemode as zoom.
actualSize is original Size of Image in pbImage.
When I zoom in, I get image without pixelation. But I want it to get fully pixelated and show the image as shown in MS Paint on zooming. Any help is appreciated. VB.Net code, C# code or just any algorithm is welcomed.
In your picturebox paint:
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
'Draw the image
End Sub
Edit: Try this
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim srcRect As RectangleF = New Rectangle(0, 0, PictureBox1.Width / 8, PictureBox1.Height / 8)
Dim dstRect As RectangleF = New RectangleF(0, 0, PictureBox1.Width, PictureBox1.Height)
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(pctBoxImage, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub
pctBoxImage is the bitmap which will be 800% zoomed
or
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.ScaleTransform(8, 8)
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(pctBoxImage, 0, 0)
End Sub
0,0 is the coordinates of the left up corner position on the bitmap.
valter

About Drawing a moveable rectangle of an uploaded image in Visual Basic

I've been working on a moveable rectangle to compute the ROI of an Image in VB 2010, Since i'm new to VB, I've been able to create a rectangle, But it doesnot appear on my uploaded image. It appears but not on the image. By my listed code below
1. How do I get the rectangle to display on the image.
2. How do get the rectangle to become moveable through out the image. I will be grateful. thanks.
Private Sub ROIToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ROIToolStripMenuItem.Click
Dim G As Graphics
G = PictureBox1.CreateGraphics
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
G.FillRectangle(Brushes.Silver, ClientRectangle)
Dim P As Point
Dim Box As Rectangle
P.X = 1
P.Y = 1
Dim S As Size
S.Width = 100
S.Height = 20
Box = New Rectangle(P, S)
G.DrawRectangle(Pens.Red, Box)
Try the below code
Dim G As Graphics
Dim uploadedBmp1, backgroundBmp1 As Bitmap
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
uploadedBmp1 = New Bitmap("C:\Windows\winnt.bmp")
backgroundBmp1 = New Bitmap(uploadedBmp1.Width, uploadedBmp1.Height)
G = Graphics.FromImage(backgroundBmp1)
PictureBox1.Image = backgroundBmp1
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DrawRectangleOnImage(New Point(0, 0), New Size(100, 50))
End Sub
Private Sub DrawRectangleOnImage(ByVal point1 As Point, ByVal size1 As Size)
G.Clear(Color.White)
G.DrawImage(uploadedBmp1, 0, 0, uploadedBmp1.Width, uploadedBmp1.Height)
Dim rectangle1 As New Rectangle(point1, size1)
G.DrawRectangle(Pens.Red, rectangle1)
PictureBox1.Invalidate()
End Sub
to make a moving rectangle, just call the DrawRectangleOnImage sub with the parameters that you want. It will automatically clear the previous snapshot and draw everything again.