怎样改变原有的尺寸320 X 240 ,达到1寸照片的效果,我用代码调整尺寸,但照片变形,请帮我看看程序或用其他的方法来实现,感谢!
unit GetPhotoUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,jpeg,AviCap, OleCtrls;type
TGetPhotoForm = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Shape2: TShape;
Image1: TImage;
Shape1: TShape;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
procedure BmpToJpg;
Procedure ChangePicSiz(SourceFileName,DescFileName:string);
public
{ Public declarations }
end;
const WM_CAP_START = WM_USER;
const WM_CAP_STOP = WM_CAP_START + 68;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
const WM_CAP_SAVEDIB = WM_CAP_START + 25;
const WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
const WM_CAP_SEQUENCE = WM_CAP_START + 62;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
const WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;
const WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
const WM_CAP_SET_SCALE=WM_CAP_START+ 53;
const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;function capCreateCaptureWindowA(lpszWindowName : PCHAR; dwStyle : longint; x : integer;
y : integer;nWidth : integer;nHeight : integer;ParentWin : HWND;
nId : integer): HWND;STDCALL EXTERNAL 'dll\AVICAP32.DLL';
var
GetPhotoForm: TGetPhotoForm;
hWndC : THandle;
implementation{$R *.dfm}
uses ProMainUnit;
procedure TGetPhotoForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
Action:=cafree;
end;procedure TGetPhotoForm.BitBtn1Click(Sender: TObject);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
//
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.Handle,0);
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.Handle,0);
if hWndC <> 0 then
begin
// SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
end;
end;procedure TGetPhotoForm.BitBtn2Click(Sender: TObject);
var Dc: HDC;
MyCanvas: TCanvas;
MyRect: TRect;
begin
Dc := GetWindowDC(0);
MyCanvas := TCanvas.Create;
SendMessage(hWndC,WM_CAP_SAVEDIB,0,longint(pchar(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp')));
try
MyCanvas.Handle := Dc;
MyRect:=Rect(0, 0,Image1.Width, Image1.Height);
Image1.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
end;
BmpToJpg;
Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
showmessage('照相成功!');
ProMainForm.Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg'); close;
end;procedure TGetPhotoForm.BmpToJpg;
var
Jpeg : TJPEGImage;
Bmp : TBitmap;
begin
Bmp := TBitmap.Create;
try
Bmp.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp');
Jpeg := TJPEGImage.Create;
try
Jpeg.Assign(bmp);
jpeg.CompressionQuality :=30;
Jpeg.Compress;
//保存图片
Jpeg.SaveToFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
ChangePicSiz(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg',ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg')
finally
Jpeg.Free;
end;
finally
Bmp.Free;
end;end;procedure TGetPhotoForm.BitBtn3Click(Sender: TObject);
begin close;
end;procedure TGetPhotoForm.ChangePicSiz(SourceFileName, DescFileName: string);
var
jpg:TJPEGImage;
bmp:TBitmap;
SourceJpg:TJPEGImage;
FWidth,FHeight:Integer;
begin
FWidth := 100;
FHeight := 126;
bmp := TBitmap.Create;
SourceJpg := TJPEGImage.Create;
Jpg:= TJPEGImage.Create;
try
//读取源文件
SourceJpg.LoadFromFile(SourceFileName);
//缩小
bmp.Width := FWidth;
bmp.Height := FHeight;
bmp.PixelFormat := pf24bit;
bmp.Canvas.StretchDraw(Rect(0, 0, FWidth, FHeight), SourceJpg);
//保存
jpg.Assign(bmp);
jpg.SaveToFile(DescFileName);
finally
bmp.Free;
jpg.Free;
SourceJpg.Free;
end; end;end.
unit GetPhotoUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,jpeg,AviCap, OleCtrls;type
TGetPhotoForm = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Shape2: TShape;
Image1: TImage;
Shape1: TShape;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
procedure BmpToJpg;
Procedure ChangePicSiz(SourceFileName,DescFileName:string);
public
{ Public declarations }
end;
const WM_CAP_START = WM_USER;
const WM_CAP_STOP = WM_CAP_START + 68;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
const WM_CAP_SAVEDIB = WM_CAP_START + 25;
const WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
const WM_CAP_SEQUENCE = WM_CAP_START + 62;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
const WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;
const WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
const WM_CAP_SET_SCALE=WM_CAP_START+ 53;
const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;function capCreateCaptureWindowA(lpszWindowName : PCHAR; dwStyle : longint; x : integer;
y : integer;nWidth : integer;nHeight : integer;ParentWin : HWND;
nId : integer): HWND;STDCALL EXTERNAL 'dll\AVICAP32.DLL';
var
GetPhotoForm: TGetPhotoForm;
hWndC : THandle;
implementation{$R *.dfm}
uses ProMainUnit;
procedure TGetPhotoForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
Action:=cafree;
end;procedure TGetPhotoForm.BitBtn1Click(Sender: TObject);
begin
if hWndC <> 0 then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
hWndC := 0;
end;
//
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.Handle,0);
hWndC := capCreateCaptureWindowA('GetPhotoForm',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,100,126,GetPhotoForm.Handle,0);
if hWndC <> 0 then
begin
// SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
end;
end;procedure TGetPhotoForm.BitBtn2Click(Sender: TObject);
var Dc: HDC;
MyCanvas: TCanvas;
MyRect: TRect;
begin
Dc := GetWindowDC(0);
MyCanvas := TCanvas.Create;
SendMessage(hWndC,WM_CAP_SAVEDIB,0,longint(pchar(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp')));
try
MyCanvas.Handle := Dc;
MyRect:=Rect(0, 0,Image1.Width, Image1.Height);
Image1.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
end;
BmpToJpg;
Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
showmessage('照相成功!');
ProMainForm.Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg'); close;
end;procedure TGetPhotoForm.BmpToJpg;
var
Jpeg : TJPEGImage;
Bmp : TBitmap;
begin
Bmp := TBitmap.Create;
try
Bmp.LoadFromFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.bmp');
Jpeg := TJPEGImage.Create;
try
Jpeg.Assign(bmp);
jpeg.CompressionQuality :=30;
Jpeg.Compress;
//保存图片
Jpeg.SaveToFile(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg');
ChangePicSiz(ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg',ExtractFilePath(Application.ExeName)+'Tmp\TmpPhoto.jpg')
finally
Jpeg.Free;
end;
finally
Bmp.Free;
end;end;procedure TGetPhotoForm.BitBtn3Click(Sender: TObject);
begin close;
end;procedure TGetPhotoForm.ChangePicSiz(SourceFileName, DescFileName: string);
var
jpg:TJPEGImage;
bmp:TBitmap;
SourceJpg:TJPEGImage;
FWidth,FHeight:Integer;
begin
FWidth := 100;
FHeight := 126;
bmp := TBitmap.Create;
SourceJpg := TJPEGImage.Create;
Jpg:= TJPEGImage.Create;
try
//读取源文件
SourceJpg.LoadFromFile(SourceFileName);
//缩小
bmp.Width := FWidth;
bmp.Height := FHeight;
bmp.PixelFormat := pf24bit;
bmp.Canvas.StretchDraw(Rect(0, 0, FWidth, FHeight), SourceJpg);
//保存
jpg.Assign(bmp);
jpg.SaveToFile(DescFileName);
finally
bmp.Free;
jpg.Free;
SourceJpg.Free;
end; end;end.
FGpBitmap := TGpBitmap.Create(FGpBitmapName);//FGpBitmapName = 文件名
self.Color := clBlack;
pg := TGPGraphics.Create(form.Canvas.Handle);
rc := GpRect(3,3,103,70);
pg.DrawImage(FGpBitmap,rc, 3, 3, FGpBitmap.Width, FGpBitmap.Height, utPixel);
finally
FGpBitmap.Free;
pg.Free;
end;
http://tg1a128.mail.163.com/netfolder/servlet/nfapp/GetFile?sid=sCfrQiGGJvuKBjmRdKGGQFbVRKhOvles&mid=1tbi7h90METM3Rl2uAAAs3
可以使用一个Image来把图片放到Image上,然后在在Image上调整就简单了,我以前就是这样取尺寸的。
不过VFW现在过时了!以前公司的视频用我这个的时候,有些摄相头的图象搞不出来
所以,建议楼主使用DirectShow吧!