I want to display a image on picturebox and also i want to show grid on picturebox.So if i zoom the image then i can easily identify the pixel size on picturebox.Can any one help me to do this?For eg.
This normal display of image on picturebox
But i want to display image like this on picturebox
Here's the code to draw grid lines using the Graphics.DrawLine():
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.White) '~~~ color of the lines
Dim x As Integer
Dim y As Integer
Dim intSpacing As Integer = 10 '~~~ spacing between adjacent lines
'~~~ Draw the horizontal lines
x = PictureBox1.Width
For y = 0 To PictureBox1.Height Step intSpacing
g.DrawLine(pn, New Point(0, y), New Point(x, y))
Next
'~~~ Draw the vertical lines
y = PictureBox1.Height
For x = 0 To PictureBox1.Width Step intSpacing
g.DrawLine(pn, New Point(x, 0), New Point(x, y))
Next
End Sub
End Class
To test this, create a new project and add a picturebox (name = PictureBox1). Then select an image for it(you could use the property window to set the image). Then copy-paste the above code and run it. You will see grid lines. We have written the code to draw the gridlines on the paint event of the Picturebox. So, these grids will be redrawn when you set an image on the picturebox at runtime too.
Hope it will give you an idea. BTW, the above was coded and tested using VB.Net.
Wish you good luck...
Related
I am trying to build out an application that has tabs on the left, but I want the text to be horizontal and not vertical. I have seen many forum posts for WPF and C#, but nothing specific to VB.net.
Is there a specific property I can use to have the text change from vertical to horizontal? How do I implement this type of change? I know this seems novice like to be asking, but I feel I have hit a brick wall. Any help would be greatly appreciated.
I was able to find the following on the .Net Resource page for Microsoft.
https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-display-side-aligned-tabs-with-tabcontrol
The only item I now want to be able to do is add in padding to the text so that its not right against the dialog box. If anyone has any ideas, please advise.
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
Dim g As Graphics = e.Graphics
Dim _TextBrush As Brush
' Get the item from the collection.
Dim _TabPage As TabPage = TabControl1.TabPages(e.Index)
' Get the real bounds for the tab rectangle.
Dim _TabBounds As Rectangle = TabControl1.GetTabRect(e.Index)
If (e.State = DrawItemState.Selected) Then
' Draw a different background color, and don't paint a focus rectangle.
_TextBrush = New SolidBrush(Color.Red)
g.FillRectangle(Brushes.Gray, e.Bounds)
Else
_TextBrush = New System.Drawing.SolidBrush(e.ForeColor)
e.DrawBackground()
End If
' Use our own font.
Dim _TabFont As New Font("Arial", 10.0, FontStyle.Bold, GraphicsUnit.Pixel)
' Draw string. Center the text.
Dim _StringFlags As New StringFormat()
_StringFlags.Alignment = StringAlignment.Center
_StringFlags.LineAlignment = StringAlignment.Center
g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))
End Sub
in my program I have a picture box with a specific width x and height y. Then the user can enter an elementsize (e.g. 10) and then the programm will create buttons with the width and height of the elementsize over the picture box. The thing is that these buttons need to be transparent so only their border is visible. That means that through the buttons you can see the content of the picture box.
So this is my code for creating these buttons when I click on my start button:
Imports System.Math
Public Class Form1
Dim x As Double
Dim y As Double
Dim elsize As Integer
Dim numberofbuttons As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = 400
y = 200
elsize = 20
numberofbuttons = Round(x / elsize) * Round(y / elsize)
Dim i As Integer
Dim j As Integer
For j = 1 To Round(y / elsize)
For i = 1 To Round(x / elsize)
Dim Btn As New Button
Btn.Width = elsize
Btn.Height = elsize
Btn.Location = New Point(elsize * (i - 1), elsize * (j - 1))
Btn.BackColor = Color.Transparent
PictureBox1.Controls.Add(Btn)
Next
Next
End Sub
End Class
I use
Btn.BringToFront()
to put these buttons in front of the picture box and I wanted to use
Btn.BackColor = Drawing.Color.Transparent
to make them transparent but this won't work. Has anybody any ideas how to do this? Also I wanted to put these buttons in the coordinate system of the picture box and not of the form1. I thought that this is possible through
Btn.Parent = PictureBox1
but the buttons always use the coordinate system of the form.
Thanks in advance for your help.
This:
Me.Controls.Add(Btn)
is setting the Parent of the Button to be the form. You don't Add to one parent and assign a different parent explicitly. Do one or the other. In short, get rid of that Add call and you should be good to go. Alternatively, get rid of the Parent assignmnent and Add to the Controls collection of the PictureBox instead. I'd suggest the latter.
Either way, DO NOT display the Button until you have configured it, i.e. set the Parent or call Controls.Add last.
EDIT: I tested this code and it worked as expected:
Dim btn As New Button
With btn
.Size = mySize
.Location = myLocation
.BackColor = Color.Transparent
.FlatStyle = FlatStyle.Flat
PictureBox1.Controls.Add(btn)
End With
hey every one I want to make a program that able to draw coordinate graph of Two dimensions of (x-y).when I enter a value in (x) text box and (y) text box and hit draw button it well draw the graph in the blue picture box . I searched in web sit but I found only one method that draw using the mouse and this not what I want .This is the image of the program and and it supposed to draw in white line like this image
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
Static last As Point
If e.Button = Windows.Forms.MouseButtons.Left Then
PictureBox2.CreateGraphics.DrawLine(Pens.White, last.X, last.Y, e.X, e.Y)
End If
last = e.Location
End Sub
this is the code that I found that draw using the mouse
You should use the Graphic class inside the panel's Paint event
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
' Create pen.
Using blackPen As New Pen(Color.Black, 3)
' Create points that define line.
Dim point1 As New Point(100, 100)
Dim point2 As New Point(500, 100)
' Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2)
End Using
End Sub
Then call Panle1.Invalidate() to fire the Paint event
It would be good to have a bit more details of your code, especially how and in what class you store the X and Y coordinate. By the way you draw a line between 2 points so you would need two sets of X and Y coordinate boxes on your form.
After that it is as easy as what you found on the internet using the DrawLine method (https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx), you just needs to fire it from the Click event of your "draw" button.
Hi again Ahmed
Here is a simple sample of a form that would draw a line when button is clicked. Of course it would need more bootstrapping to make sure user only enter integer value for the number of pixels and in its basic form, (0,0) is the top left of the panel but it can easily be converted for a bottom left approach...
and
Class Form1
Private Sub cmdDraw_Click(sender As Object, e As EventArgs) Handles cmdDraw.Click
Dim x1 As Integer = Integer.Parse(txtX1.Text)
Dim y1 As Integer = Integer.Parse(txtY1.Text)
Dim x2 As Integer = Integer.Parse(txtX2.Text)
Dim y2 As Integer = Integer.Parse(txtY2.Text)
pnlMap.CreateGraphics.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
End Sub
End Class
I have a drawing program I am developing in VB. In the mousedownevent while a certain radiobutton is ticked, I want the mousedownevent handler to grab the color of the selected pixel that is clicked in a picturebox with an image. How do I do that? Thanks.
If you create a Bitmap object and assign it to your PictureBox's image you can use the GetPixel method to get a pixel's color from the specified point.
Dim PickedColor As Color
Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PickedColor = DirectCast(PictureBox1.Image, Bitmap).GetPixel(e.X, e.Y)
End Sub
Granted, I had interpolation mode set because this is a pixel editor and needed basic coloring for video game 8 bit design and antialiasing is not what i needed so this fixed it.
Dim COLOR1 as color
Dim x As Integer = 0
Dim y As Integer = 0
Dim Image1 As New Bitmap(picturebox1.Image, picturebox1.Width, picturebox1.Height)
Using g As Graphics = Graphics.FromImage(Image1)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half
g.DrawImage(BackgroundImage1, 0, 0, Image1.Width, Image1.Height)
End Using
For x = 0 To picturebox1.Width
Next
For y = 0 To picturebox1.Height
Next
COLOR1 = New Bitmap(Image1).GetPixel(e.X, e.Y)
ColorDialog2.Color = COLOR1
PictureBox3.BackColor = COLOR1
But if you want this color picker without the interpolation pixel mode, so you can color pick on jpegs and whatnot, just take out the whole using code. Works fine.
Any help would be appreciated. How do I create a colored dot on a panel on where a user clicks his mouse? I can get the coordinates of the mouse click and output it through a message box but I can't draw the dot on the panel where the user clicked. I have these codes tried.
Private Sub createDot(x, y)
MsgBox(x & " " & y)
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Maroon, 20)
myGraphics.DrawRectangle(myPen, x, y, 1, 1)
End Sub
Private Sub Panel1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
missed += 1
lblMissed.Text = missed
Dim x, y As Integer
x = e.X.ToString
y = e.Y.ToString
createDot(x, y)
End Sub
Thanks!
Three things:
1.If you draw on panel you should use Panel1.CreateGraphics not Me.CreateGraphics
2.The width of the pen is to large for a dot. Use 1 instead
3.Do not convert x, y to strings and pass it to createDot
Caution:
As soon as the panel is invalidated (for example you move another window over it) the dot will disappear. The drawing code should be in the Panel1_Paint event` (Scott Chamberlain)
Private Sub createDot(ByVal x As Integer, ByVal y As Integer)
MsgBox(x.ToString & " " & y.ToString)
Dim myGraphics As Graphics = Panel.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Maroon, 1)
myGraphics.DrawRectangle(myPen, x, y, 1, 1)
End Sub
Private Sub Panel1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
missed += 1
lblMissed.Text = missed
createDot(e.x, e.y)
End Sub
If you want to create dot on panel you should change Me.CreateGraphics to Panel1.CreateGraphics
Remove ToString from
x=e.X
y=e.Y