So
I need to put an image on a picturebox
I have a form with a panel which contain a picturebox and an image.
I use image because I need to rotate it.
what I get is that the picture cover the image and I need the opposite.
picturebox.sendToBack doesnt work and i cant find img.bringToTop function.
Imports System.Drawing.Drawing2D
Public Class Form1
Dim g As Graphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
g = Panel1.CreateGraphics
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As Image
img = Bitmap.FromFile(Application.StartupPath & "/pic/" & "clay.jpg")
g.RotateTransform(330)
g.DrawImage(img, 70, 170, 100, 100)
Pb.SendToBack()
End Sub
End Class
[ pictures][1]
https://i.stack.imgur.com/s9goC.jpg
Here is an example of what I linked to in the comments that applies to your specific scenario. This code will draw the same Image on a Panel and a PictureBox. It will work whether the PictureBox is a child of the Panel or a sibling.
Private ReadOnly img As Image = Image.FromFile("file path here")
Private Sub DrawImage(c As Control, g As Graphics)
'The image will be drawn at (100,100) in form coordinates.
'Translate that location to coordinates for the current control.
Dim imgLocation = c.PointToClient(Me.PointToScreen(New Point(100, 100)))
g.DrawImage(img, imgLocation)
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
DrawImage(Panel1, e.Graphics)
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
DrawImage(PictureBox1, e.Graphics)
End Sub
In that code, there is an individual Paint event handler for each control and they both call a common method that does the drawing. If you want to, you can bundle it all into a single common event handler:
Private ReadOnly img As Image = Image.FromFile("file path here")
Private Sub Controls_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint,
PictureBox1.Paint
'The image will be drawn at (100,100) in form coordinates.
'Translate that location to coordinates for the current control.
Dim imgLocation = DirectCast(sender, Control).PointToClient(Me.PointToScreen(New Point(100, 100)))
e.Graphics.DrawImage(img, imgLocation)
End Sub
Related
I have a simple form on which I want to draw an Icon file from disk.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim g As Graphics = Me.CreateGraphics()
'Dim theIcon As Icon = LoadIconFromFile("c:\Users\blabla\pmt.ico")
Dim theIcon As Icon = New Icon("c:\Users\blabla\pmt.ico",New Size(64, 64))
g.DrawIcon(theIcon, 20, 20)
g.Dispose()
theIcon.Dispose()
End Sub
But the Form is loaded empty of any image on it.
Do you know what am I doing wrong?
Thank you!
I have two textbox controls in a vb form.- I can draw rectangles about the two textbox if they aren't in a groupbox.- Only when the textbox got the focus the rectangle is drawed.- The code works, but somehow, when i put the textbox in a groupbox the rectangles aren't drawed.-
the rectangle only is drawed when textbox is focused
This is the code that i have been using.-
Public Class Form1
Dim curControl As TextBox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
If Not curControl Is Nothing Then
If curControl.Name = "TextBox1" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Lime, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox1.Location, TextBox1.Size))
pen.Dispose()
End If
End If
If Not curControl Is Nothing Then
If curControl.Name = "TextBox2" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Red, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox2.Location, TextBox2.Size))
pen.Dispose()
End If
End If
End Sub
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
curControl = DirectCast(sender, TextBox)
Me.Invalidate()
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave
curControl = Nothing
Me.Invalidate()
End Sub
I have a project where I want to put controls on the form's border, when the formborderstyle is set to formborderstyle.sizable
Formborderstyle.none will not work, since it can't be sized on runtime, and goes in front of the taskbar when maximized.
I'm using vb.net 2010.
I'm not sure if you can override the drawing of the border and if you could not sure how you would add control to the border.
You can temporarily change the border style before you maximize the form.
And you can overload the client event to handle re-sizing the form yourself.
Are there any other reasons you dont want to go with Formborderstyle.none?
Public Class Form1
Inherits Windows.Forms.Form
Private Const BorderWidth As Integer = 30 'This is just for demo purposes.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Panel1.Location = New Point(Me.Width - BorderWidth, 0)
Panel1.Width = BorderWidth
End Sub
Public xLocation, yLocation As Integer
Private Sub Panel1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
xLocation = PointToScreen(Cursor.Position).X
yLocation = PointToScreen(Cursor.Position).Y
End Sub
Private Sub Panel1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
'Stop resizing form
Me.Width = Me.Width + (PointToScreen(Cursor.Position).X - xLocation)
xLocation = PointToScreen(Cursor.Position).X
yLocation = PointToScreen(Cursor.Position).Y
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End Sub
End Class
Create a new form with three picture boxes. This code is intended to draw a border when the mouse enters the picture box and remove it when it leaves. It is inconsistent in the results. Sometimes it draws/removes the border, sometimes it doesn't. This code is not complex. Using VS 2012.
Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.FixedSingle
' Debug.WriteLine("E " & pb.Name)
End Sub
Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.None
' Debug.WriteLine("X " & pb.Name)
End Sub
I could also reproduce the issue. So, expanding on the comments above about "drawing something else" instead of using the Picturebox's property, let me suggest this quick and dirty approach:
Use a RectangleShape object, the one provided by the VisualBasic Powerpack 3.0 addon. Simply put one of those in the same form your PictureBox is in, and make it invisible (visible = false).
The code is also easy:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1)
Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1)
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
Me.RectangleShape1.Visible = True
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
Me.RectangleShape1.Visible = False
End Sub
End Class
Need some help from your Form MouseEnter Event ..
Dim pb As PictureBox = New PictureBox
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
pb.BorderStyle = BorderStyle.None
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
pb = PictureBox1
pb.BorderStyle = BorderStyle.FixedSingle
End Sub
I followed KalaNag idea by putting my picturebox inside a panel and handled the event on the pciturebox by doing like this
private void PictureBox_MouseEnter(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 20;
(control.Parent as Panel).Height = 20;
(control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D;
}
private void PictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 18;
(control.Parent as Panel).Height = 18;
(control.Parent as Panel).BorderStyle = BorderStyle.None;
}
I changed the control's size because otherwise, the picturebox keeps flickering when the mouse hovers the borders as the cursor is entering and leaving indefinitely since the borders change the size of the control.
Works like a charm !
I am using Visual Basic.Net and am drawing graphics on the screen.
Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim gr As Graphics = Graphics.FromHwnd(New IntPtr(0))
gr.DrawString("text on screen",
New Font(Me.Font.FontFamily, 25,
FontStyle.Regular), Brushes.Red, 50, 50)
End Sub
In the above code, text is drawn on the screen. My question is this: How can I remove the text that is drawn to screen? I see that there is a .Clear method, however, this 'Clears the entire drawing surface and fills it with the specified background color', rather than just removing the drawn text.
Thanks in advance.
EDIT
I am wanting to develop a subliminal message application that will show messages on screen while the user is using other applications. Would the transparent form be the best way to do this?
I have found the following code that works:
Private WithEvents TextForm As New Form
Private Zipper As New FontFamily("Zipper")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With TextForm
.BackColor = Color.DimGray
.TransparencyKey = Color.DimGray
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.ShowInTaskbar = False
.WindowState = FormWindowState.Maximized
.Opacity = 0
.Show(Me)
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static showText As Boolean
showText = Not showText
If showText Then TextForm.Opacity = 0.99 Else TextForm.Opacity = 0
End Sub
Private Sub TextForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles TextForm.Paint
e.Graphics.DrawString("text on screen 12345", New Font(Zipper, 30, FontStyle.Bold), Brushes.Red, 50, 50)
End Sub
Take different bitmaps and draw each new thing in separate bitmaps and after merge new bitmap with old one. When you want to remove text reload old bitmap which is without text.
Search for drawing in new bitmaps and save drawing.
You can try this:
Dim Graphics0 as Graphics = Graphics.fromHwnd(0) 'This is the desktop's graphics
Graphics0.DrawText("Test 1..2..3..",New Font(Arial,10),Brushes.Black,New Point(0,0))