How to draw a basic circle on an image

Post

Posted
Rating:
#1 (In Topic #1559)
Regular
DIYTinkerer is in the usergroup ‘Regular’
I want to draw a circle on an image, but I just can't get my head around it. I've looked in the wiki, here on the forum and in the online gambas book but nowhere can I find a simple example of drawing a simple shape on an image.

Things I'm confused about.
<LIST>
  • <LI>should I be using draw or paint - and what's the difference - I think they are the same but paint has superseded draw - although there is no paint event so you still use draw for event handling?</LI>
</LIST>
<LIST>
  • <LI>How the whole drawing/paint system works</LI>
</LIST>


This is as much as I gleaned from various resources online, but I will be honest I really don't understand what this is doing, but it was my best (and failed) attempt at simplifying the examples.

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   Dim exampleImage As Image = Image.Load("exampleImage.jpg")
  7.  
  8.   ImageView1.Update(exampleImage)
  9.   paintCircle()
  10.  
  11.  
  12. Public Sub paintCircle()
  13.   'I think this simply set up the paint parameters and triggers the draw event
  14.   'i.e. I want to draw a circle at (50,50) with a radius of 100 in red ink with a line width of 5 pixels
  15.   Paint.Begin(ImageView1.Image)
  16.   Paint.Brush = Paint.Color(Color.red)
  17.   Paint.LineWidth = 5
  18.   Paint.circle(50, 50, 100)
  19.   Paint.End
  20.  
  21.  
  22.  
  23. Public Sub ImageView1_Draw(View As Image)
  24.   'I think this actually draws what was requested onto the imageView1.Image
  25.   Paint.Begin(ImageView1.image)
  26.   Paint.DrawImage(ImageView1.image, 0, 0)
  27.   paint.End
  28.  

I don't think I'm an idiot, but I'll accept that I am if you say so…
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
use Paint.class , Draw is drepreciated.

Most Paint methods have to be finished with either Paint.Stroke or Paint.Fill

Things that include the word Paint or Draw usually do not like Paint.DrawImage)

Code (gambas)

  1. Public Sub paintCircle()
  2.   'I think this simply set up the paint parameters and triggers the draw event
  3.   'i.e. I want to draw a circle at (50,50) with a radius of 100 in red ink with a line width of 5 pixels
  4.   Paint.Begin(ImageView1.Image)
  5.   Paint.Brush = Paint.Color(Color.red)
  6.   Paint.LineWidth = 5
  7.   Paint.circle(50, 50, 100)
  8.  
  9.   Paint.Stroke  ' use Paint.Fill for a solid circle
  10.  
  11.   Paint.End
  12.    
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
DIYTinkerer is in the usergroup ‘Regular’

BruceSteers said

use Paint.class , Draw is drepreciated.
Thanks I got that right then  :D

BruceSteers said

Most Paint methods have to be finished with either Paint.Stroke or Paint.Fill
OK I added that but it still doesn't work  :(

BruceSteers said

Things that include the word Paint or Draw usually do not like Paint.DrawImage)
:?: Very cryptic, what should I be using? I'm genuinely stumbling in the dark, but perhaps a little elevated that it seems like I'm almost there without knowing what I'm doing :!:
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Remove all this, it is not needed and it is overwtiting your circle with a fresh copy of the image.

Code (gambas)

  1. Public Sub ImageView1_Draw(View As Image)
  2.   'I think this actually draws what was requested onto the imageView1.Image
  3.   Paint.Begin(ImageView1.image)
  4.   Paint.DrawImage(ImageView1.image, 0, 0)
  5.   paint.End
  6.    
  7.  
The Draw event happens every time the Control needs to refresh


I would just load the image directly to the ImageView

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public Sub Form_Open()
  4.    
  5.   ImageView1.Image = Image.Load("exampleImage.jpg")
  6.   paintCircle()
  7.  
  8.  
  9. Public Sub paintCircle()
  10.  
  11.   Paint.Begin(ImageView1.Image)
  12.   Paint.Background = Color.red   ' this is an easier way to set brush color :)
  13.   Paint.LineWidth = 5
  14.   Paint.circle(50, 50, 45)  ' size is radius (from the center to edge)
  15.   Paint.Stroke
  16.   Paint.End
  17.    
  18.  
  19.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

DIYTinkerer said

BruceSteers said

Things that include the word Paint or Draw usually do not like Paint.DrawImage)
:?: Very cryptic, what should I be using? I'm genuinely stumbling in the dark, but perhaps a little elevated that it seems like I'm almost there without knowing what I'm doing :!:

Well not many have it, there is…
Paint.DrawImage      Draw an Image, or part of it.
Paint.DrawPicture      Draw a Picture, or part of it.
Paint.DrawRect      Draw a rectangle frame
Paint.DrawRichText      Draws a piece of rich text.
Paint.DrawRichTextShadow      Draw the shadow of a rich text.
Paint.DrawText      Draws the specified text.
Paint.DrawTextShadow      Draw the shadow of a text.

For example Paint.DrawText will write the text without using Paint.Stroke or Paint.Fill
But Paint.Text will need it.

You'll get used to it and what you should use for your needs as you get more experience :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
DIYTinkerer is in the usergroup ‘Regular’
Thanks BruceSteers this makes more sense to me.

But in my actual program the circle then disappears, so I thought; ahh, perhaps that is what the draw event is for - maintaining the drawn elements, but as soon as I add a draw event the imageview1.image disappears.

Is there a simpletons description of how drawing works, the relationship between the contents of an an imageview and the draw event handler
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
DIYTinkerer is in the usergroup ‘Regular’
Ah I fixed it :!:  :D , I draw to the actual image rather than the ImageView1.Image - I'm assuming that the ImageView1.Image gets refreshed with the content it is pointing to - which wasn't drawn on, so the circle disappears, by drawing to the actual limage that imageview1 points to the refresh retains the circle thanks for your help - and you patience with beginners like myself!
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’

DIYTinkerer said

Ah I fixed it :!:  :D , I draw to the actual image rather than the ImageView1.Image - I'm assuming that the ImageView1.Image gets refreshed with the content it is pointing to - which wasn't drawn on, so the circle disappears, by drawing to the actual limage that imageview1 points to the refresh retains the circle thanks for your help - and you patience with beginners like myself!

Well i do not know if all that is true, I am loading and writing directly to the imageview.Image just fine.

Without seeing all your code i could not guess why the circle gets overwritten again, (I would guess you are still using the ImageView1_Draw event)

but you got it working now so cool

The following code works okay writing directly to the ImageView.image property.

Code (gambas)

  1. Public Sub Form_Open()
  2.    
  3.   ImageView1.Image = Image.Load("~/Pictures/Me.png")
  4.   paintCircle(ImageView1.Image, 50, 50)
  5.  
  6.  
  7. Public Sub paintCircle(hImage As Image, X As Integer, Y As Integer)
  8.  
  9.   Paint.Begin(hImage)
  10.  
  11.   ' use a fancy gradient color :)
  12.   Paint.Brush = Paint.LinearGradient(X - 50, Y - 50, X - 50, Y + 50, [Color.Red, Color.Yellow, Color.Red], [0, 0.5, 1])
  13.  
  14.   Paint.LineWidth = 5
  15.   Paint.circle(X, Y, 45)
  16.   Paint.Stroke
  17.   Paint.End
  18.  
  19.  

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
DIYTinkerer is in the usergroup ‘Regular’
Yea it worked ok with the example code. and its working now in my code and I'm not using the draw event handler, thanks for your help, really appreciate you taking the time to guide me through it.

I've put my source code on github https://github.com/SimonTelescopium/ViewFromAbove if you are interested.

This is genuinely my first gambas project so I expect there are quite a few area that will cause a frown :lol: or if less interested there are some screenshots here https://forum.gambas.one/viewtopic.php?t=2201

Thanks Again.
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
 Happy to help.

ImageView_Draw is not for the novice.
you have to manually draw the image. working out zoom and scroll, etc :=/

PS. a Draw event happens inside the controls Paint.Begin and Paint.End instructions so you should not use Paint.Begin and Paint.End in a Draw event.
Online now: No Back to the top
1 guest and 0 members have just viewed this.