Cut image

Post Reply
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Cut image

Post by Marcelo Via Giglio »

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
User avatar
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Cut image

Post by ukoenig »

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.
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Re: Cut image

Post by Marcelo Via Giglio »

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
User avatar
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Cut image

Post by ukoenig »

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

Image

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
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Cut image

Post by Daniel Garcia-Gil »

Marcelo

Ejemplo funcional

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

Code: Select all | Expand


#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
 
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Re: Cut image

Post by Marcelo Via Giglio »

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
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Cut image

Post by Daniel Garcia-Gil »

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: Select all | Expand


#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
 
 
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Re: Cut image

Post by Marcelo Via Giglio »

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: Select all | Expand


   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
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Re: Cut image

Post by Marcelo Via Giglio »

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
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: Cut image

Post by Daniel Garcia-Gil »

Hello

i dont know if this feature is supported by freeimage, for now is only BMP using fivewin
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: Cut image

Post by ukoenig »

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 )

Image

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 )

Image

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

Image

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 )

Image

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 )

Image

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

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.
Marcelo Via Giglio
Posts: 1077
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia
Has thanked: 1 time

Re: Cut image

Post by Marcelo Via Giglio »

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
Post Reply