I am trying to draw two rectangles in a picture box in VB (for school) but it does not seem to work at all
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim paper As Graphics()
paper = PictureBox1.CreateGraphics()
Dim pen As Pen = New Pen(Color.Black)
paper.DrawRectangle(pen, 10, 10, 100, 50)
paper.DrawRectangle(pen, 10, 75, 100, 100)
End Sub
If you don't paint in the Paint event it will not persist.
Private Sub Picturebox1_Paint(ByVal sender As Object, ByVal e As System.PaintEventArgs) Handles Picturebox1.Paint
Using pen As New Pen(Color.Black)
e.Graphics.DrawRectangle(pen, 10, 10, 100, 50)
e.Graphics.DrawRectangle(pen, 10, 75, 100, 100)
End Using
End Sub
Related
Is Possible to get Image in pdf where we save pdf from PrintDocument
Here i added an picturebox , button, printdocument, printpreviewdialog
In printDocument
Code:
Public Class Form2
Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim XPos, YPos As Long
XPos = 300
YPos = 20
Dim MyFont As New Font("Arial", 18)
e.Graphics.DrawString("Stack Overflow is Best", MyFont, Brushes.Black, XPos + 30, YPos)
YPos += 50
YPos += 75
MyFont = New Font("Arial", 12)
e.Graphics.DrawImage(PictureBox2.Image, XPos - 200, YPos + 50, 300, 300)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PrintPreviewDialog2.ShowDialog()
End Sub
End Class
But while i save that file and open it doesn't contain picture.
Maybe you can save the image locally and then print it.
I try to paint a dot on an image
…
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
Using p As New System.Drawing.Pen(Color.Yellow, 4)
Using g As Graphics = PictureBox1.CreateGraphics()
g.DrawEllipse(p, 15, 5, 10, 10)
End Using
End Using
End Sub
The image is painted but not the dot.
When separating the code to 2 steps:
1. Load the image
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
End Sub
2. paint
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using p As New System.Drawing.Pen(Color.Yellow, 4)
Using g As Graphics = PictureBox1.CreateGraphics()
g.DrawEllipse(p, 15, 5, 10, 10)
End Using
End Using
End Sub
This time the dot was painted.
I posted this question at an another site and was told it's a question of timing.
Ok it's a question of timing, but how to solve it?
You need to draw everything including the image. You could:
Create a class level variable of Bitmap type and name it say, Bmp:
Private Bmp as Bitmap
To load a new image:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'dispose the old image if any:
bmp?.Dispose()
'and assing the new one:
bmp = New Bitmap(Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg"))
'and call:
PictureBox1.Invalidate()
End Sub
Now the painting routine:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If bmp IsNot Nothing Then
Dim srcRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim desRect As New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height)
Dim G As Graphics = e.Graphics
G.SmoothingMode = SmoothingMode.AntiAlias
G.Clear(BackColor) 'or: G.Clear(Parent.Backcolor) if you want.
G.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel)
Using pn As New Pen(Color.Yellow, 4)
G.DrawEllipse(pn, 15, 5, 10, 10)
End Using
End If
End Sub
Finally, don't forget to clean up:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
bmp?.Dispose()
End Sub
Good luck.
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
I have written the following code but there is an issue which I am not trying to figure out.. I am trying to draw a simple line on the image in my picture box on mouse click. however, paint method is called but doesnt draw it, instead the line at that point is drawn when I scroll the picture in the picture box.. Pls help
Private Sub picCurrentimage_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCurrentimage.MouseDown
px = e.X
py = e.Y
highlightPic(px, py)
End Sub
Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
haspoint = True
picCurrentimage.Invalidate()
picCurrentimage.Update()
End Sub
Private Sub picCurrentimage_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCurrentimage.Paint
If haspoint Then
Dim g As Graphics = picCurrentimage.CreateGraphics
Dim r As Rectangle = New Rectangle(px, py, 600, 10)
Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1)
Dim b As Brush = New SolidBrush(pen.Color)
g.FillRectangle(b, r)
End If
End Sub
Try to replace your code with the following modifications:
Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
haspoint = True
picCurrentimage.Refresh() 'this do both of your lines in one
End Sub
Private Sub picCurrentimage_Paint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles picCurrentimage.Paint
If haspoint Then
Dim g As Graphics = e.Graphics
Dim r As Rectangle = picCurrentimage.ClientRectangle
Dim b As Brush = New SolidBrush(Color.FromArgb(128, 32, 100, 200))
g.FillRectangle(b, r)
b.Dispose
End If
End Sub
Is there any way to embed a vb.net form to another vb.net form. What I am trying to do is to make Form-A semi-transparent and Form-B as the embedded main form. So that the final application has a semi transparent border around it . Also I don't want to use a MDI form.
Edit: How to make the border of a vb.net form semi-transparent without using MDI form.
I've simplified the code a bit and hooked up most of the events to a single method. There is no longer a Form-A, just a Form-B. Form-B now creates it's own Form-A on the fly without any need to actually make a code file. I moved the border size to a variable so it would be easy to adjust it.
Imports System.Runtime.InteropServices
Public Class InnerForm
Private borderSize As Integer = 10
Private border As Form = New Form()
Private Sub InnerForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
border.FormBorderStyle = Windows.Forms.FormBorderStyle.None
border.ShowInTaskbar = False
border.BackColor = Color.Blue
border.Opacity = 0.5
border.Enabled = False
RefreshBorder()
End Sub
Private Sub DrawRectangle()
Dim p As New Drawing2D.GraphicsPath()
p.StartFigure()
p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
p.AddLine(40, 0, border.Width - 40, 0)
p.AddArc(New Rectangle(border.Width - 40, 0, 40, 40), -90, 90)
p.AddLine(border.Width, 40, border.Width, border.Height - 40)
p.AddArc(New Rectangle(border.Width - 40, border.Height - 40, 40, 40), 0, 90)
p.AddLine(border.Width - 40, border.Height, 40, border.Height)
p.AddArc(New Rectangle(0, border.Height - 40, 40, 40), 90, 90)
p.CloseFigure()
border.Region = New Region(p)
End Sub
Private Sub RefreshBorder()
border.Show()
border.Size = New Size(Me.Width + borderSize * 2, Me.Height + borderSize * 2)
border.Location = New Point(Me.Location.X - borderSize, Me.Location.Y - borderSize)
DrawRectangle()
SetWindowPos(border.Handle, Me.Handle, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATE)
Me.BringToFront()
End Sub
Private Sub frmMAin_Refresh(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.GotFocus, Me.Move, Me.Activated, Me.SizeChanged, MyBase.Shown
'dont show when maximized or minimized, else show it'
If Me.WindowState = FormWindowState.Maximized Or Me.WindowState = FormWindowState.Minimized Then
border.Hide()
Else
RefreshBorder()
End If
End Sub
<DllImport("user32.dll")> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function
Public Const SWP_NOSIZE As Int32 = &H1
Public Const SWP_NOMOVE As Int32 = &H2
Public Const SWP_NOACTIVATE As Int32 = &H10
End Class
Let me know if this works for you.
You should check this out. It is in C# but you could try to get help translating it. Extending Form with Non-Client Area Painting. It is way above my head and since you are new to, it might be pretty tricky.
First of, since you did not specify, I am assuming you are using winforms and not WPf or Asp.net, both of which I have next to no experience with.
I have a method that is just messing with a bunch of winforms property's. It is kinda glitchy on my winxp, box but it works :). Here is the code for the two forms, I have included th property's in code to simplify things.
The Main form
Public Class frmMAin
Dim border As Form = New frmBackground()
Private Sub frmMAin_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
RefreshBorder()
End Sub
Private Sub frmMAin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RefreshBorder()
End Sub
Private Sub RefreshBorder()
border.Show()
border.Size = New Size(Me.Width + 20, Me.Height + 20)
border.Location = New Point(Me.Location.X - 10, Me.Location.Y - 10)
End Sub
Private Sub frmMAin_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
border.Hide()
End Sub
Private Sub frmMAin_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
RefreshBorder()
End Sub
End Class
The Background Form
Public Class frmBackground
Private Sub frmBackground_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.ShowInTaskbar = False
Me.BackColor = Color.Blue
Me.Opacity = 0.5
End Sub
End Class
And it turns out something like this.
This code still has some issues with focus but is mostly functional. Also keep in mind this is not the most "elegant" way to solve this problem, there is most likely a better way to do this with some system dll. You also might try WPF, as I hear you have much more control over look then in winforms.