Autore Topic: Mostrare un'immagine con Paint  (Letto 484 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Mostrare un'immagine con Paint
« il: 24 Novembre 2013, 17:55:45 »
Vi riporto un'altra discussione su come mostrare un'immagine con la Classe Paint, apparsa nella M.L. ufficiale:


" Dear list members,

in my program I seemingly cannot get an image to be painted.

I have a DrawingArea called "fr".
In memory I keep an image named "img".

In my form code, I declare

Codice: gambas [Seleziona]
    public img as image


In the Form_Open handler, I instantiate "img" with
Codice: gambas [Seleziona]
    img = New Image(fr.Width, fr.Height)


In some other code that is then run, I fill the x/y pixels of "img" with integer values
Codice: gambas [Seleziona]
    img[x,y] = < some integer >



Then I issue a "fr.Refresh" command, hoping "fr" would display
Codice: gambas [Seleziona]

Public Sub fr_Draw()
   Paint.Begin(fr)
   fr.Cached = True
   fr.Background = Color.RGB(100, 140, 180)

   Paint.Image(img, 0, 0) ? ? ?
 
   paint.End

End


In the line containing the "? ? ?", I have tried many variations of code
to make "img" appear in the "fr" DrawingArea, all without success.
The background color, however, is displayed correctly.

What do I have to write in place of "? ? ?" to make "img" display?

Thank you.

Regards,
ukimiku
"


" cached must be false

and never set this property in the draw event ... but before... then
if you put the cached property to true, the draw event is not fired.

Fabien Bodard
"


" Hi Fabien,

thank you for your prompt reply. I must be missing something, I guess. It
still does not display the image.

In the Form_open event, I set

  
Codice: gambas [Seleziona]
fr.cached = false


I see the effect when I drag the window with the mouse. It is slow and a bit
jerky as, I guess, the redrawing is taking place quite often.

Still, the image does not appear. I have checked that the pixel values
contain reasonable color components, but not a single pixel of "img" is
being painted.

So far, my dr.Draw() event reads like this:

Codice: gambas [Seleziona]
Public Sub fr_Draw()
  Paint.Begin(fr)
  fr.Background = Color.RGB(100, 140, 180)
  Paint.Image(img, 0, 0)
  paint.End
End


What am I missing? Is "Paint.Image" the right command?

Thank you.

Regards,
ukimiku
"


" Oops. I should have seen this earlier... Your suspicion is justified.
Paint.Image() only creates a brush from the image. I think you can imagine
this brush as a "stamp" of your image. You can then paint and fill a
rectangle or something to see your picture like:


Codice: gambas [Seleziona]
Public Sub fr_Draw()
  Paint.Brush = Paint.Image(img)
  Paint.Rectangle(0, 0, img.W, img.H)
  Paint.Fill()
End


but things get complicated if you want the image to be painted on
coordinates which are non-multiples of img.W and img.H - you may try it out!

The easier and more flexible method of painting a standalone image (not an
image pattern which is what Paint.Image() seems to be there for) is Paint.DrawImage():


Codice: gambas [Seleziona]
Public Sub fr_Draw()
  Paint.DrawImage(img, 0, 0)
End


You can safely vary the coordinates with this method and immediately see the Image.

Regards,
Tobi
"


" Hi Tobias,

thanks for looking into this:

In the fr.Draw() event handler, when I plot my image pixel by pixel, using
this code:

Codice: gambas [Seleziona]

For y = 0 To img.Height - 1
     For x = 0 To img.Width - 1
        Draw.Foreground = img[x, y]
        Draw.Point(x, y)
     Next
  Next

the image appears. But this method is slow.

When I use your

Codice: gambas [Seleziona]
    Paint.DrawImage(img, 0, 0)

the image does not appear. I understand the logic.

Oh, thank you for your explanations on the Brush methods. Nice!

Regards,
ukimiku
"
« Ultima modifica: 24 Novembre 2013, 17:59:38 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Top Fuel

  • Gran Maestro dei Gamberi
  • *****
  • Post: 960
    • Mostra profilo
Re: Mostrare un'immagine con Paint
« Risposta #1 il: 24 Novembre 2013, 22:22:47 »
In the fr.Draw() event handler, when I plot my image pixel by pixel, using
this code:[/color][/i]
Codice: gambas [Seleziona]

For y = 0 To img.Height - 1
     For x = 0 To img.Width - 1
        Draw.Foreground = img[x, y]
        Draw.Point(x, y)
     Next
  Next

the image appears. But this method is slow.

Bah, lento...
Ho provato con una immagine 800x800 riempita di pixel casuali e ha messo più tempo a calcolare i pixel casuali che a ridisegnare la DrawingArea.
E che ho pure una cpu vecchiotta single core... :-\
« Ultima modifica: 14 Aprile 2014, 18:45:25 da Top Fuel »
Dear youtube administrators, your search bar is broken. When I type the letter "J" it appears justin bieber when it should appear Jimi Hendrix. Fix this, please.

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Mostrare un'immagine con Paint
« Risposta #2 il: 25 Novembre 2013, 01:47:18 »
...continua...


" set cached to false !!

are you calling the refresh in the mousemove event ?

--
Fabien Bodard
"


" No, I am not calling "Refresh" in the mousemove event, only once after the
image data has been read in.

I set "cached" to "False". Still, the image does not appear with
"DrawImage". But with painted brushes, it works. I can live with that. I
just wished I understood the logic behind why some of these methods work
with the same settings and why "DrawImage" does not.

Regards,
ukimiku
"


" Please provide a little project that reproduces your bug
(Paint.DrawImage not working).

--
Benoît Minisini
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Mostrare un'immagine con Paint
« Risposta #3 il: 25 Novembre 2013, 01:49:16 »
...continua...


" I think at this point it is best to give you a known-working project to play
with. The attached project
(vedi allegato) is working on my system. If it doesn't work on
yours, we've got a problem. If it works, then try to understand why.

Regards,
Tobi
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.292
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Mostrare un'immagine con Paint
« Risposta #4 il: 25 Novembre 2013, 01:56:01 »
...continua...


" Monsieur,

here is a demo project I uploaded:
Link: https://www.dropbox.com/s/aztacpsshfo3h4i/Fr002DrawImage.zip

Basically, click on "Load", and you are prompted for a binary PPM or PGM
file. The picture is then displayed. If you click on "Save", you can select
a new filename and save the picture in binary PPM format (a tremendous waste
of space for several reasons, but I am getting to know Gambas, and this is
my first project).

*The problem appears to be in*

Codice: gambas [Seleziona]
Public Sub fr_Draw()
     Paint.Begin(fr)
     fr.Clear
 
     Paint.brush = Paint.Image(img)
     Paint.Rectangle(0, 0, img.Width, img.Height)
     Paint.fill
 
     'Paint.DrawImage(img, 0, 0)    ' This does not work, for whatever reason (bug?)
     fr.Cached = False
     paint.End
End


When you un-comment "'Paint.DrawImage(img,0,0)"
(img is a class-global image) and comment the three lines above it
(Paint.brush....), The image is not drawn.

Thank you for looking into this.

Regards,
ukimiku
"


" Hi Tobi,

this is very kind of you. Your sample project works like a charm: I see the
pixels update and change color randomly.

The problem with my project is: After I fill my image with color data, it
gets displayed correctly. Then I fill in new color data, and it is not being
updated anymore. While with the Paint.brush method, the image is being
updated as expected. This is irritatingly inconsistent to me. But then
again, I am a complete Gambas newbie... :)

How do you manage to append attachments to your posts here on the mailing
list? Thanks.

Regards,
ukimiku
"


" Well, you have a working project now which uses Paint.DrawImage(). Try to
find out what your project lacks.

> How do you manage to append attachments to your posts here on the mailing
> list? Thanks.
>

I see you use nabble.com... If you want to send attachments, you have to
subscribe to this mailing list at sourceforge:
https://lists.sourceforge.net/lists/listinfo/gambas-user

Regards,
Tobi
"


" remove all the occurence on fr.cached... this is not needed at all
(there is some with = true)

remove fr.clear (this is slow and uneeded in the _draw event)

Fabien Bodard
"


" work fine here

Fabien Bodard
"


" works fine? I suspect that on the first run, you see some sort of blue-ish
stripes, and what happens after "Load" and selecting a PPM image? On my
system, nothing happens if I use "Paint.DrawImage". Only if use the
Paint.brush method, it works, and the loaded image is displayed.

??

Regards,
ukimiku
"


" Fabien,

thank you for your tips. I cannot remove

 
Codice: gambas [Seleziona]
fr.cached = True

before the file open dialog comes up:
 
Codice: gambas [Seleziona]
Dialog.Title = "Save image file..."
  ...

If I do remove the line, the file dialog comes to halt, taking minutes to
display a single file entry. Maybe may Gambas application is refreshing if
"cached" is set to "false"?

Regards,
ukimiku
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »