[Gambas-user] Drawing

Timothy Marshal-Nichols timothy.marshal-nichols at ...247...
Thu Apr 27 16:07:43 CEST 2006


Converting a Drawing to a image/picture is easy. Here is code that draws a
Drawing on a Picture and saves it in one of the image file types supported
by Gambas.

' Convert the drawing to an picture and save it
PRIVATE SUB SaveDrawing(drw AS Drawing)
  DIM savePicture AS Picture
  Dialog.Filter = FileFilter()
  IF Dialog.SaveFile() THEN RETURN
  IF Exist(Dialog.Path) THEN
    IF Message.Warning("The file \n\n\t" & Dialog.Path & "\n\nalready
exists. Do you want to overwrite it?",
      "Yes", "Cancel") > 1 THEN
      RETURN
    END IF
  END IF
  savePicture = NEW Picture(drw.Width, drw.Height, FALSE)
  Draw.Begin(savePicture)
  Draw.BackColor = Color.White
  Draw.ForeColor = Color.White
  Draw.FillColor = Color.White
  Draw.FillStyle = Fill.Solid
  Draw.Rect(0, 0, savePicture.Width, savePicture.Height)
  Draw.Drawing(drw, 0, 0)
  Draw.End
  savePicture.Save(Dialog.Path)
CATCH
  Message.Warning("Could not save image\n\n\t" & Dialog.Path & "\n\n" &
ERROR.Text)
END

PRIVATE FUNCTION FileFilter() AS String[]
  DIM filter AS NEW String[]
  filter.Add("Portable Network Graphics (*.png)")
  filter.Add("Joint Photographic Experts Group (*.jpeg *.jpg)")
  filter.Add("Windows Bitmap (*.bmp)")
  filter.Add("Graphics Interchange Format (*.gif)")
  filter.Add("X PixMap (*.xpm)")
  RETURN filter
END

Note that I clear the new Picture myself. You could try the Clear() method
on the Picture. But I found this method works best. I want to make sure the
background to the drawing is white.

Look in the help for the distinction between a Picture and a Image. You can
convert between them easily:
	newPicture = oldImage.Picture
	newImage = oldPicture.Image
As for getting the content of a DrawingArea. It can be done with the Grab()
method which return a Picture.

PUBLIC SUB ButtonSaveDrawingArea_Click()
  DIM savePicture AS Picture
  Dialog.Filter = FileFilter()
  IF Dialog.SaveFile() THEN RETURN
  IF Exist(Dialog.Path) THEN
    IF Message.Warning("The file \n\n\t" & Dialog.Path & "\n\nalready
exists. Do you want to overwrite it?",
      "Yes", "Cancel") > 1 THEN
      RETURN
    END IF
  END IF
  savePicture = DrawingAreaSVG.Grab()
  savePicture.Save(Dialog.Path)
CATCH
   Message.Warning("Could not save image\n\n\t" & Dialog.Path & "\n\n" &
ERROR.Text)
END

But I would not use this. Grab() only returns the visible area of the
DrawingArea so you might not get what you expect. In any case you should not
need it.


Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>


-----Original Message-----
From: gambas-user-admin at lists.sourceforge.net
[mailto:gambas-user-admin at lists.sourceforge.net]On Behalf Of javier
romero
Sent: Wednesday, 26 April 2006 14:51
To: gambas-user at lists.sourceforge.net
Subject: RE: Re: [Gambas-user] Drawing


Thanks very much for the help.

I have an aditional question, is possible to convert a Drawing or a
DrawingArea into a Image or Picture?

Regards
Javier

----------------------------------------
> From: timothy.marshal-nichols at ...247...
> To: gambas-user at lists.sourceforge.net
> Subject: RE: Re: [Gambas-user] Drawing
> Date: Wed, 26 Apr 2006 09:39:23 +0100
>
>
>
> You must be using an older version of Gambas. The *= is just
multiplication.
> Try:
>
>      imageWidth = imageWidth * scaleIsotropic
>      imageHeight = imageHeight * scaleIsotropic
>
> this the same code in long hand.
>
> The PrintImage code was to show you how to use the Resolution of the
Printer
> and Desktop to find the required image/drawing size. Its just I had code
to
> hand using a image that I knew worked. You can still Draw a Drawing.
>
> This is what you might get:
>
> PRIVATE SUB PrintDrawing(drw AS Drawing)
>   DIM drawingWidth AS Integer
>   DIM drawingHeight AS Integer
>   DIM scaleIsotropic AS Float
>   ' Display printer dialog
>   IF Printer.Setup() THEN RETURN
>   INC Application.Busy
>   ' Get drawing size for the printer resolution
>   drawingWidth = (drw.Width * Printer.Resolution) / Desktop.Resolution
>   drawingHeight = (drw.Height * Printer.Resolution) / Desktop.Resolution
>   ' Adjust drawing size if it does not fit on the printer page
>   IF drawingWidth > Printer.Width OR drawingHeight > Printer.Height THEN
>     ' Scale Isotropic, keep aspect ratio of the image
>     scaleIsotropic = Min(Printer.Width / drawingWidth, Printer.Height /
> drawingHeight)
>     drawingWidth *= scaleIsotropic
>     drawingHeight *= scaleIsotropic
>   END IF
>   'PRINT "Width: " & drawingWidth, drw.Width, Printer.Width
>   'PRINT "Height: " & drawingHeight, drw.Height, Printer.Height
>   ' Draw drawing centred on printer page
>   Draw.Begin(Printer)
>   'Draw.Rect((Printer.Width - drawingWidth) / 2, (Printer.Height -
> drawingHeight) / 2, drawingWidth, drawingHeight)
>   Draw.Drawing(drw, (Printer.Width - drawingWidth) / 2, (Printer.Height -
> drawingHeight) / 2, drawingWidth, drawingHeight)
>   Draw.End
>   DEC Application.Busy
> END
>
> Two points:
>
> 1. Convert the *= again!
>
> 2. On my system this still only prints the Drawing at the old resolution.
> You can see if you uncomment the Draw.Rect line that the calculation for
the
> drawing size is correct. By setting the width and height the help says the
> drawing should be scaled. I think this has been fixed with later versions
of
> Gambas as I know there has been some work the SVG code. (I was going to
> upgrade after installing SUES 10.1.)
>
> Also the scaling of a Drawing when drawing on a DrawingArea does not
work -
> so it must be a problem with the Draw.Drawing method. If this method has
not
> been fixed then you will never be able to print good image at the right
> size.
>
> Thanks
>
> 8-{)} Timothy Marshal-Nichols
> <mailto: timothy.marshal-nichols at ...247...>
>
>
> -----Original Message-----
> From: gambas-user-admin at lists.sourceforge.net
> [mailto:gambas-user-admin at lists.sourceforge.net]On Behalf Of javier
> romero
> Sent: Tuesday, 25 April 2006 19:24
> To: gambas-user at lists.sourceforge.net
> Subject: RE: Re: [Gambas-user] Drawing
>
>
> I try to do that, but in
>
>      imageWidth *= scaleIsotropic
>      imageHeight *= scaleIsotropic
>
> the interpreter sends an error.
> there is any error in there?.
>
> Also i need convert a drawing area into a image, how i can do that?
>
> Thanks
> Javier
> ----------------------------------------
> > From: timothy.marshal-nichols at ...247...
> > To: gambas-user at lists.sourceforge.net
> > Subject: RE: Re: [Gambas-user] Drawing
> > Date: Mon, 24 Apr 2006 17:57:08 +0100
> >
> >
> > This example shows how to print an image centred on the page. In
includes
> > code to scale an image for the printer resolution. If the image is the
to
> > large for the printer height or width resizes it to fit on the page -
> > keeping the aspect ratio.
> >
> > You will need something like this only with a drawing.
> >
> > PRIVATE SUB PrintImage(img AS Image)
> >   DIM imageToPrint AS Image
> >   DIM imageWidth AS Integer
> >   DIM imageHeight AS Integer
> >   DIM scaleIsotropic AS Float
> >   ' Dispaly printer dialog
> >   IF Printer.Setup() THEN RETURN
> >   INC Application.Busy
> >   ' Get image size for the printer resolution
> >   imageWidth = (img.Width * Printer.Resolution) / Desktop.Resolution
> >   imageHeight = (img.Height * Printer.Resolution) / Desktop.Resolution
> >   ' Adjust image size if it does not fit on the printer page
> >   IF imageWidth > Printer.Width OR imageHeight > Printer.Height THEN
> >     ' Scale Isotropic, keep aspect ratio of the image
> >     scaleIsotropic = Min(Printer.Width / imageWidth, Printer.Height /
> > imageHeight)
> >     imageWidth *= scaleIsotropic
> >     imageHeight *= scaleIsotropic
> >   END IF
> >   'PRINT "Width: " & imageToPrint, Printer.Width
> >   'PRINT "Height: " & imageToPrint, Printer.Height
> >   ' Scale image
> >   imageToPrint = img.Stretch(imageWidth, imageHeight, TRUE)
> >   ' Draw image centered on printer page
> >   Draw.Begin(Printer)
> >   Draw.Image(imageToPrint, (Printer.Width - imageWidth) / 2,
> > (Printer.Height - imageHeight) / 2)
> >   Draw.End
> >   DEC Application.Busy
> > END
> >
> >
> > Thanks
> >
> > 8-{)} Timothy Marshal-Nichols
> > <mailto: timothy.marshal-nichols at ...247...>
> >
> >
> > -----Original Message-----
> > From: gambas-user-admin at lists.sourceforge.net
> > [mailto:gambas-user-admin at lists.sourceforge.net]On Behalf Of javier
> > romero
> > Sent: Monday, 24 April 2006 14:37
> > To: gambas-user at lists.sourceforge.net
> > Subject: RE: Re: [Gambas-user] Drawing
> >
> >
> > Ok, thanks, its works but the print is very small, i try scaling with
> this:
> >
> >    draw.Begin(printer)
> >      draw.drawing(p1,350,350, Printer.Width, Printer.Height)
> >    draw.End
> >    but dosent works the print is the same, little, i try then with
> >
> >    draw.Begin(printer)
> >      draw.drawing(p1,350,350, p1.Width, p1.Height)
> >    draw.End
> > and the same, very little, in both dont change
> >
> > How i scale?
> >
> > sorry about my very bad english
> >
> > Regards
> > Javier
> > ----------------------------------------
> > > From: christian.faurebouvard at ...357...
> > > To: gambas-user at lists.sourceforge.net
> > > Subject: Re: [Gambas-user] Drawing
> > > Date: Sat, 22 Apr 2006 17:50:15 -0400
> > >
> > > El Viernes 21 Abril 2006 09:48, javier romero escribió:
> > > > PUBLIC p1 AS Drawing
> > > > PUBLIC pag AS DrawingArea
> > > >
> > > > PUBLIC SUB _new()
> > > >     p1 = NEW Drawing
> > > >     draw.Begin(p1)
> > > >       draw.Text("Hello", 0, 0)
> > > >     draw.End
> > > >
> > > > END
> > > >
> > > > PUBLIC SUB Form_Open()
> > > >
> > > > pag = NEW DrawingArea(sv1)
> > > >
> > > >   pag.Cached = TRUE
> > > >   pag.Clear
> > > >   pag.BackColor = color.White
> > > >   pag.Width = printer.Width
> > > >   pag.Height = printer.Height
> > > >   pag.Border = 1
> > > >   draw.Begin(pag)
> > > >     draw.Drawing(p1, 0, 0)
> > > >   draw.End
> > > > ' This works OK
> > > >   pag.Visible = TRUE
> > > >
> > > > END
> > > >
> > > > PUBLIC SUB Button1_Click()
> > > >
> > > >   draw.Begin(printer)
> > > >     draw.drawing(p1,0,0, pag.Width, pag.Height)
> > > >   draw.End
> > > > ' This doesn't works
> > > > END
> > >
> > > Hi Javier,
> > >
> > > Some printers can't print in (0,0)
> > > Try
> > >  draw.drawing(p1, 350, 350)
> > >
> > > it work, but very small, need to be scaled.
> > >
> > > Regards,
> > > Christian
> > >
> > >
> > > -------------------------------------------------------
> > > Using Tomcat but need to do more? Need to support web services,
> security?
> > > Get stuff done quickly with pre-integrated technology to make your job
> > easier
> > > Download IBM WebSphere Application Server v.1.0.1 based on Apache
> Geronimo
> > > http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
> > > _______________________________________________
> > > Gambas-user mailing list
> > > Gambas-user at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> > _________________________________________________________________
> > Descarga gratis la Barra de Herramientas de MSN
> >
>
http://www.msn.es/usuario/busqueda/barra?XAPID=2031&DI=1055&SU=http%3A//www.
> > hotmail.com&HL=LINKTAG1OPENINGTEXT_MSNBH
> >
> > -------------------------------------------------------
> > Using Tomcat but need to do more? Need to support web services,
security?
> > Get stuff done quickly with pre-integrated technology to make your job
> > easier
> > Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
> > http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> >
> >
> >
> > -------------------------------------------------------
> > Using Tomcat but need to do more? Need to support web services,
security?
> > Get stuff done quickly with pre-integrated technology to make your job
> easier
> > Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
> > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
>
> _________________________________________________________________
> Descarga gratis la Barra de Herramientas de MSN
>
http://www.msn.es/usuario/busqueda/barra?XAPID=2031&DI=1055&SU=http%3A//www.
> hotmail.com&HL=LINKTAG1OPENINGTEXT_MSNBH
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

_________________________________________________________________
Descarga gratis la Barra de Herramientas de MSN
http://www.msn.es/usuario/busqueda/barra?XAPID=2031&DI=1055&SU=http%3A//www.
hotmail.com&HL=LINKTAG1OPENINGTEXT_MSNBH

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user






More information about the User mailing list