본문 바로가기

프로그래밍/Delphi

StretchBitBlt를 이용한 이미지 확대 축소

This will paint directly to the screen just as example of not having to use a TImage. You'll have to modify for your needs...

procedure TForm1.Button2Click(Sender: TObject);
var jpg: TJPegImage;
    bmpInfo: TBitmapInfo;
    bits: Pointer;
    bmp: TBitmap;
    imgHeaderSize, ImgSize:  Cardinal;
    ScreenDC: HDC;
begin
  jpg:= TJpegImage.Create;
  bmp:= TBitmap.Create;
  ScreenDC:= getdc(0);
  try
    jpg.LoadFromFile('c:\temp\handshak.jpg');
    bmp.Assign(jpg);
    GetDIBSizes(bmp.Handle, imgHeaderSize, ImgSize);
    GetDIBSizes(jpg.Handle, imgHeaderSize, ImgSize);
    GetMem(bits, ImgSize);
    try
      GetDIB(bmp.Handle, bmp.Palette, bmpInfo, bits^);
      StretchDIBits(ScreenDC, //Image1.Canvas.Handle,
                    0, 0, Image1.Width, Image1.Height,
                    0, 0, jpg.Width, jpg.Height,
                    bits,
                    bmpInfo,
                    DIB_RGB_COLORS,
                    SRCCOPY);
    finally
      FreeMem(bits);
    end;
  finally
    jpg.Free;
    bmp.Free;
    ReleaseDC(0, ScreenDC);
  end;
end;