从流中读出jpg,为什么不行,高手指点 Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiMultimedia/html/delphi_20060927172223286.html
var
stmrcv:tmemorystream;
jp:tjpegimage;
stmrcv.CopyFrom (adata,adata.Size ) ;
stmrcv.Position:=0;
jp.LoadFromStream(stmrcv);为什么最后这句不执行?
adata中的数据可能不是TJPEGImage所支持的
另外adata也需要position := 0;
传输的时候是一个jpg saveto的,
stmrcv.CopyFrom (adata,adata.Size ) ;这句之后看了下stmrcv的大小和传出的流大小一样,就是到了jp.LoadFromStream(stmrcv);不执行了
大小一样你确定数据也一样吗?
用SaveToFile保存到文件,然后分别比较一下传输前和传输后以及CopyFrom前后的文件内容
传输前的流SaveToFile都是jpg,但stmrcv.CopyFrom (adata,adata.Size )后stmrcv在SaveToFile后出现的是一个大小一样的jpg文件,但打不开,每一步都position := 0了,不知道问题出在哪一步上
stmrcv你有没初始化过?
mark
多半是少了stmrcv.seek(0, soFromBeginning)
stmrcv, jp没看见建立对象语句
你把流保存成文件,看看是不是正确的JPEG文件。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
j, j2: TJPEGImage;
ms, ms2: TMemoryStream;
begin
bmp := TBitmap.Create;
bmp.Width := ClientWidth;
bmp.Height := ClientHeight;
PaintTo(bmp.Canvas, 0, 0);
j := TJPEGImage.Create;
j.Assign(bmp);
FreeAndNil(bmp);
ms := TMemoryStream.Create;
j.SaveToStream(ms);
FreeAndNil(j);
///////////////////////////////////////////////////
ms2 := TMemoryStream.Create;
ms.Position := 0;
ms2.CopyFrom(ms, ms.Size);
FreeAndNil(ms);
j2 := TJPEGImage.Create;
ms2.Position := 0;
j2.LoadFromStream(ms2);
FreeAndNil(ms2);
j2.SaveToFile('c:\1.jpg');
FreeAndNil(j2);
end;
end.