graphics.DrawIcon() does not draw on form - vb.net

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!

Related

Moving a bitmap to the front

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

VB.NET paint application

I'm facing two problems in my application:
The Undo Function
The Drawing Part
When i draw on the picturebox , it draws very well, when - let's say I want to undo an action, it undo's it but when I click back on the picturebox it reacts like a redo function ,all the drawings appear back on the image.
the second problem is : i want to be able to edit a picture so i load a image by clicking on a listview item but due to something i'm missing the image it is not show but instead it shows a white background in which i am able to draw.
bellow is the code i am using
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Public drawgraph, g As Graphics
Private redoBuffer As New Stack(Of Image)()
Private undoBuffer As New Stack(Of Image)()
Dim color As Color
Dim UndoStack As New Stack(Of Bitmap)()
Dim xStart, yStart, xEnd, yEnd As Integer
Public Drawbitmap As Bitmap
Dim Drawgraphics As Graphics
Dim myPen As New Pen(color.Black, 4)
Dim myColor As Color = color.Black
Dim myPenWidth As Integer
Dim myBGColor As Color = color.White
Dim Drawing As Boolean
Private Sub drawMyline()
PictureBox4.Image = Drawbitmap
Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Drawgraphics.DrawLine(myPen, xStart, yStart, xEnd, yEnd)
End Sub
Private Sub PushUndo(ByVal b As Bitmap)
UndoStack.Push(b)
End Sub
Private Function PopUndo() As Bitmap
If UndoStack.Count = 0 Then
Return Nothing
Exit Function
End If
If UndoStack.Count > 0 Then
Return UndoStack.Pop
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Drawbitmap = New Bitmap(PictureBox4.Width, PictureBox4.Height)
Drawgraphics = Graphics.FromImage(Drawbitmap)
PictureBox4.Image = Drawbitmap
Drawgraphics.Clear(color.White)
myPenWidth = NumericUpDown1.Value
xStart = -1
yStart = -1
Drawing = False
End Sub
Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
Dim bmp As Bitmap
bmp = PopUndo()
If bmp IsNot Nothing Then
PictureBox4.Image = bmp
End If
End Sub
Private Sub PictureBox4_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseDown
Drawing = True
PushUndo(PictureBox4.Image.Clone)
End Sub
Private Sub PictureBox4_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseUp
Drawing = False
End Sub
Private Sub PictureBox4_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseMove
If Drawing Then
xStart = e.X
yStart = e.Y
drawMyline()
End If
xEnd = e.X
yEnd = e.Y
End Sub
End Class
I tried making changes but i can't load the image i want into the picturebox4 and draw on it , it always loads a white background as for the undo function it works until a click again on picturebox4 and all the undone drawings appear back. Can someone help me fix this 2 problems that I have?

VB .NET WinFormApplication Printing Failed Landscape mode after setting

I am using VB .NET programming and I would like to print my WinFormsApplication in landscape mode as the portrait mode could not fit it properly.
I have set the landscape mode as true. You may refer to the code below:
Private Sub PrintAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles PrintAll.Click
PrintForm1.Form = Me
PrintDocument1.DefaultPageSettings.Landscape = True
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.FullWindow)
PrintDialog1.ShowDialog()
End Sub
And the result is shown as below in the screenshot
http://s335.photobucket.com/user/blakeex/media/notcomplete.png.html
Could anyone share some hint or guides?
to print the complete client area of a scrollable form, even if the form has been resized.
Try PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
Here's another way that will print whatever part of the form is viewable on the screen:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
CaptureScreen()
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)
PrintDocument1.Print()
End Sub
Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
End Sub
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim pagerec As New RectangleF(e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y, e.PageSettings.PrintableArea.Height, e.PageSettings.PrintableArea.Width)
e.Graphics.DrawImage(memoryImage, pagerec, New Rectangle(Me.Location, Me.Size), GraphicsUnit.Pixel)
End Sub

About Drawing a moveable rectangle of an uploaded image in Visual Basic

I've been working on a moveable rectangle to compute the ROI of an Image in VB 2010, Since i'm new to VB, I've been able to create a rectangle, But it doesnot appear on my uploaded image. It appears but not on the image. By my listed code below
1. How do I get the rectangle to display on the image.
2. How do get the rectangle to become moveable through out the image. I will be grateful. thanks.
Private Sub ROIToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ROIToolStripMenuItem.Click
Dim G As Graphics
G = PictureBox1.CreateGraphics
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
G.FillRectangle(Brushes.Silver, ClientRectangle)
Dim P As Point
Dim Box As Rectangle
P.X = 1
P.Y = 1
Dim S As Size
S.Width = 100
S.Height = 20
Box = New Rectangle(P, S)
G.DrawRectangle(Pens.Red, Box)
Try the below code
Dim G As Graphics
Dim uploadedBmp1, backgroundBmp1 As Bitmap
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
uploadedBmp1 = New Bitmap("C:\Windows\winnt.bmp")
backgroundBmp1 = New Bitmap(uploadedBmp1.Width, uploadedBmp1.Height)
G = Graphics.FromImage(backgroundBmp1)
PictureBox1.Image = backgroundBmp1
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DrawRectangleOnImage(New Point(0, 0), New Size(100, 50))
End Sub
Private Sub DrawRectangleOnImage(ByVal point1 As Point, ByVal size1 As Size)
G.Clear(Color.White)
G.DrawImage(uploadedBmp1, 0, 0, uploadedBmp1.Width, uploadedBmp1.Height)
Dim rectangle1 As New Rectangle(point1, size1)
G.DrawRectangle(Pens.Red, rectangle1)
PictureBox1.Invalidate()
End Sub
to make a moving rectangle, just call the DrawRectangleOnImage sub with the parameters that you want. It will automatically clear the previous snapshot and draw everything again.

Draw graphics on screen

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))