[格式转换 TBitmap,OleVariant,TStream]里面的代码怎么会达不到要求呢 ( 积分: 50 )

  • 主题发起人 主题发起人 colonel
  • 开始时间 开始时间
C

colonel

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;PicStream: OleVariant;<br> &nbsp;vStream: TFileStream;<br> &nbsp;vStreamA: TMemoryStream;<br>begin<br> &nbsp;if OpenDialog1.Execute then<br> &nbsp;begin<br> &nbsp; &nbsp;vStream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);<br> &nbsp; &nbsp;vStreamA:= TMemoryStream.Create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Image1.Picture.Bitmap.LoadFromStream(vStream);<br> &nbsp; &nbsp; &nbsp;PicStream:= StreamToVariant(vStream);<br><br> &nbsp; &nbsp; &nbsp;VariantToStream(PicStream, vStreamA);<br> &nbsp; &nbsp; &nbsp;Image2.Picture.Bitmap.LoadFromStream(vStreamA);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;freeandnil(vStream);<br> &nbsp; &nbsp; &nbsp;freeandnil(vStreamA);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>//------------------------------------<br>procedure VariantToStream(const Data: OleVariant; Stream: TStream);<br>var <br> p: Pointer; <br>begin <br> p := VarArrayLock(Data);<br> try <br> &nbsp; Stream.Write(p^, VarArrayHighBound(Data,1) + 1); &nbsp;<br> finally <br> &nbsp; VarArrayUnlock(Data);<br> end; <br>end; <br><br>function StreamToVariant(Stream: TStream): OleVariant;<br>var <br> p: Pointer; <br>begin <br> Result := VarArrayCreate([0, Stream.Size - 1], varByte); <br> p := VarArrayLock(Result); <br> try <br> &nbsp; Stream.Position := 0; &nbsp;<br> &nbsp; Stream.Read(p^, Stream.Size); <br> finally <br> &nbsp; VarArrayUnlock(Result); <br> end; <br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;PicStream: OleVariant;<br> &nbsp;vStream: TFileStream;<br> &nbsp;vStreamA: TMemoryStream;<br>begin<br> &nbsp;if OpenDialog1.Execute then<br> &nbsp;begin<br> &nbsp; &nbsp;vStream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);<br> &nbsp; &nbsp;vStreamA:= TMemoryStream.Create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Image1.Picture.Bitmap.LoadFromStream(vStream);<br> &nbsp; &nbsp; &nbsp;PicStream:= StreamToVariant(vStream);<br><br> &nbsp; &nbsp; &nbsp;VariantToStream(PicStream, vStreamA);<br> &nbsp; &nbsp; &nbsp;Image2.Picture.Bitmap.LoadFromStream(vStreamA);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;freeandnil(vStream);<br> &nbsp; &nbsp; &nbsp;freeandnil(vStreamA);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>//------------------------------------<br>procedure VariantToStream(const Data: OleVariant; Stream: TStream);<br>var <br> p: Pointer; <br>begin <br> p := VarArrayLock(Data);<br> try <br> &nbsp; Stream.Write(p^, VarArrayHighBound(Data,1) + 1); &nbsp;<br> finally <br> &nbsp; VarArrayUnlock(Data);<br> end; <br>end; <br><br>function StreamToVariant(Stream: TStream): OleVariant;<br>var <br> p: Pointer; <br>begin <br> Result := VarArrayCreate([0, Stream.Size - 1], varByte); <br> p := VarArrayLock(Result); <br> try <br> &nbsp; Stream.Position := 0; &nbsp;<br> &nbsp; Stream.Read(p^, Stream.Size); <br> finally <br> &nbsp; VarArrayUnlock(Result); <br> end; <br>end;
 
你的流的位置忘了复位了:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;PicStream: OleVariant;<br> &nbsp;vStream: TFileStream;<br> &nbsp;vStreamA: TMemoryStream;<br>begin<br> &nbsp;if OpenDialog1.Execute then<br> &nbsp;begin<br> &nbsp; &nbsp;vStream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);<br> &nbsp; &nbsp;vStreamA:= TMemoryStream.Create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Image1.Picture.Bitmap.LoadFromStream(vStream);<br> &nbsp; &nbsp; &nbsp;PicStream:= StreamToVariant(vStream);<br><br> &nbsp; &nbsp; &nbsp;VariantToStream(PicStream, vStreamA);<br> &nbsp; &nbsp; &nbsp;[red]vStreamA.Position := 0;[/red]<br> &nbsp; &nbsp; &nbsp;Image2.Picture.Bitmap.LoadFromStream(vStreamA);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;freeandnil(vStream);<br> &nbsp; &nbsp; &nbsp;freeandnil(vStreamA);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>=^0^=
 
接受答案了.
 
后退
顶部