보여지는 틀보다 큰 사이즈의 이미지를 불러오려고 합니다...
물론 불어와는 지는데 제가 원하는 대로는 되지 않는군요...
예를 들면 틀보다 그림의 사이즈가 크기때문에 stretch를 쓰면 틀에 맞게 보여지기는 합니다. 또, 중앙에 정렬시킬 수도 있구요... 그런데, 제가 원하는 것은 틀에는 보여줄 수 있는 크기만 보여지구 마우스로 그림을 잡아서 옮긴다던지 하면서 전체를 훑어 볼 수 있는 것을 원하거든요...
혹시 제가 property, event를 잘 몰라서 그런 것일 수도 있습니다...
아시는 분 좀 알려주세요...
아래것은 컴포넌트이동이구요.
지금은 캠퍼스를 써서 이미지 확대,축소,이동,회전 예제입니다.
늦게 또 보내는것은 필요로 예제 만들다 여기저기 종합하다가
참고하시면 좋을것 같아서요...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, jpeg, ExtDlgs;
CONST
MaxPixelCount = 32768;
TYPE
pRGBArray = ^TRGBArray; // Use SysUtils.pByteArray for 8-bit color
TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple;
type
TForm1 = class(TForm)
ScrollBar2: TScrollBar;
Panel3: TPanel;
Label19: TLabel;
ScrollBar1: TScrollBar;
ScrollBar3: TScrollBar;
Image1: TImage;
Image2: TImage;
Button1: TButton;
OpenPictureDialog1: TOpenPictureDialog;
ScrollBar4: TScrollBar;
LabelRotateTime: TLabel;
Label1: TLabel;
Button2: TButton;
SavePictureDialog1: TSavePictureDialog;
procedure ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
var ScrollPos: Integer);
procedure FormShow(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
BitmapOriginal: TBitmap;
BitmapRotated : TBitmap;
iRotationAxis : INTEGER;
jRotationAxis : INTEGER;
procedure WMEraseBkgnd(var M: TWMEraseBkGnd); message WM_ERASEBKGND;
procedure Redraw;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
////////////////////////////////////////////////////////////////////////////////
procedure TForm1.FormCreate(Sender: TObject);
begin
iRotationAxis := 0;
jRotationAxis := 0;
BitmapOriginal := TBitmap.Create;
BitmapRotated := TBitmap.Create;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
//
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
BitmapOriginal.Free;
BitmapRotated.Free;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Redraw;
end;
////////////////////////////////////////////////////////////////////////////////
//이미지그릴때 깜박거림 없애기
procedure TForm1.WMEraseBkgnd(var M: TWMEraseBkGnd);
begin
M.Result := 1;
end;
////////////////////////////////////////////////////////////////////////////////
procedure TForm1.Redraw;
VAR
cosTheta : DOUBLE;
Delta : DWORD; // D3/D4 compatibility
i : INTEGER;
iOriginal : INTEGER;
iPrime : INTEGER;
iPrimeRotated : INTEGER;
j : INTEGER;
jOriginal : INTEGER;
jPrime : INTEGER;
jPrimeRotated : INTEGER;
RowOriginal : pRGBArray;
RowRotated : pRGBArray;
sinTheta : DOUBLE;
StartTime : DWORD;
Theta : DOUBLE;
TransX, TransY, diffX, diffY : Integer;
begin
//회전
BitmapRotated.Width := BitmapOriginal.Width;
BitmapRotated.Height := BitmapOriginal.Height;
BitmapRotated.PixelFormat := pf24bit;
StartTime := GetTickCount;
Theta := -(ScrollBar4.Position) * PI / 180;
sinTheta := SIN(Theta);
cosTheta := COS(Theta);
FOR j := BitmapRotated.Height-1 DOWNTO 0 DO
BEGIN
RowRotated := BitmapRotated.Scanline[j];
jPrime := 2*(j - jRotationAxis) + 1;
FOR i := BitmapRotated.Width-1 DOWNTO 0 DO
BEGIN
iPrime := 2*(i - iRotationAxis) + 1;
iPrimeRotated := ROUND(iPrime * CosTheta - jPrime * sinTheta);
jPrimeRotated := ROUND(iPrime * sinTheta + jPrime * cosTheta);
iOriginal := (iPrimeRotated - 1) DIV 2 + iRotationAxis;
jOriginal := (jPrimeRotated - 1) DIV 2 + jRotationAxis;
IF (iOriginal >= 0) AND (iOriginal <= BitmapOriginal.Width-1) AND
(jOriginal >= 0) AND (jOriginal <= BitmapOriginal.Height-1)
THEN BEGIN
RowOriginal := BitmapOriginal.Scanline[jOriginal];
RowRotated[i] := RowOriginal[iOriginal]
END
ELSE BEGIN
RowRotated[i].rgbtBlue := 255; // assign "corner" color
RowRotated[i].rgbtGreen := 0;
RowRotated[i].rgbtRed := 0
END
END
END;
Delta := GetTickCount - StartTime; // "stop" the clock
LabelRotateTime.Caption := 'Rotation Time = ' + IntToStr(Delta) + ' ms';
//--------------------------------------------------------------------------
//위치이동 //확대/축소
TransX := (ScrollBar1.Position);
TransY := (ScrollBar2.Position);
Image1.Picture := nil; //image1.Canvas.Brush.Color := clWhite;
diffX := Round((BitmapRotated.Width*ScrollBar3.Position)/100);
diffY := Round((BitmapRotated.Height*ScrollBar3.Position)/100);
StretchBlt(Image1.Canvas.Handle, TransX, TransY, BitmapRotated.Width + diffX, BitmapRotated.Height + diffY,
BitmapRotated.Canvas.Handle, 0, 0, BitmapRotated.Width, BitmapRotated.Height, SRCCOPY);
end;
////////////////////////////////////////////////////////////////////////////////
procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
var ScrollPos: Integer);
begin
Redraw;
end;
////////////////////////////////////////////////////////////////////////////////
FUNCTION LoadGraphicsFile(CONST Filename: STRING): TBitmap;
VAR
Extension: STRING;
{$IFDEF GIF}
GIFImage : TGIFImage;
{$ENDIF}
Icon : TIcon;
JPEGImage: TJPEGImage;
Metafile : TMetafile;
BEGIN
RESULT := NIL; // In case anything goes wrong
IF FileExists(Filename)
THEN BEGIN
Extension := UpperCase( COPY(Filename, LENGTH(Filename)-2, 3) );
// Quick and dirty check that file type is OK
ASSERT( (Extension = 'BMP') OR
(Extension = 'EMF') OR
{$IFDEF GIF}
(Extension = 'GIF') OR
{$ENDIF}
(Extension = 'ICO') OR
(Extension = 'JPG') OR
(Extension = 'WMF') );
RESULT := TBitmap.Create;
// BMP File -- no additional work to get TBitmap
IF Extension = 'BMP'
THEN RESULT.LoadFromFile(Filename);
{$IFDEF GIF}
// GIF File
IF Extension = 'GIF'
THEN BEGIN
GIFImage := TGIFImage.Create;
TRY
GIFImage.LoadFromFile(Filename);
RESULT.Height := GIFImage.Height;
RESULT.Width := GIFImage.Width;
RESULT.PixelFormat := pf24bit;
RESULT.Canvas.Draw(0,0, GIFImage)
FINALLY
GIFImage.Free
END
END;
{$ENDIF}
// ICO File
IF Extension = 'ICO'
THEN BEGIN
Icon := TIcon.Create;
TRY
TRY
Icon.LoadFromFile(Filename);
RESULT.Height := Icon.Height;
RESULT.Width := Icon.Width;
RESULT.PixelFormat := pf24bit; // avoid palette problems
RESULT.Canvas.Draw(0,0, Icon)
EXCEPT
// Ignore problem icons, e.g., Stream read errors
END;
FINALLY
Icon.Free
END
END;
// JPG File
IF Extension = 'JPG'
THEN BEGIN
JPEGImage := TJPEGImage.Create;
TRY
JPEGImage.LoadFromFile(Filename);
RESULT.Height := JPEGImage.Height;
RESULT.Width := JPEGImage.Width;
RESULT.PixelFormat := pf24bit;
RESULT.Canvas.Draw(0,0, JPEGImage)
FINALLY
JPEGImage.Free
END
END;
// Windows Metafiles, WMF or EMF
IF (Extension = 'WMF') OR
(Extension = 'EMF')
THEN BEGIN
Metafile := TMetafile.Create;
TRY
Metafile.LoadFromFile(Filename);
RESULT.Height := Metafile.Height;
RESULT.Width := Metafile.Width;
RESULT.PixelFormat := pf24bit; // avoid palette problems
RESULT.Canvas.Draw(0,0, Metafile)
FINALLY
Metafile.Free
END
END;
END;
// If Graphic is missing or invalid, create the "Red X"
IF RESULT = NIL
THEN BEGIN
RESULT.Height := 32;
RESULT.Width := 32;
RESULT.PixelFormat := pf24bit;
WITH RESULT.Canvas DO BEGIN
Pen.Color := clRed;
Pen.Width := 3;
MoveTo( 2, 2);
LineTo(29,29);
MoveTo( 2,29);
LineTo(29, 2);
END
END
END {LoadGraphicFile};
///////////////////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then begin
Image2.Picture.LoadFromFile(OpenPictureDialog1.FileName);
BitmapOriginal := LoadGraphicsFile(OpenPictureDialog1.FileName);
//BitmapRotated := LoadGraphicsFile(OpenPictureDialog1.FileName);
ScrollBar1.Max := BitmapOriginal.Width;
ScrollBar1.Min := -BitmapOriginal.Width;
ScrollBar2.Max := BitmapOriginal.Height;
ScrollBar2.Min := -BitmapOriginal.Height;
iRotationAxis := BitmapOriginal.Width DIV 2;
jRotationAxis := BitmapOriginal.Height DIV 2;
Redraw;
end;
end;
//////////////////////////////////////////////////////////////////////
procedure TForm1.Button2Click(Sender: TObject);
begin
if SavePictureDialog1.Execute then
BitmapRotated.SaveToFile(SavePictureDialog1.Filename)
end;
end.