Rectangle Panel Graphic relationship - vb.net

The following code does not work:
Private Sub panelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles panelButton.Click
Dim myBrush As Brush
myBrush = New SolidBrush(Color.Yellow)
fBitmap = New Bitmap(picturePanel.Width, picturePanel.Height)
Dim gg As Graphics = Graphics.FromImage(fBitmap)
gg.Clear(Color.White)
'<<<<<my attempt<<<<<<
Dim rec As Rectangle
rec = New Rectangle(picturePanel.Location.X, picturePanel.Location.Y, picturePanel.Width, picturePanel.Height)
gg.FillRectangle(myBrush, rec)
'<<<<<<<<<<<<<<<<<<<<<
'gg.FillRectangle(myBrush, gg.ClipBounds) '<<actual answer
gg.Dispose()
picturePanel.Refresh()
End Sub
In the Panel's repaint handler I've got this:
Private Sub picturePanel_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles picturePanel.Paint
If fBitmap IsNot Nothing Then
e.Graphics.DrawImage(fBitmap, 0, 0)
End If
End Sub
I've included the recommended code (marked as actual answer) but why doesn't the section marked my attempt turn the panel yellow? - can it be tweaked so that it turns the panel yellow?

rec = New Rectangle(picturePanel.Location.X, picturePanel.Location.Y, _
picturePanel.Width, picturePanel.Height)
That's the wrong rectangle. It is relative from the panel's Parent instead of the panel itself. At best you'll see the rectangle at the far right-bottom corner. Or not at all if it is off the bitmap. Draw relative to the bitmap's upper left corner instead. Fix:
rec = New Rectangle(0, 0, fBitmap.Width, fBitmap.Height)
Do note that you'll no longer see any white since you completely overdrew that. It isn't clear what you meant to do. Perhaps more illustrative is to give it a yellow border:
rec = New Rectangle(0, 0, fBitmap.Width-1, fBitmap.Height-1)
gg.DrawRectangle(Pens.Yellow, rec)
Do favor the Using statement instead of the explicit Dispose() call. Also use it on the brush, it should be disposed as well.

Related

How to remove the white lines surrounding a button appearing when I click it

It works fine until I click it and pop up a file dialog box,and then white lines appears surrounding it.
I don't know how to remove these ugly lines.
The only code is openFileDialog1.ShowDialog().
It's a Button whose FlatStyle is flat and whose BackgroundImage is a PNG image.
After that the white lines appears, and if I click the Form it will disappear.
A simple workaround is to set the Button FlatAppearance.BorderColor to its Parent.BackColor. It will overwrite the focus rectangle. The MouseUp event can be used to set the value, it will be raised before a new Window is opened (the Control.Leave event will never be raised):
Private Sub SomeButton_MouseUp(sender As Object, e As MouseEventArgs) Handles SomeButton.MouseUp
Dim ctl As Button = DirectCast(sender, Button)
ctl.FlatAppearance.BorderColor = ctl.Parent.BackColor
End Sub
Using the Control.Paint event, we can also use the Control.BackColor property to paint the border, both with the ControlPaint class DrawBorder method (simpler than using the ButtonRenderer class):
Private Sub SomeButton_Paint(sender As Object, e As PaintEventArgs) Handles SomeButton.Paint
Dim ctl As Button = DirectCast(sender, Button)
ControlPaint.DrawBorder(e.Graphics, ctl.ClientRectangle, ctl.BackColor, ButtonBorderStyle.Solid)
End Sub
and painting the Control's border ourselves:
(Note that the ClientRectangle size must be shrinked, by 1 pixel, both in the Width and Height dimensions. This is by design).
Private Sub SomeButton_Paint(sender As Object, e As PaintEventArgs) Handles SomeButton.Paint
Dim ctl As Control = DirectCast(sender, Control)
Dim r As Rectangle = ctl.ClientRectangle
Using pen As Pen = New Pen(ctl.BackColor, 1)
e.Graphics.DrawRectangle(pen, r.X, r.Y, r.Width - 1, r.Height - 1)
End Using
End Sub

Find position of mouse relative to control, rather than screen

I have a Picture Box called BGImage. I hope that when the user clicks on this I can capture the position of the mouse relative to BGImage.
I've tried using MousePosition, only to find it gives the mouse location on the screen, not on the PictureBox.
So I also tried using PointToClient:
Dim MousePos As Point = Me.PointToClient(MousePosition)
But this gives me the location {X=1866,Y=55} whereas I actually clicked on the PictureBox at around {X=516,Y=284}.
I think the problem arises because I have full-screened my program and set the position of the PictureBox to be at the centre of the screen (BGImage.Location = New Point((My.Computer.Screen.WorkingArea.Width / 2) - (1008 / 2), ((My.Computer.Screen.WorkingArea.Height / 2) - (567 / 2))))
I should also mention that the size of the PictureBox is 1008 By 567 pixels and my screen resolution is 1366 by 768.
Is there any way I can get the mouse position relative to BGImage's position?
Add a mouse click event to your picture box
Then use the MouseEventArgs to get the mouse position inside the picture box.
This will give you the X and the Y location inside the picture box.
Dim PPoint As Point
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
PPoint = New Point(e.X, e.Y)
MsgBox(Convert.ToString(PPoint))
End Sub
I have before the same problem and just solved with the help of some friends.
Give a look Here mouse position is not correct
Here its the code that give you the correct position of the Mouse Based On A Picture.
Tanks to #Aaron he have give a final solution to this problem.
This will put a red dot on the exact point you click. I wonder how useful setting the cursor position will be though, as they will almost certainly move the mouse after clicking the button (inadvertently or not).
Setting the Cursor position needs to be in Screen coordinates - this converts back to client coordinates for drawing. I don't believe the PointToClient is necessary for the cursor position. In the below code, it is an unnecessary conversion, as you just go back to client coordinates. I left it in to show an example of each conversion, so that you can experiment with them.
Public Class Form1
Private PPoint As Point
Public Sub New()
' This call is required by the designer.
InitializeComponent()
PictureBox1.BackColor = Color.White
PictureBox1.BorderStyle = BorderStyle.Fixed3D
AddHandler PictureBox1.MouseClick, AddressOf PictureBox1_MouseClick
AddHandler Button8.Click, AddressOf Button8_Click
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs)
Dim g As Graphics = PictureBox1.CreateGraphics()
Dim rect As New Rectangle(PictureBox1.PointToClient(PPoint), New Size(1, 1))
g.DrawRectangle(Pens.Red, rect)
End Sub
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
PPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
Label8.Text = PPoint.X.ToString()
Label9.Text = PPoint.Y.ToString()
End Sub
End Class
Instead of using:
Dim MousePos As Point = Me.PointToClient(MousePosition)
You should be using:
Dim MousePos As Point = BGImage.PointToClient(MousePosition)
It will give you mouse position in BGImage coordinates, whereas the first code gives you the mouse position in the Form's coordinates.

Drag a label and make it leave a black trail behind it

I am working on a project and I have a label in a picturebox that I need to drag around and while dragging around have it leave a black trail on the picturebox. I am stumped as to how to make this happen. The label is 15x15 and is moved using the mouse and mousedown, mousemove, and mouseup events. During the mousemove the label needs to draw black wherever the label has covered. Thanks for the help!
You need to override the Form's OnPaint() method and then track the offset of the mouse Location from the point of drag origniation. This is essentially implementing an animated effect, so you will need to fire a redraw timer on the Label's drag start, paint the "tail" at a previous drag coordinate, repeating as your drag op moves the mouse coordinates past a certain threshold or certain speed. This will require some playing around to get the effect just right, but its fairly easy to implement.
Get started here: http://msdn.microsoft.com/en-us/library/3e40ahaz(v=vs.110).aspx
Lets say you have a background image in picturebox. Create a bitmap with picturebox dimensions:
Private pctBoxImage As Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pctBoxImage = New Bitmap(PictureBox1.Width, PictureBox1.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
pctBoxImage = CType(PictureBox1.BackgroundImage, Bitmap)
End Sub
If you dont have a background image just some color:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pctBoxImage = New Bitmap(PictureBox1.Width, PictureBox1.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim objGraphics As Graphics = Graphics.FromImage(pctBoxImage)
objGraphics.Clear(PictureBox1.BackColor)
objGraphics.Dispose()
End Sub
When ever you move the label:
Dim pt as Point
'pt = PictureBox1.PointToClient(Cursor.Position)
pt = Label1.PointToScreen(New Point(0, 0))
pt = PictureBox1.PointToClient(pt)
Dim objGraphics As Graphics = Graphics.FromImage(pctBoxImage)
objGraphics.FillRectangle(Brushes.Black, pt.X, pt.Y, 15, 15)
objGraphics.Dispose()
PictureBox1.BackgroundImage = pctBoxImage
valter

Trying to draw on a Picture Box, but nothing appears

I'm converting a VB6 application to VB.Net that draws on picture boxes. Naturally I read the fine manual and turn up this example here. I therefore produced a little project with a form containing only a picture box and tried the following:-
Private Sub Picture1_paint(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles PictureBox1.Paint
Dim mygraphics As Graphics
mygraphics = PictureBox1.CreateGraphics
Dim pen As New Drawing.Pen(System.Drawing.Color.Red, 1)
mygraphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose
End Sub
just like it says. But on running the application, the box turns up blank. Searching for help turned up a suggestion here that I should use a Frame instead, but the result was the same. I have checked that I'm not drawing in the background colour, and that the function is actually invoked.
What have I overlooked?
Paint handler has invalid type for EventArgs. It should be System.Windows.Forms.PaintEventArgs
Use e.Graphics property to obtain graphics instance.
mygraphics = e.Graphics
Reference Link MSDN - Control.Paint Event
I think e is of PainEventArgs type, with already contains a graphics object in e.Graphics. Use that instead.
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pen As New Pen(Color.Red, 1)
e.Graphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose()
End Sub
End Class

VB.Net Background gradient using LinearGradientBrush and display Image

I try to achieve to paint a form with a gradient backcolor and overlap an image with transparency.
This is possible?
I want using a tile background image with transparent background and paint the background with a custom linear gradient.
I do it!, I want share my solution with you (It's pretty easy):
External help: Tile a Shape with an Image
Private Sub BackgroundGradient(ByRef Control As Object, _
ByVal Color1 As Drawing.Color, _
ByVal Color2 As Drawing.Color)
Dim vLinearGradient As Drawing.Drawing2D.LinearGradientBrush = _
New Drawing.Drawing2D.LinearGradientBrush(New Drawing.Point(Control.Width, Control.Height), _
New Drawing.Point(Control.Width, 0), _
Color1, _
Color2)
Dim vGraphic As Drawing.Graphics = Control.CreateGraphics
' To tile the image background - Using the same image background of the image
Dim vTexture As New Drawing.TextureBrush(Control.BackgroundImage)
vGraphic.FillRectangle(vLinearGradient, Control.DisplayRectangle)
vGraphic.FillRectangle(vTexture, Control.DisplayRectangle)
vGraphic.Dispose() : vGraphic = Nothing : vTexture.Dispose() : vTexture = Nothing
End Sub
Here's how to draw the gradient background. Tiling the image will be slow unless you use the windows API or something.
Imports System.Drawing
Public Class frmBG
Private Sub frmBG_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim p1 As Point = Me.ClientRectangle.Location
Dim p2 As Point = New Point(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
Using brsGradient As New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, Color.Red, Color.Blue)
g.FillRectangle(brsGradient, e.ClipRectangle)
g.DrawImage(My.Resources.demoImage, Me.ClientRectangle.Location)
End Using
End Sub
Private Sub frmBG_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
Me.Invalidate()
End Sub
End Class