画像をjpgで保存したいです。
tmj 2013/4/13(Sat) 02:26:06|NO.53536
単純な画像切り取りソフトを作っています。
任意の画像をHSPウィンドウで開いて、任意の2点をクリックすると
その部分の画像がサクッと保存できるという内容です。
BMPでの保存までは出来たのですが、
Windows API BitBlt を良く理解出来てなく、JPGで保存するモジュールを
使いこなせていません。
下記に現在のソースを貼り付けます。
切り取られた画像はペーストされてるのですが、
その画像のサイズにウィンドウサイズを合わせる事ができず、
元の画像とおなじになり、余白が黒くなります。
ウィンドウを画像ピッタリにするにはどうしたらよいでしょうか
#include "hspcv.as"
#include "hgimg3.as"
; 2013/4/8 画像切り取りソフト
; マウスでドラッグした所を自動的に保存するソフト
; まず、ドラッグアンドドロップで画像を表示する。
; 切り取る座標をマウスクリックで指定してキャプチャする
;スクリーンショット--------------------------------------------
; http://chokuto.ifdef.jp/advanced/capturescreen.html
/*
#uselib "gdi32.dll"
#cfunc CreateDC "CreateDCA" sptr,sptr,sptr,int
#func DeleteDC "DeleteDC" int
#func BitBlt "BitBlt" int,int,int,int,int,int,int,int,int
*/
#define NULL 0
#define SRCCOPY 0x00CC0020
#define CAPTUREBLT 0x40000000
;スクリーンショット--------------------------------------------
;ドラッグアンドドロップ-----------------------------------------
#uselib "shell32.dll"
#func DragAcceptFiles "DragAcceptFiles" int,int
#func DragQueryFile "DragQueryFileA" int,int,int,int
#func DragQueryPoint "DragQueryPoint" int,int
#func DragFinish "DragFinish" int
#define WM_DROPFILES 0x0233
;ドラッグアンドドロップ-----------------------------------------
; WM_DROPFILES メッセージハンドラを設定
oncmd gosub *OnDropFiles, WM_DROPFILES
cnt1 = 0: x1 = 0: x2 = 0: y1 = 0: y2 = 0:
//v06 JPG保存モジュール/////////////////////////////////////////////////////////////////////////
#module picsave
#uselib "user32.dll"
#func GetWindowRect "GetWindowRect" int,var
#func GetClientRect "GetClientRect" int,var
#cfunc GetDC "GetDC" int
#func ReleaseDC "ReleaseDC" int,int
#uselib "gdi32.dll"
#func BitBlt "BitBlt" int,int,int,int,int,int,int,int,int
#cfunc CreateCompatibleDC "CreateCompatibleDC" int
#cfunc CreateCompatibleBitmap "CreateCompatibleBitmap" int,int,int
#cfunc SelectObject "SelectObject" int,int
#func DeleteDC "DeleteDC" int
#func DeleteObject "DeleteObject" int
#uselib "gdiplus.dll"
#func GdiplusStartup "GdiplusStartup" var,var,int
#func GdipCreateBitmapFromHBITMAP "GdipCreateBitmapFromHBITMAP" int,int,var
#func GdipSaveImageToFile "GdipSaveImageToFile" var,var,var,var
#func GdipDisposeImage "GdipDisposeImage" var
#func GdiplusShutdown "GdiplusShutdown" int
#uselib "Rpcrt4.dll"
#func UuidFromString "UuidFromStringA" str,var
#define global IMAGE_BMP "557CF400-1A04-11D3-9A73-0000F81EF32E"
#define global IMAGE_JPEG "557CF401-1A04-11D3-9A73-0000F81EF32E"
#define global IMAGE_GIF "557CF402-1A04-11D3-9A73-0000F81EF32E"
#define global IMAGE_TIFF "557CF405-1A04-11D3-9A73-0000F81EF32E"
#define global IMAGE_PNG "557CF406-1A04-11D3-9A73-0000F81EF32E"
#defcfunc GdipSave int mhwnd,str fn, str id,int q ,int x , int y, int sizex1, int sizey1
dim rc,4
sdim wfn,520
cnvstow wfn,fn
GetClientRect mhwnd,rc
rc(2)-= rc(0)
rc(3)-= rc(1)
mhdc = GetDC(mhwnd)
memdc = CreateCompatibleDC(mhdc)
hbm = CreateCompatibleBitmap(mhdc,rc(2),rc(3))
ret=SelectObject(memdc, hbm)
if( ret!=0 ){
;BitBlt memdc,0,0,rc(2),rc(3),mhdc,rc(0),rc(1),0x00cc0020
;BitBlt hdc, 0, 0, sizex, sizey, hdcScreen, x1 , y1 , SRCCOPY | CAPTUREBLT
dialog sizex1
BitBlt memdc,0,0,sizex1, sizey1,mhdc,x , y ,0x00cc0020
dim GdiplusStartupInput,4
GdiplusStartupInput(0) = 1,0,0,0
GdiplusStartup token,GdiplusStartupInput,0
if( stat==0 ){
GdipCreateBitmapFromHBITMAP hbm,0,pGpBitmap
if( stat==0 ){
dupptr GpBitmap,pGpBitmap,4
dim uuid,4
UuidFromString id,uuid
dim encode,8
eid="1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB"
UuidFromString eid,encode(1)
quality=q
encode(0)=1
encode(5)=1,4,varptr(quality)
GdipSaveImageToFile GpBitmap,wfn,uuid,encode
if( stat==0 ) : ret=1 : else : ret=0
GdipDisposeImage GpBitmap
}
GdiplusShutdown token
}
}
ReleaseDC hwnd,mhdc
DeleteDC memdc
DeleteObject hbm
return ret
#deffunc jpgsave str filename , int x1 , int y1, int sizex, int sizey
;dialog x1
return GdipSave(hwnd,filename,IMAGE_JPEG,100, x1, y1, sizex, sizey)
#deffunc gifsave str filename
return GdipSave(hwnd,filename,IMAGE_GIF,100)
#deffunc tiffsave str filename
return GdipSave(hwnd,filename,IMAGE_TIFF,100)
#deffunc pngsave str filename
return GdipSave(hwnd,filename,IMAGE_PNG,100)
#global
screen
cls 1
; ファイルをドラッグ&ドロップできるように設定
DragAcceptFiles hwnd, 1
;ドラッグアンドドロップ前処理ここまで-------------------
title "画像切り取りソフト"
;切り取り位置取得---------------------------------------
onclick goto *m1
stop
;ドラッグアンドドロップ-------------------------------------
*OnDropFiles
; --- ファイルがドロップされたとき ---
; ドロップされたファイルの数を取得する
hdrop = wParam ; ドロップファイル情報のハンドル
DragQueryFile hdrop, -1, 0, 0
ndrop = stat ; ファイルの数
; ファイル名を取得して表示
sdim filename, 260
DragQueryFile hdrop, cnt, varptr(filename), 260
fname=filename
cvload fname,0
screen 0,640 ,480 ,0 ,0 ,0
title fname
cvgetimg 0,1
stop
*m1
if(cnt1 == 0 ){
x1 = mousex: y1 = mousey
cnt1 = cnt1 + 1
}else{
x2 = mousex: y2 = mousey
mes1 = str(x1) + ":" + str(y1) + ":" + " " + str(x2) + ":" + str(y2)
dialog mes1
; 画面サイズのバッファ画面を作成
sizex = x2 - x1 : sizey = y2 - y1
mes2 = str(sizex) + " " + str(sizey)
dialog mes2
; buffer 2 , sizex , sizey
screen 2 , sizex , sizey , 2 , 0, 0,
;デスクトップ画面のデバイスコンテキスト取得
; hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL)
;デスクトップ画面の画像をコピー
; BitBlt hdc, x1, y1, sizex, sizey, hdcScreen, x1 , y1 , SRCCOPY | CAPTUREBLT
; BitBlt hdc, x1, y1, sizex, sizey, hdcScreen, x1 + 150 , y1 + 150 , SRCCOPY | CAPTUREBLT
; BitBlt hdc, 0, 0, sizex, sizey, hdcScreen, x1 + 10 , y1 + 30 , SRCCOPY | CAPTUREBLT
; BitBlt hdc, x1, y1, sizex, sizey, hdcScreen, x2 , y2 , SRCCOPY | CAPTUREBLT
;デバイスコンテキスト削除
; DeleteDC hdcScreen
;ビットマップファイルとして保存
gsel 0, 1 :
; screen 2, sizex, sizey
; gsel 2, 1
;JPG保存
#uselib "user32.dll"
#cfunc GetDesktopWindow "GetDesktopWindow"
jpgsave "Q:\\test.jpg" , x1 , y1, sizex, sizey
if(stat){
dialog "OK"
}else{
dialog "NG"
}
end
dialog "bmp", 17
if stat {
gsel 2 : bmpsave refstr
}
end
}
暇人 2013/4/13(Sat) 03:41:17|NO.53537
サイズ決めてるのは
>GetClientRect mhwnd,rc
> rc(2)-= rc(0)
> rc(3)-= rc(1)
ここだろうから
rc(2)=sizex1
rc(3)=sizey1
で、良いんじゃない?
tmj 2013/4/13(Sat) 10:53:03|NO.53541
お返事ありがとうございます。
やってみました。
ウィンドウサイズはリサイズされたのですが、画面が真っ白になって保存されてしまい、
切り取った画像の情報が引き継がれてません。
ウィンドウハンドラがmain部分とJPGモジュールで2つ生成されて、
それを整合すれば良いと思うのですが、APIが良くわかってないので
上手くいかないのです。
暇人 2013/4/13(Sat) 11:19:38|NO.53542
ん?
NO.53536のにNO.53537の修正を加えただけで
ちゃんと切り取った画像を保存できたよ?
tmj 2013/4/13(Sat) 16:53:27|NO.53543
あ、凡ミスやってました><
大変失礼しました。
解決しました。
ありがとうございます!!
tmj 2013/4/13(Sat) 16:54:33|NO.53544
解決しました
YSR 2013/4/16(Tue) 02:07:55|NO.53560
>NO.53544
まあ解決したんなら別にいいんだが、
>任意の画像をHSPウィンドウで開いて、任意の2点をクリックすると
>その部分の画像がサクッと保存できるという内容です。
>BMPでの保存までは出来たのですが、
>Windows API BitBlt を良く理解出来てなく、JPGで保存するモジュールを
>使いこなせていません。
これ、mousex,mouseyで座標把握→gcopyでバッファに複写→openCVを駆使してjpg保存、
すればよかったんじゃね?(いちいちAPIを叩かなくてもいい)