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
In the Form_Open handler, I instantiate "img" with
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
img[x,y] = < some integer >
Then I issue a "fr.Refresh" command, hoping "fr" would display
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
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:
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:
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():
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:
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
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 "
...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*
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
before the file open dialog comes up:
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 "