KOL位图到Jpeg

时间:2012-05-19 00:05:24

标签: delphi delphi-7

我正在使用Delphi7和KOL组件以及JPegObj。 如何将PBitmap传输到PJpeg?

var 
 MyBitmap : PBitMap;
 MyJpeg   : PJpeg;
begin
 MyBitMap := ....;
 MyJPeg.Bitmap.Assign (MyBitMap); // ===> Wrong?
 MyJPeg.SaveToFile ('C:\test.jpg');
end;

感谢您的帮助。

编辑:想象一下:

enter image description here

编辑:我的代码:

program Project2;

{$APPTYPE CONSOLE}

uses
 Kol,
 JpegObj;

var
 Jpeg: PJpeg;
 Bitmap: PBitmap;

begin
 Bitmap := NewBitmap(50, 50);
 try
  Bitmap.Canvas.Brush.Color := $0000FF80;
  Bitmap.Canvas.Ellipse(0, 0, 50, 50);
  Jpeg := NewJpeg;
  try
   Jpeg.Bitmap := Bitmap;
   Jpeg.SaveToFile('test.jpg');
  finally
   Jpeg.Free;
  end;
 finally
 Bitmap.Free;
end;

运行时错误216 0041128E

编辑: 我在JpegObj中取消注释了这一行:

 {$DEFINE VER62} // if you plan to use .obj-files from Delphi7 distributive only!

现在程序刚刚冻结。

编辑:程序冻结在JpegObj

function __ftol: Integer;
 var
  f: double;
begin
 asm
  lea    eax, f             //  BC++ passes floats on the FPU stack
  fstp  qword ptr [eax]     //  Delphi passes floats on the CPU stack
 end;
 Result := Integer(Trunc(f));
end;

1 个答案:

答案 0 :(得分:4)

免责声明:以下内容已在Delphi 2009上测试过!

您无法在PJpeg.Bitmap.Assign实例上使用PJpeg,因为PJpeg.Bitmap在您访问它时为零,因为它未在PJpeg构造函数中实例化,也不会在您之后实例化正在与该实例合作。因此,尝试使用PJpeg.Bitmap会导致访问冲突。

尝试以这种方式分配位图(它基于JpegObj扩展名中的示例):

var 
  MyJpeg: PJpeg;
  MyBitmap: PBitmap;
begin
  // the MyBitMap has a picture assigned here
  MyJpeg := NewJpeg;
  try
    MyJpeg.Bitmap := MyBitmap;
    MyJpeg.SaveToFile('c:\test.jpg');
  finally
    MyJpeg.Free;
  end;
end;

这是一个VCL简约演示:

uses
  KOL, JPEGObj;

procedure TForm1.Button1Click(Sender: TObject);
var
  Jpeg: PJpeg;
  Bitmap: PBitmap;
begin
  Bitmap := NewBitmap(50, 50);
  try
    Bitmap.Canvas.Brush.Color := $0000FF80;
    Bitmap.Canvas.Ellipse(0, 0, 50, 50);
    Jpeg := NewJpeg;
    try
      Jpeg.Bitmap := Bitmap;
      Jpeg.SaveToFile('c:\image.jpg');
    finally
      Jpeg.Free;
    end;
  finally
    Bitmap.Free;
  end;
end;

令人惊讶的结果: - )

enter image description here

这是另一个控制台简约演示:

program Console;

{$APPTYPE CONSOLE}

uses
  SysUtils, KOL, JPEGObj;

var
  Jpeg: PJpeg;
  Bitmap: PBitmap;    
begin
  try
    Bitmap := NewBitmap(50, 50);
    try
      Bitmap.Canvas.Brush.Color := $0000CCFF;
      Bitmap.Canvas.Ellipse(0, 0, 50, 50);
      Jpeg := NewJpeg;
      try
        Jpeg.Bitmap := Bitmap;
        Jpeg.SaveToFile('c:\image.jpg');
      finally
        Jpeg.Free;
      end;
    finally
      Bitmap.Free;
    end;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

令人兴奋的结果: - )

enter image description here