Что нового

Конвертация картинок

Ganibal95

GreenBytes
Сообщения
877
Репутация
240
Всем привет :smile:, как можно примерно перевести картинку с 1го формата в другой, например: Bmp > jpg, jpg > bmp, ico - png, png - ico .и.т.п...
А также чтоб можно было указать новый размер картинки пример: 1024х600 > 600х400...
Зарание спс :IL_AutoIt_1:
 

KING536

Новичок
Сообщения
21
Репутация
0
У меня тоже возникал такой вопрос, UP !!!
 

Zaramot

I ♥ AutoIt
Сообщения
1,160
Репутация
660
BMP в JPG:
Код:
#include <GDIPlus.au3>

Global $sImage, $hImage, $sCLSID

$sImage = FileOpenDialog("BMP to JPG", "", "BMP Image (*.bmp)", '', '', GUICreate(''))
if @Error  then Exit

_GDIPlus_StartUp()
$hImage = _GDIPlus_ImageLoadFromFile($sImage)
$sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
_GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & "\AutoItImage.jpg", $sCLSID)
_GDIPlus_ShutDown()
 
Автор
Ganibal95

Ganibal95

GreenBytes
Сообщения
877
Репутация
240
Мне бы чтоб устанавливать новый размер...
и чтоб получалось примерно так:
Чтоб картинка не обрезалась, а сжималась(Уменьшалось)... 1024х600 > 400x200...
Чтоб от старой картинки не 400х200 отрезалось, а именно вся старая картинка уменьшалась, и умещалась на 400х200...
 

Zaramot

I ♥ AutoIt
Сообщения
1,160
Репутация
660
PNG в ICO:
Код:
#include <GDIPlus.au3>


$PngFile = FileOpenDialog("Choose your PNG icon", "", "PNG icon file (*.png)", 1)
$sIcoFile = @ScriptDir & '\Icon.ico'


_GDIPlus_Startup()
; Load image
$hImage = _GDIPlus_ImageLoadFromFile($PngFile)
; create a HICON from the image
$HICON = __GDIPlus_BitmapCreateHICONFromBitmap($hImage)
;Clean up and shutdown GDIPlus
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
; create an ico file from the image
__CreateIconFileFromHICON($HICON, $sIcoFile)
; Destroy the HICON now I've finished with it.
_WinAPI_DestroyIcon($HICON)

Func __CreateIconFileFromHICON($HICON, $sOutIcon)
	Local $aInfo, $sIco, $sBmp, $hCDC, $tBI, $tBits, $iSz, $sBD, $FO
	; Start of single icon Header 3 x 2 bytes = 6 bytes: 0000 Reserved / 0100 icon / 0100 Numer of icons, total length will be 22 bytes for a single icon when finished
	$sIco = "0x000001000100"
	; Start of the Bitmap data header 1 x 4bytes: length of the header will be 40 bytes when finished. Will be appended to the end of the icon header when finished
	$sBmp = "28000000"
	; Get info about the HICON, this is mainly to get the pointers to the Color/Mask bitmaps data
	$aInfo = _WinAPI_GetIconInfo($HICON)
	; create a memory Compatable DC
	$hCDC = _WinAPI_CreateCompatibleDC(0)
	; create a BITMAPINFO Struct to store the Bitmap Info, it needs to be inilialized by setting the struct size.
	$tBI = DllStructCreate($tagBITMAPINFO)
	DllStructSetData($tBI, "Size", DllStructGetSize($tBI))
	; Pass a bitmap data pointer to the BITMAPINFO struct so we can recieve the details of the color bitmap data, we use it to write the headers
	_WinAPI_GetDIBits($hCDC, $aInfo[5], 0, 0, 0, DllStructGetPtr($tBI), 0)
	; Now we have some the basic info to add to the icon & Bitmap header so we'll add that to the headers.
	$sIco &= Hex(DllStructGetData($tBI, "Width"), 2) & Hex(DllStructGetData($tBI, "Height"), 2) & "00000100" & _
			__StringReverseBytes(Hex(DllStructGetData($tBI, "BitCount"), 4))
	$sBmp &= __StringReverseBytes(Hex(DllStructGetData($tBI, "Width"))) & __StringReverseBytes(Hex(DllStructGetData($tBI, "Height") * 2)) & "0100" & _
			__StringReverseBytes(Hex(DllStructGetData($tBI, "BitCount"), 4)) & "00000000"
	; Get the size of the Bitmap data from the BITMAPINFO Struct, we'll use this in the headers further on.
	$iSz = DllStructGetData($tBI, "SizeImage")
	; create a struct to store the Bitmap data Bits of the first bitmap, reset the BITMAPINFO struct
	$tBits = DllStructCreate("byte[" & DllStructGetData($tBI, "SizeImage") & "]")
	; Get the color bitmap dib bits into the $tBits struct.
	DllCall('gdi32.dll', 'int', 'GetBitmapBits', 'ptr', $aInfo[5], 'int', $iSz, 'ptr', DllStructGetPtr($tBits))
	; Get GetBitmapBits returns Bottom to Top dib, so I turn it to Top to Bottom dib ;)
	; ATM I'm only assuming that GetBitmapBits returns a Bottom to Top dib, maybe the bitmap bits you use could be Top Down already?.
	For $i = DllStructGetData($tBI, "SizeImage") + 1 To 0 Step -(DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))
		$sBD &= StringTrimLeft(BinaryMid(DllStructGetData($tBits, 1), $i, (DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))), 2)
	Next
	;Clear the BITMAPINFO & $tBits Struct as we'll use the same variables again for the mask bitmap data
	$tBits = 0
	$tBI = 0
	; create a BITMAPINFO Struct to store the Bitmap Info again, it needs to be inilialized by setting the struct size.
	$tBI = DllStructCreate($tagBITMAPINFO)
	DllStructSetData($tBI, "Size", DllStructGetSize($tBI))
	; Pass a bitmap data pointer to the BITMAPINFO struct so we can recieve the details of the bitmask bitmap data
	_WinAPI_GetDIBits($hCDC, $aInfo[4], 0, 0, 0, DllStructGetPtr($tBI), 0)
	; We've finished with the Compatable DC, delete it.
	_WinAPI_DeleteDC($hCDC)
	; Add the size of the of the color + bitmask bitmap data as we need this for both the icon & Bitmap header
	$iSz += DllStructGetData($tBI, "SizeImage")
	; combine the bitmap data size with the bitmap header, I'm padding the rest of the 40 byte bitmap header with 0's., that's the Bitmap header done
	$sBmp &= __StringReverseBytes(Hex($iSz)) & "00000000000000000000000000000000"
	; Now add the size of the Bitmap data + bitmap header size and combine the icon header together with the bitmap header and color bitmap data
	$sIco &= __StringReverseBytes(Hex($iSz + 40)) & __StringReverseBytes(Hex("22")) & $sBmp & $sBD
	; create a struct to store the Bitmap dib Bits of the mask bitmap
	$tBits = DllStructCreate("byte[" & DllStructGetData($tBI, "SizeImage") & "]")
	; Get the mask bitmap dib bits into the $tBits struct.
	DllCall('gdi32.dll', 'int', 'GetBitmapBits', 'ptr', $aInfo[4], 'int', DllStructGetData($tBI, "SizeImage"), 'ptr', DllStructGetPtr($tBits))
	; Get GetBitmapBits returns Bottom to Top dib, so I turn it to a Top to Bottom dib and append the mask bitmap data to the icon
	For $i = DllStructGetData($tBI, "SizeImage") + 1 To 0 Step -(DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))
		$sIco &= StringTrimLeft(BinaryMid(DllStructGetData($tBits, 1), $i, (DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))), 2)
	Next
	; Write the icon to a file.
	$FO = FileOpen($sOutIcon, 18)
	FileWrite($sOutIcon, Binary($sIco))
	FileClose($FO)
	; Clear the structs
	$tBits = 0
	$tBI = 0
EndFunc   ;==>__CreateIconFileFromHICON

; Reverse Byte String
Func __StringReverseBytes($sByte)
	Local $aX = StringRegExp($sByte, "(.{2})", 3), $sX = ''
	For $i = UBound($aX) - 1 To 0 Step -1
		$sX &= $aX[$i]
	Next
	Return $sX
EndFunc   ;==>__StringReverseBytes

Func __GDIPlus_BitmapCreateHICONFromBitmap($hBitmap)
	Local $HICON

	$HICON = DllCall($ghGDIPDll, "int", "GdipCreateHICONFromBitmap", "hwnd", $hBitmap, "int*", 0)
	If @error Then Return SetError(@error, 0, -1)
	Return SetError($HICON[0], 0, $HICON[2])
EndFunc   ;==>__GDIPlus_BitmapCreateHICONFromBitmap
 

Zaramot

I ♥ AutoIt
Сообщения
1,160
Репутация
660
Так как уменьшить то? :smile:
Код:
#include <GDIPlus.au3>

$Image = FileOpenDialog('Resize Image', '', 'BMP Image (*.bmp)', '', '', GUICreate(''))
_ImageResize($Image, @ScriptDir & "\Bliss.jpg", 400, 300)

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)

    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ;Start GDIPlus
    _GDIPlus_Startup()

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iW)

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
        $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()
EndFunc
 
Автор
Ganibal95

Ganibal95

GreenBytes
Сообщения
877
Репутация
240
Вроде все... :smile:
всем спс за участие
 
Автор
Ganibal95

Ganibal95

GreenBytes
Сообщения
877
Репутация
240
Размер у ico и png не меняет!!! Остается прежним или 0 байт...
Размер - Уменьшение картинки(Подгонка под новые пиксели)
 

Medic84

Омега
Команда форума
Администратор
Сообщения
1,590
Репутация
341
http://autoit-script.ru/index.php/topic,678.0.html
Писал программу для конвертации давненько - одна из первых моих программ =)
 
Верх