AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help using GDI+ to crop images

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
louco
Guest





PostPosted: Sun May 06, 2007 6:46 am    Post subject: help using GDI+ to crop images Reply with quote

Hello.
I don't know how to call GDI+ to crop images.
I have this info:

Quote:
DECLARE FUNCTION GdipCloneBitmapAreaI LIB "GDIPLUS.DLL" ALIAS "GdipCloneBitmapAreaI" ( _
BYVAL x AS LONG _ ' INT
, BYVAL y AS LONG _ ' INT
, BYVAL nWidth AS LONG _ ' INT
, BYVAL nHeight AS LONG _ ' INT
, BYVAL PixelFormat AS LONG _ ' PixelFormat
, BYVAL srcBitmap AS DWORD _ ' *GpBitmap
, BYREF dstBitmap AS DWORD _ ' **GpBitmap
) AS LONG ' GpStatus <enum>

%PixelFormat1bppIndexed = 196865
%PixelFormat4bppIndexed = 197634
%PixelFormat8bppIndexed = 198659
%PixelFormat16bppGrayScale = 1052676
%PixelFormat16bppRGB555 = 135173
%PixelFormat16bppRGB565 = 135174
%PixelFormat16bppARGB1555 = 397319
%PixelFormat24bppRGB = 137224
%PixelFormat32bppRGB = 139273
%PixelFormat32bppARGB = 2498570
%PixelFormat32bppPARGB = 925707
%PixelFormat48bppRGB = 1060876
%PixelFormat64bppARGB = 3424269
%PixelFormat64bppPARGB = 29622286
%PixelFormatMax = 15



I already read "GDIplusHekper.ahk" and others gdi related scripts,I tried for hours but no results.
Any help is welcome, thanks.
Back to top
Paulo-nli
Guest





PostPosted: Mon May 07, 2007 2:15 am    Post subject: Reply with quote

Hi,louco
I think you have all the needed info.
I guess you are thinking that this function will write the resulting file to disk, but it is not true.
You should call another function to save the file.

You will need PhiLho's GDIplusWrapper.ahk(http://www.autohotkey.com/forum/topic11860.html&sid=12dd74aee6284a5f06bdc77ba38cc7f1)
Add these lines to GDIplusWrapper.ahk
Code:

GDIplus_cropImage(x,y,w,h,bitmap, ByRef croped)
{
   local r
   #GDIplus_lastError =
   r := DllCall("GDIplus.dll\GdipCloneBitmapAreaI"
      , "Int", x
      , "Int", y
      , "Int", w
      , "Int", h
      , "Int", 0
      , "UInt", bitmap
      , "UInt *", croped)
   If (r != #GDIplusOK)
   {
      #GDIplus_lastError := "GdipCloneBitmapAreaI (" . r . ": " . #GpStatus@%r% . ") _" . ErrorLevel
   }
   Return r
}


And try this code:

Code:

#Include, GDIplusWrapper.ahk

src = somefile ;larger than 50x50
dest = crop.jpg ;let's use jpeg format
jpg_quality = 88 ;quality 0-100
If (GDIplus_Start() != 0)
{
   Goto g_GDI_Error
   Return
}
If (GDIplus_GetEncoderCLSID(jpgEncoder, #GDIplus_mimeType_JPG) != 0)
{
   Goto g_GDI_Error
   Return
}
GDIplus_InitEncoderParameters(jpegEncoderParams, 1)
If (GDIplus_AddEncoderParameter(jpegEncoderParams, #EncoderQuality, jpg_quality) != 0)
{
   Goto g_GDI_Error
   Return
}
If (GDIplus_LoadBitmap(bitmap, src) != 0)
   Goto g_GDI_Error
If (GDIplus_cropImage(0,0,30,30,bitmap,croped) != 0)
   Goto g_GDI_Error
If (GDIplus_SaveImage(croped, dest, jpgEncoder, jpegEncoderParams) != 0)
   Goto g_GDI_Error
GDIplus_Stop()
Return

g_GDI_Error:
SoundBeep, 300, 200
If (#GDIplus_lastError != "")
   MsgBox 16, GDIplus Test, Error in %#GDIplus_lastError%
GDIplus_Stop()
Return

[/code]
Back to top
louco
Guest





PostPosted: Mon May 07, 2007 4:14 am    Post subject: Reply with quote

Thanks Paulo-nli
I did not know about the need of the GdipSaveImageToFile function.
I'll try some more functions now!
Thanks again Very Happy
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Mon May 07, 2007 12:30 pm    Post subject: Reply with quote

Dear Paulo, Smile

Splendid. Very Happy Thanks for the GDIplus_CropImage().

This may sound overzealous, but can you make that function standalone ?!
I need just that one functionality from GDI Plus. I need it for one of my scripts and was thinking of using a command-line tool before I saw your post.

Can you help, Please ?

Regards, Smile
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon May 07, 2007 1:17 pm    Post subject: Reply with quote

Cool, thanks for the clear example.
I might add this to my wrapper, if you don't mind.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Paulo-nli
Guest





PostPosted: Tue May 08, 2007 12:13 am    Post subject: Reply with quote

Hi, Skan.
With sure we can write new functions merging the functions called to crop a img, but I am not sure if it is a good idea since the crop operation requires many steps calling many functions wich may call more functions.
To mantain some flexibility we will need at least four functions : to start GDI, to define the encoder settings, to load-crop-save-dispose imgs and to close GDI.
I think we will not save resources merging the functions and the performance is limited by the read/write operations anyway.
Maybe it is better to use a version of GDIplusWrapper.ahk without the functions and vars you will never use.

@PhiLho
Of course I don't mind

note :
I forgot to comment the PixelFormat value line ", "Int", 0" - 0 =PixelFormatDontCare
And I was not carefull enough choosing the vars names
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Tue May 08, 2007 1:00 pm    Post subject: Reply with quote

Paulo-nli wrote:
With sure we can write new functions merging the functions called to crop a img, but I am not sure if it is a good idea since the crop operation requires many steps calling many functions wich may call more functions.
To mantain some flexibility we will need at least four functions : to start GDI, to define the encoder settings, to load-crop-save-dispose imgs and to close GDI.


I realised that, after taking a look @ PhiLho's GDI+ wrapper.

Quote:
I think we will not save resources merging the functions and the performance is limited by the read/write operations anyway.
Maybe it is better to use a version of GDIplusWrapper.ahk without the functions and vars you will never use.


My requirement: I am writing a Picture puzzle for which I have to slice an image in to 36 equal parts. Right now I am using IrfanView's command-line-interface for crop facility. Using the GDI wrapper will make my game code very complicated and so thought I could have a standalone function.

Now I have decided to compile an AHK executable ( with your function and GDI+ Wrapper ) which will accept the right parameters and do the slicing job.

I thank you again for posting the function when I am in need for it.

Regards, Smile
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed May 09, 2007 10:50 am    Post subject: Reply with quote

Skan wrote:
My requirement: I am writing a Picture puzzle for which I have to slice an image in to 36 equal parts. Right now I am using IrfanView's command-line-interface for crop facility.
Maybe I don't understand fully your need, but I think you can do that without using GDI+ and without needing 36 images.
See my Infinite Progress Bar which uses plain GDI's BitBlt to take parts of a large image and in Include bitmaps in your scripts!, I give a ReplacePictureImage that show how to replace the Picture image. Stitching them together might allow you to get what you want.
I might try and find some time to help you if you want.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Wed May 09, 2007 11:40 am    Post subject: Reply with quote

Dear PhiLho, Smile

Quote:
Maybe I don't understand fully your need, but I think you can do that without using GDI+ and without needing 36 images.


I am not sure myself. Rolling Eyes. Allow me a couple of days. I will post a fully working script dependent on IrfanView.

Quote:
See my Infinite Progress Bar which uses plain GDI's BitBlt to take parts of a large image


Yes! That might be the candidate which can replace IrfanView.

Quote:
I might try and find some time to help you if you want.


I will be glad if you have the time. I will alert you as soon as I post it.

Regards, Smile
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Thu May 24, 2007 7:49 am    Post subject: Reply with quote

Dear PhiLho, Smile

Here it is: Crazy Scripting : Picture Puzzle 0.9a

I am using IrfanView for
1) Converting the source image to a target BMP file with fixed resolution of 768x576
2) Slicing the target BMP into 36 equal parts which will be loaded as individual static controls.

Please look into it at let me know your valuable suggestions.

Regards, Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group