I'm a begginer using VB.Net framework 4.7.2 Winforms.
Im trying to rotate an image of a plane in my application , but it shows the rotated one and the original.
left picture is not rotated and the right is rotated at -25°
Private Sub Rotation(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(dgr)
.DrawImage(PictureBox1.Image, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
End With
End Sub
This is the code I rotate the image with
I want just the rotated image shown.
Thanks in advance.
To elaborate, if you assign an Image object to the Image property of the PictureBox then the control will draw that Image itself every time. If you then draw that same Image object yourself in the Paint event handler then of course you see two images.
Basically, don't assign anything to the Image property of the control but rather to your own Image field and use that in the Paint event handler:
Private myImage As Image
Private Sub Rotation(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(dgr)
.DrawImage(myImage, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
End With
End Sub
You would set myImage in code wherever you're currently setting PictureBox1.Image or in the Load event handler if you're currently setting it in the designer.
Related
I'm trying to get familiar with graphics in vb.net and want to draw just a straight line going from the top left to the bottom right of my form Form1 without using the Form1_Paint method but by using Button1
However, the line is only drawn about a quarter of the way and stops as shown in the picture:
I think I need to increase the Clip of the Graphics Object z and I tried the following but it doesn't work. I also tried decreasing the Clip size and that worked as expected.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
z.Clip.MakeInfinite()
z.DrawLine(pen1, 0, 0, Me.Size.Width, Me.Size.Height)
End Sub
pen1 and z are initialized at the start of the program as follows:
Dim z As Graphics = CreateGraphics()
Dim pen1 = New Pen(Color.Black, 3)
I also tried using different set values for the x2 and y2 but nothing managed to be drawn outside of the box apart from using Form1_paint.
It has nothing to do with the clip, and everything to do with WHEN you called CreateGraphics(). At the point that the graphics was created, the form was not fully initialized and it is using the incorrect size.
When you use CreateGraphics() or create a Pen, you should explicitly Dispose() of them as soon as you're done. This is best accomplished via a Using block:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using z As Graphics = CreateGraphics()
Using pen1 = New Pen(Color.Black, 3)
z.DrawLine(pen1, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
End Using
End Using
End Sub
In general, though, using CreateGraphics() in this manner is almost always the INCORRECT approach. The line drawn this way is TEMPORARY. Minimize the form and restore it and the line will be gone.
I'm doing a project for my kitten who died...
I would like to know how I make it flip (look at the right side) when it comes to the left side of the screen, I'll leave an example in the print
Here's some code you can use to start with a single image file and display it in its original form or flipped horizontally depending on the horizontal position of its PictureBox on the screen.
'The Image to display on the left side of the screen.
Private leftImage As Image
'The Image to display on the right side of the screen.
Private rightImage As Image
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Assume that the original image is the one to display on the left.
leftImage = Image.FromFile("file path here")
'Create a mirror image to display on the right.
rightImage = leftImage.Clone()
rightImage.RotateFlip(RotateFlipType.RotateNoneFlipX)
End Sub
Private Function IsPictureBoxOnLeftSideOfScreen() As Boolean
Dim screenMiddle = Screen.PrimaryScreen.WorkingArea.Width \ 2
Dim pictureBoxMiddle = PictureBox1.PointToScreen(Point.Empty).X + PictureBox1.Width \ 2
Return screenMiddle > pictureBoxMiddle
End Function
Private Sub SetImage()
PictureBox1.Image = If(IsPictureBoxOnLeftSideOfScreen(), leftImage, rightImage)
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
rightImage.Dispose()
leftImage.Dispose()
End Sub
Where you call that SetImage method depends on exactly how you're moving the PictureBox, which you never showed us. If you're moving the form then you might do so in the LocationChanged event handler of the form. If you're moving the PictureBox then you might do so in the LocationChanged event handler of the PictureBox.
I have an image (800x800p) loaded in a PictureBox. It's slightly interactive, clicking somewhere on it will show tooltips or popup windows.
Occasionally, clicking would result in adding of 100x100 px image on the location where it was clicked. That modification needs to be saved onto the main image.
Any ideas as to how to do this?
Draw the new image over your original image.
For example:
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim NewIMG As New Bitmap(100, 100)
Graphics.FromImage(NewIMG).Clear(Color.Black) 'just to make my image black since I don't have any image
'You drawing new image here
Graphics.FromImage(PictureBox1.Image).DrawImage(NewIMG, New Point(e.X - 50, e.Y - 50)) 'mouse position - half new image size so that new image center is placed at cursor location
PictureBox1.Refresh() 'to make picbox redraw and display new image
End Sub
End Class
I am tring to create a custom button width rounded corner and a white outline which follows its shape. On the OnPaint event I've added the following code.
Public Class RoundedButton
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
Dim grPath As GraphicsPath = New GraphicsPath(FillMode.Winding)
grPath.AddArc(0, 0, ClientSize.Height, ClientSize.Height, 90, 180)
grPath.AddLine(grPath.GetLastPoint, New Point(ClientSize.Width - grPath.GetLastPoint.X * 2, 0))
grPath.AddArc(New RectangleF(grPath.GetLastPoint, New Size(ClientSize.Height, ClientSize.Height)), 270, 180)
grPath.CloseFigure()
Me.Region = New Region(grPath)
Dim mypen As New Pen(Color.White, 2)
mypen.Alignment = PenAlignment.Inset
e.Graphics.DrawPath(mypen, grPath)
End Sub
End Class
If I try to use it in a form, it works only if Backcolor property is set to Transparent. If not I can't see the pen path.
I would like to change the backcolor without loose the rounded white border of the pen.
I would like to obtain something like this:
With all projects that you want custom buttons, it is usually easiest to curve a PictureBox with a picture of your button, then using your coding language (VB.NET, C#, C++) to add an on click function to make the action.
Here is a mini example using VB.NET:
Private Sub pictureBox1_Click(sender As Object, e As EventArgs) Handles pictureBox1.Click
Process.start("http://stackoverflow.com")
End Sub
Hope I helped,
Matt
I want an image to be inserted when I clicked on specific location on picturebox. the image shows when I put coordinates in number form in the code below.
g.DrawImage(Image.FromFile("myImage.gif"), New Point(10, 10))
But what I want is that the coordinates is retrieved when I click on a specific location and put it on a variable. But the image is not displaying with this code, I just replaced the number 10,10 with a variable.
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
myX = e.X
myY = e.Y
g.DrawImage(Image.FromFile("myImage.gif"),New Point(myX, myY))
End Sub
Why is it not working ? Any idea sirs.
You need coordinates relative to the form. e.X, e.Y And Cursor.Position, they all supply global coordinates. Thus, set the relative coordinates using:
g.DrawImage(Image.FromFile("myImage.gif"), Me.PointToClient(Cursor.Position))