Drawing on a TImage and save (revamped) :-) [Solved]

Drawing on a TImage and save (revamped) :-) [Solved]

Postby Enrico Maria Giordano » Thu Apr 03, 2014 6:06 pm

Dear friends, I need to draw on a TImage control and save the result. I already tried using SetPixel() on TImage hDC but it saves the original image without my new drawings. This is a sample:

Code: Select all  Expand view
#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL oDlg, oImg

    DEFINE DIALOG oDlg;
           SIZE 800, 600

    @ 0, 0 IMAGE oImg;
           FILE "TEST.JPG";
           ADJUST

    @ 15, 0 BUTTON "Draw";
            ACTION DRAWIMG( oImg )

    @ 15, 20 BUTTON "Save";
             ACTION oImg:SaveImage( "MYIMAGETEST.JPG", 2 )

    ACTIVATE DIALOG oDlg;
             CENTER

    RETURN NIL


STATIC FUNCTION DRAWIMG( oImg )

    LOCAL hDC := oImg:GetDC()

    LOCAL x, y

    FOR y = 10 TO 50
        FOR x = 10 TO 50
            SETPIXEL( hDC, x, y, CLR_HRED )
        NEXT
    NEXT

    oImg:ReleaseDC()

    RETURN NIL


Any ideas?

Thank you in advance.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Thu Apr 03, 2014 7:24 pm

Enrico,

I finished the Tool.
I think everything You need will be included.
Just added a BOXPAINTING and a extra dialog < EXTENSIONS > where we can add some more options
like all selected images will be saved to a defined size WIDTH or HEIGHT keeping the < aspect ratio >
The size of a loaded OVERSIZED image, will be resized to fit the painting area.
Now FREEHAND, LINES and BOXES are supported.
A runtime-change of image BORDER / NOBORDER is possible now.

Tomorrow I will add the Download.
Maybe to create a extra toppic with the title : Drawing lines on images and export ?

Image

Best Regards
Uwe :lol:
Last edited by ukoenig on Sat Apr 05, 2014 11:23 am, edited 2 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Thu Apr 03, 2014 7:59 pm

Uwe,

can you fix my sample so it saves the modified image correctly, please?

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Thu Apr 03, 2014 10:25 pm

Enrico,

Your sample
I will add it to the tool, to make it easy, to understand the logic ( ENRICO.PRG )
because it is a little bit complicated.

Image

The EXPORT
oImg:SaveImage( cExport, 2, 25 )
For better quality, You can change the quality-factor
The EXPORT-size is 1:1 to original. You can resize the EXPORT
( nResize := 70 will be 70 % of the original )

Image

Code: Select all  Expand view


#include "Fivewin.ch"
#include "Image.ch"

STATIC nImgW, nImgH

FUNCTION MAIN()
LOCAL oDlg, hDC, oImg, hPen, nStart := 0, lPaint := .F., aPoints[1][2]
LOCAL cWorkfile, cDestfile, nPenColor, nPenSize, nResize,cExtension

c_path := cFilePath(GetModuleFileName( GetInstance() ) )
c_path1 := c_path + "IMAGES\"

cWorkFile := "
TEST.JPG"
cDestFile := "
MYIMAGETEST"
nPenColor := 255
nPenSize := 5
nResize := 100
cExtension := "
JPG"

hPen := CreatePen( 0, nPensize, nPenColor )

DEFINE DIALOG oDlg SIZE 800, 600

@ 0, 0 IMAGE oImg FILENAME NIL OF oDlg ;
SIZE 100, 100 PIXEL NOBORDER        // any size, it will be adjusted
oImg:bPainted := { |hDC| ( IMGSIZE(cWorkfile, nResize), ;
                                                  DRAWIMG( hDC, oImg, cWorkFile, nResize ) ) }
oImg:cTooltip := "
selected Image"

// @ 0, 0 IMAGE oImg FILE c_path1 + "
TEST.JPG" ADJUST

@ 15, 0 BUTTON "
Draw" ACTION DRAW( hDC )

@ 15, 20 BUTTON "
Save";
ACTION SAVE_IMG(oDlg, oImg, cDestFile, cExtension)

ACTIVATE DIALOG oDlg CENTER ;
ON INIT ( IMGSIZE(cWorkfile, nResize), ;
          hDC := oImg:GETDC(), ;
          oImg:Move( 1, 1, nImgW * (nResize / 100), nImgH * (nResize / 100), .f. ) )

ReleaseDC( oImg:hWnd, hDC )
DeleteObject( hPen )

RETURN NIL

//-------------

STATIC FUNCTION IMGSIZE(cWorkfile, nResize)
LOCAL oTest

DEFINE IMAGE oTest FILENAME c_path1 + cWorkFile
nImgW := oTest:nWidth * (nResize / 100)
nImgH := oTest:nHeight * (nResize / 100)
oTest:End()

RETURN NIL

//-------------

FUNCTION DRAWIMG( hDC, oImg, cWorkFile, nResize )
LOCAL oBrush, hPen, oTest

IF FILE( c_path1 + cWorkFile )
       oImg:Move( 0, 0, nImgW, nImgH, .f. )
    // resize if needed witth % nResize
    DEFINE IMAGE oBrush FILENAME c_path1 + cWorkFile
    PalBmpDraw( hDC, 0, 0, oBrush:hBitmap, , nImgW, nImgH )
    oBrush:End()
ELSE
    IF !EMPTY(cWorkFile)
        MsgAlert( "
File : " + cWorkFile + CRLF + ;
                "
does not exist" + CRLF + ;
                    "
to show Image !", "ATTENTION" )
    ENDIF
ENDIF

RETURN( NIL )

// ----------------------------

STATIC FUNCTION SAVE_IMG(oDlg, oImg, cDestFile, cExtension)
LOCAL oImage, cExport := c_Path1 + ALLTRIM(cDestFile) + "
." + cExtension

// Save the painted image as BMP
oImg:SaveToBmp( c_Path1 + ALLTRIM(cDestFile) + "
.bmp" )  

IF FILE ( c_Path1 + ALLTRIM(cDestFile) + "
.bmp" )
    // CONVERT if selected
    IF cExtension <> "
BMP"
        @ 0, 0 IMAGE oImg SIZE 1, 1 OF oDlg
        oImg:LoadBmp( c_Path1 + ALLTRIM(cDestFile) + "
.bmp" )
        oImg:SaveImage( cExport, 2, 25 )
        oImg:End()
    ENDIF
    IF !FILE ( cExport  )
        MsgAlert( "
Export-file : " + CRLF + ;
                   cExport + CRLF + ;
                   "
not created !", "CREATION-ERROR" ) 
    ELSE   
        MsgAlert( "
Export-file : " + CRLF + ;
                   cExport + CRLF + ;
                   "
created !", "FILE-CREATE" )
            IF cExtension <> "
BMP"  // Delete the painted BMP
                DELETE FILE (  c_Path1 + cDestFile + "
.bmp" )
            ENDIF
    ENDIF
ELSE
    MsgAlert( "
BMP Export-file : " + CRLF + ;
               c_Path1 + ALLTRIM(cDestFile) + "
.bmp" + CRLF + ;
               "
is NOT created !", "ERROR" )
ENDIF

RETURN NIL

// --------------------------

STATIC FUNCTION DRAW( hDC )

LOCAL x, y

FOR y = 10 TO 50
        FOR x = 10 TO 50
            SETPIXEL( hDC, x, y, CLR_HRED )
        NEXT
NEXT

RETURN NIL


Best regards
Uwe :lol:
Last edited by ukoenig on Thu Apr 03, 2014 10:58 pm, edited 6 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Thu Apr 03, 2014 10:41 pm

Uwe,

sorry but it doesn't work correctly. Just try with a 1024 x 768 TEST.JPG...

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Thu Apr 03, 2014 10:42 pm

And the saved image is of poor quality. :-(

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Thu Apr 03, 2014 10:49 pm

Enrico,

1. a oversized image ( bigger the painting area )
--- is reduced in size for painting, it must be <= the defined painting area
--- You can define the original size ( expand ) for the EXPORT.
--- It is not possible to paint on a SCROLL-image
--- how it works, is shown in the tool.
--- ( I can modify You sample, to show how to work with oversized images )
2. You can change the Quality-level !!!

cWorkFile := "TEST.JPG"
cDestFile := "MYIMAGETEST"
nPenColor := 255
nPenSize := 5
nResize := 100 // RESIZE-factor
cExtension := "JPG" // define the EXPORT-format, it will be converted to the format-number

oImg:SaveImage( cExport, 2, 25 )
it was defined ( low quality ), how to change the quality

changed from 25 to 80 see the difference !
it will grow in size !!!

Image

Best regards
Uwe :?:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Fri Apr 04, 2014 8:15 am

Uwe,

ukoenig wrote:Enrico,

1. a oversized image ( bigger the painting area )
--- is reduced in size for painting, it must be <= the defined painting area
--- You can define the original size ( expand ) for the EXPORT.
--- It is not possible to paint on a SCROLL-image


And what if you want to draw on an image bigger than screen?

ukoenig wrote:--- ( I can modify You sample, to show how to work with oversized images )


Yes, please.

ukoenig wrote:2. You can change the Quality-level !!!


Ah, ok, sorry.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Fri Apr 04, 2014 10:29 am

Enrico,

1. The oversized Image is resized to 70 %, to be shown complete inside the drawing area.
2. Do Your drawing
3. Export the imgage with > oImg:SaveToBmp( c_Path1 + ALLTRIM(cDestFile) + ".bmp" )
--- only doing it this way, will save / include Your paintings !!!
--- You cannot use a image with defined SCROLL, because a refresh deletes the painting ( clears the image )
4. Next the BMP is resized back to the original size and exported to the destination file / format ( JPG )

It is not easy to understand :cry:
I added some extensions to Your sample and will place it as soon it is finished.
I think showing the logic with a small sample is a good idea.
To understand the tool, the inexperted user might getting confused :cry: .

Image

The exported Image with original size ( 1024 x 768 )

Image

Best regard
Uwe :lol:
Last edited by ukoenig on Fri Apr 04, 2014 10:54 am, edited 3 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Fri Apr 04, 2014 10:42 am

Uwe,

with resizing you will lost image resolution (quality). Or am I wrong?

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Fri Apr 04, 2014 10:46 am

Enrico,

Big resizing YES, but it is only from 70 to 100 %.
That is not visible.
The needed resize-value belongs to the used painting area.
A big area needs a smaller resizing.

Best regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Fri Apr 04, 2014 10:52 am

Uwe,

sorry, it's not what I'm looking for. A need a way to draw on an image a save it without quality loss.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Fri Apr 04, 2014 11:01 am

Enrico,

Why not calling a commercial tool like < Paint.exe > loading Your oversized image ?
It belongs only to images > the defined painting area.

Best regards
Uwe :?:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Drawing on a TImage and save (revamped) :-)

Postby Enrico Maria Giordano » Fri Apr 04, 2014 11:09 am

Uwe,

I can't use an external painting tool because I need to paint on the image programmatically not by hand.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Drawing on a TImage and save (revamped) :-)

Postby ukoenig » Fri Apr 04, 2014 11:24 am

Enrico,

I rearranged the drawing area and reached a resize-factor of 90 %
That is NOTHING !!
Another solution : change the computer used resulution.
I will try and think that You can reach 100 % loading a image 1024 x 768.

Another idea :
There is NO need, using a dialog.
Use a FULL-screen-window for painting and LEFT, RIGHT mouseclick, to draw and save. Saving will close the window as well.
With a window, You can load 1024 x 768 with 100 % without changing the computer-resulution
I can test this solution.
It could be done with a dialog as well using STYLE no border no title ( full screen ).

Image

Best regards
Uwe :?:
Last edited by ukoenig on Fri Apr 04, 2014 11:34 am, edited 7 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 97 guests