FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Cut image
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Cut image
Posted: Mon May 30, 2011 08:22 PM

Hello,

I need to read image from disk without displaying it, then cut some area and save these to disk in new file

some help I will appreciate

regards

Marcelo

Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: Cut image
Posted: Mon May 30, 2011 08:51 PM

Marcelo,

I don*t understand.
How do You want to save a area of a Image without Preview,
to select the 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.
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Cut image
Posted: Mon May 30, 2011 08:57 PM

Hello,

thanks for your response, I will try to be clear

I have a image in file, what I want is cut some area from this image and save cuted area to other image file, without show any thing. The area to cut (top, left...) will be parameters.

thanks in advance

Marcelo

Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: Cut image
Posted: Mon May 30, 2011 09:55 PM
Marcelo,

there is a command-line-tool < IMAGEMAGICK > Freeware
Download :
http://www.imagemagick.org/script/index.php

Read about using Crop-Commands :
http://www.imagemagick.org/Usage/crop/#trim



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.
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Cut image
Posted: Mon May 30, 2011 10:54 PM
Marcelo

Ejemplo funcional

http://www.sitasoft.net/fivewin/samples/cutbmp.zip

Code (fw): Select all Collapse
#include "fivewin.ch"


function main()

   local oWnd
   
   local hBmp := ReadBitmap( 0, "007.bmp" )
   local hCut := CutImage( 40, 40, 80, 80, hBmp )
   local hDib := DibFromBitmap( hCut )
   
   DibWrite( "test.bmp", hDib )
   
   DeleteObject( hDib )
   DeleteObject( hCut )
   DeleteObject( hBmp )
   

return nil


#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>


HB_FUNC( CUTIMAGE )
{
   HDC hdc1, hdcSrc, hdcDest;
   HBITMAP hbmpSrc  = ( HBITMAP ) hb_parnl( 5 );
   HBITMAP hbmpDest, hold1, hold2;
   RECT rct;
   BITMAP bm;
   
   rct.top  = hb_parnl( 1 );
   rct.left = hb_parnl( 2 );
   rct.bottom = hb_parnl( 3 );
   rct.right  = hb_parnl( 4 );
   
   GetObject( ( HGDIOBJ ) hbmpSrc, sizeof( BITMAP ), ( LPSTR ) &bm );

   hdc1 = GetDC( GetDesktopWindow() );
   hdcSrc = CreateCompatibleDC( hdc1 );
   hdcDest = CreateCompatibleDC( hdc1 );

   hbmpDest = CreateCompatibleBitmap( hdc1, rct.right - rct.left, rct.bottom - rct.top );

   ReleaseDC( GetDesktopWindow(), hdc1 );
      
   hold1 = SelectObject( hdcSrc, hbmpSrc );
   hold2 = SelectObject( hdcDest, hbmpDest );
      
   BitBlt( hdcDest, 0, 0, rct.right, rct.bottom, hdcSrc, rct.left, rct.top, SRCCOPY );
   
   SelectObject( hdcSrc, hold1 );
   SelectObject( hdcDest, hold2 );
   
   DeleteDC( hdcSrc );
   DeleteDC( hdcDest );
   
   hb_retnl( ( LONG ) hbmpDest );
   
}
#pragma ENDDUMP
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Cut image
Posted: Mon May 30, 2011 11:42 PM

Daniel,

thanks very much, but :-) I am trying to do the same but for JPG files, are there some function compatible with ReadBitmap but for JPG files?

Regards

Marcelo

Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Cut image
Posted: Tue May 31, 2011 12:18 AM
Marcelo

yes.. you can do it with any image format supported by freeimage

i did some changes in code and parameter

by suggestion y changed the function name to CropImage( hOriginalBmp, nTop, nLeft, nBottom, nRight ) --> hCroppedBmp
is already added to fivewin

Code (fw): Select all Collapse
#include "fivewin.ch"


function main()

   CutImage( "007.bmp", "cut_007.bmp" )
   CutImage( "halo.gif", "cut_halo.bmp" )
   CutImage( "fivewin.png", "cut_five.bmp" )
   CutImage( "olga.bmp", "cut_olga.bmp" )

return nil

function CutImage( cNameIn, cNameOut )

   local hBmp := FiLoadImg( cNameIn )
   local hCut := CropImage( hBmp, 40, 40, 80, 80 )
   local hDib := DibFromBitmap( hCut )
   
   DibWrite( cNameOut, hDib )
   
   DeleteObject( hDib )
   DeleteObject( hCut )
   DeleteObject( hBmp )
   
return nil

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

HB_FUNC( CROPIMAGE ) //hOriginalBmp, nTop, nLeft, nBottom, nRight --> hCroppedBmp
{
   HDC hdc1, hdcSrc, hdcDest;
   HBITMAP hbmpSrc  = ( HBITMAP ) hb_parnl( 1 );
   HBITMAP hbmpDest, hold1, hold2;
   RECT rct;
   BITMAP bm;

   GetObject( ( HGDIOBJ ) hbmpSrc, sizeof( BITMAP ), ( LPSTR ) &bm );
   
   rct.top    = hb_pcount() > 1 ? hb_parnl( 2 ) : 0;
   rct.left   = hb_pcount() > 2 ? hb_parnl( 3 ) : 0;
   rct.bottom = hb_pcount() > 3 ? hb_parnl( 4 ) : bm.bmHeight;
   rct.right  = hb_pcount() > 4 ? hb_parnl( 5 ) : bm.bmWidth;
   

   hdc1 = GetDC( GetDesktopWindow() );
   hdcSrc = CreateCompatibleDC( hdc1 );
   hdcDest = CreateCompatibleDC( hdc1 );

   hbmpDest = CreateCompatibleBitmap( hdc1, rct.right - rct.left, rct.bottom - rct.top );

   ReleaseDC( GetDesktopWindow(), hdc1 );
      
   hold1 = SelectObject( hdcSrc, hbmpSrc );
   hold2 = SelectObject( hdcDest, hbmpDest );
      
   BitBlt( hdcDest, 0, 0, rct.right, rct.bottom, hdcSrc, rct.left, rct.top, SRCCOPY );
   
   SelectObject( hdcSrc, hold1 );
   SelectObject( hdcDest, hold2 );
   
   DeleteDC( hdcSrc );
   DeleteDC( hdcDest );
   
   hb_retnl( ( LONG ) hbmpDest );
   
}
#pragma ENDDUMP
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Cut image
Posted: Tue May 31, 2011 01:26 AM
Daniel,

tranks, this work perfectly, I need some help more, because I am making some preprocessing before crop the image I need to translate some code to read a JPG file, the next is sample

Code (fw): Select all Collapse
   LOCAL hBmp   := oImage:hBitmap
   LOCAL hDc    := GetDc(hBmp)
   local hDCMem := CreateCompatibleDC( hDC )
   local hOldBmp:= SelectObject( hDCMem, hBmp )
   local nX     := oImage:nWidth
   local nY     := oImage:nHeight


this work with image object, but I need to work without it, then I need to replace hBmp := oImage:hBitmap, maybe hBmp := FiLoadImg( cNameIn ), but how can I get the width and height

regards

Marcelo
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Cut image
Posted: Tue May 31, 2011 02:01 AM

Daniel,

really I appreciate your help, all work perfectly, one help more sorry :-) how can I save the cropping image to a other file format like JPG

muchas gracias

Marcelo

Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Cut image
Posted: Tue May 31, 2011 02:04 AM

Hello

i dont know if this feature is supported by freeimage, for now is only BMP using fivewin

Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: Cut image
Posted: Tue May 31, 2011 10:33 AM
Daniel,

Imagemagick Command-line-tool uses freeimage.dll but is easy to use
You can convert between different formats adding any options.
It is not possible, to cover all combinations/options with FWH
I*m working on a Visual-command-line-tool, to test everything at Runtime

Some samples :

Resize JPG to 25% and save as Bmp
WAITRUN ( ( "convert fantasy2.jpg -resize 25% resize.bmp", 0 )



Resize JPG to 25% and save as Bmp with Border
WAITRUN ( ( "convert fantasy2.jpg -resize 25% -mattecolor SkyBlue -frame 6x6+2+2 frame1.bmp", 0 )



Crop JPG and save as Bmp with border
WAITRUN ( ( "convert fantasy2.jpg -crop 400x400+0+0 -frame 10x10+6+0 crop1.bmp", 0 )



Crop JPG and save as Bmp with another border-style
WAITRUN ( ( "convert fantasy2.jpg -crop 400x400+0+0 -mattecolor Tomato -frame 10x10+5+5 crop2.bmp", 0 )



Resize to 50% with double-frame raised and convert to TIF
WAITRUN ( ( "convert fantasy2.jpg -resize 50% -mattecolor SkyBlue -frame 10x10+10+0 -frame 15x15+10+0 frame3.tif" , 0 )



You can do everything like rotate, increase contrast, convert to black and white, adding borders .....

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.
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Cut image
Posted: Tue May 31, 2011 11:55 AM
Daniel, Uwe

thanks very much for your help, finally I found the way with freeimage

http://forums.fivetechsupport.com/viewtopic.php?f=6&t=14889#p76878

Thanks to César too

Regards

Marcelo

Continue the discussion