-
Mar 16th, 2021, 07:59 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Why does this not draw an image ?
Hi,
I can't see why this does not draw an image on my form.
I expect it's something basic but I can't see what.
I've checked that 'img' does in fact contain a 'System Drawing Bitmap'.
vb.net Code:
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim Img As Image = New Bitmap(Me.GetType(), "Ring80BW.png")
Private Sub Form1_Load() Handles MyBase.Load
DrawPic()
End Sub
Public Sub DrawPic()
Dim destRect As New Rectangle(20, 20, 200, 200)
Using gr As Graphics = Graphics.FromImage(Img)
gr.DrawImage(Img, destRect)
End Using
End Sub
End Class
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Mar 16th, 2021, 09:21 PM
#2
Re: Why does this not draw an image ?
You aren't drawing anything on the form, so you see nothing on the form. You're just drawing an Image on itself:
Code:
Using gr As Graphics = Graphics.FromImage(Img)
gr.DrawImage(Img, destRect)
End Using
If you want to draw on the form then you should be handling the form's Paint event and using the Graphics object the event handler provides.
-
Mar 16th, 2021, 09:23 PM
#3
Re: Why does this not draw an image ?
Also, that seems a slightly odd way to get an image resource. If this is a resource that you're adding yourself, is there a reason that you're not just using My.Resources.Ring80BW?
-
Mar 16th, 2021, 10:31 PM
#4
Re: Why does this not draw an image ?
Why are you omitting proper events signatures? They’re not optional...
Code:
imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim Img As New Bitmap("Ring80BW.png")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Refresh
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
DrawPic(e.Graphics)
End Sub
Public Sub DrawPic(gr1 As Graphics)
Dim destRect As New Rectangle(20, 20, 200, 200)
Using gr2 As Graphics = Graphics.FromImage(Img)
gr2.DrawRectangle(Pens.Black, destRect)
End Using
gr1.DrawImage(img, Point.Empty)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 16th, 2021, 10:40 PM
#5
Re: Why does this not draw an image ?
Originally Posted by .paul.
Why are you omitting proper events signatures? They’re not optional...
https://www.vbforums.com/showthread....an-observation
...
-
Mar 16th, 2021, 10:47 PM
#6
Re: Why does this not draw an image ?
Originally Posted by OptionBase1
Perhaps he can get away with it... Doesn't mean he should
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 17th, 2021, 05:41 AM
#7
Thread Starter
PowerPoster
Re: Why does this not draw an image ?
Originally Posted by .paul.
Perhaps he can get away with it... Doesn't mean he should
I've done it that way since VS2005 at which time it was recommended by someone in this forum.
Pop
Along with the sunshine there has to be a little rain sometime.
-
Mar 17th, 2021, 06:11 AM
#8
Re: Why does this not draw an image ?
You could draw it in the Paint event, or if you're not going to change it once the form is displayed, you could assign it to the background image of the form.
Code:
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim Img As Image = New Bitmap(Me.GetType(), "Ring80BW.png")
Private Sub Form1_Load() Handles MyBase.Load
DrawPic()
Me.BackgroundImage = img
End Sub
Public Sub DrawPic()
Dim destRect As New Rectangle(20, 20, 200, 200)
Using gr As Graphics = Graphics.FromImage(Img)
gr.DrawImage(Img, destRect)
End Using
End Sub
End Class
You could also change it after the form is shown if you wanted to by updating the img and refreshing the form.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using gr As Graphics = Graphics.FromImage(img)
gr.DrawRectangle(Pens.Red, 40, 40, 320, 320)
End Using
Me.Refresh()
End Sub
Since the form's background image is referencing img, if you update img and refresh the form it refreshes the background from that img without the need of a paint event handler.
Last edited by passel; Mar 17th, 2021 at 06:15 AM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Mar 17th, 2021, 06:13 AM
#9
Thread Starter
PowerPoster
Re: Why does this not draw an image ?
Originally Posted by jmcilhinney
Also, that seems a slightly odd way to get an image resource. If this is a resource that you're adding yourself, is there a reason that you're not just using My.Resources.Ring80BW?
I did it that way (My.Resources.Ring80BW) but I couldn't see it in Watch, so I used the code you're asking about... Then I realised I was looking at the wrong variable, but was too engrossed to change it back.
Along with the sunshine there has to be a little rain sometime.
-
Mar 17th, 2021, 06:14 AM
#10
Thread Starter
PowerPoster
Re: Why does this not draw an image ?
Ok, Thanks guys, that's fixed it.
Poppa
Along with the sunshine there has to be a little rain sometime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|