From: pandeng@telepath.com (Steve Schafer (TeamB)) Subject: Re: TGraphic.LoadFromStream Date: 02 Oct 1999 00:00:00 GMT Message-ID: <37ff6b42.201656974@90.0.0.40> Content-Transfer-Encoding: 7bit References: <37F558E6.9F7D3229@compuserve.com> Content-Type: text/plain; charset=us-ascii Organization: TeamB Mime-Version: 1.0 Reply-To: pandeng@telepath.com Newsgroups: borland.public.delphi.graphics On Fri, 01 Oct 1999 17:59:18 -0700, Dennis Palmer <104446.2273@compuserve.com> wrote: >When reading the graphics back from the stream, how do I determine what >type of graphic is in the stream (TBitmap, TJPEGImage, etc.) so that I >call the proper LoadFromStream method. In principle, you can determine the format by decoding the first several bytes, but it would probably be better just to store the class name in the stream (this is how image data are stored in .DFM files, by the way). >I could save the type info in a string, but I'm not sure how to do that. procedure SaveGraphicToStream(Graphic: TGraphic; Stream: TStream); var L: Word; S: String; begin S := Graphic.ClassName; L := Length(S); Stream.Write(L, SizeOf(L)); Stream.Write(S[1], Length(S)); Graphic.SaveToStream(Stream) end; function LoadGraphicFromStream(Stream: TStream): TGraphic; var GraphicClass: TGraphicClass; L: Word; S: String; begin Stream.Read(L, SizeOf(L)); SetLength(S, L); Stream.Read(S[1], Length(S)); GraphicClass := FindClass(S) as TGraphicClass; Result := GraphicClass.Create; Result.LoadFromStream(Stream) end; Depending on the context in which you use the above code, you many need to call RegisterClass for each of the different kinds of graphic objects you plan to store, or else FindClass will raise an exception. -Steve