After assigning nil TImage.Picture.Graphic to clear the image, how can I use it again?

In the code below, I clear the image in btnSaveClick, later in btnLoadClick I want to assign the image to the image, but it gives AV because the Graphic object does not exist.

How can I complete the task?

procedure TForm1.btnSaveClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; buf: TBytes; z: integer; begin fs := TFileStream.Create('c:\temp\a.my', fmCreate); s := TMemoryStream.Create; try Image1.Picture.Graphic.SaveToStream(s); z := s.Size; SetLength(buf, z); s.Position := 0; s.ReadBuffer(buf[0], z); fs.WriteBuffer(z, SizeOf(integer)); fs.WriteBuffer(buf[0], z); finally s.Free; fs.Free; end; ShowMessage('ok'); Image1.Picture.Graphic := nil; end; procedure TForm1.btnLoadClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; buf: TBytes; z: integer; gc: TGraphicClass; begin fs := TFileStream.Create('c:\temp\a.my', fmOpenRead); s := TMemoryStream.Create; try fs.ReadBuffer(z, SizeOf(integer)); SetLength(buf, z); fs.ReadBuffer(buf[0], z); s.WriteBuffer(buf[0], z); s.Position := 0; Image1.Picture.RegisterFileFormat('jpg', 'jpeg files', gc); // Image1.Picture.Graphic.LoadFromStream(s); <-- AV here. Whats the proper way to do it? finally s.Free; fs.Free; end; ShowMessage('ok'); end; 
+4
source share
3 answers

If you download from the file an extension corresponding to the registered image class, you can simply:

 Picture.LoadFromFile(FileName); 

Otherwise, you can create an instance of a specific TGraphic descendant (depending on what type of graphics you use) in the code and assign it to the Graphic property, for example. for JPEG:

 var Graphic: TJpegImage; begin Graphic := TJpegImage.Create; try Graphic.LoadFromFile(FileName); Picture.Graphic := Graphic; finally Graphic.Free; end; end; 
+4
source

Instead of this

 Image1.Picture.Graphic := nil; 

You can do:

 Image1.Visible:= false; 

or

 Image1.Picture.Assign(HiddenImage2.Picture); 

Where HiddenImage2 is an empty TImage.

+2
source

You must tell TPicture which TGraphic class to use before you can load data into it. If you use only one graphic type, you can hardcode it, for example:

 procedure TForm1.btnSaveClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; z: integer; begin s := TMemoryStream.Create; try Image1.Picture.Graphic.SaveToStream(s); fs := TFileStream.Create('c:\temp\a.my', fmCreate); try z := s.Size; fs.WriteBuffer(z, SizeOf(integer)); s.Position := 0; fs.CopyFrom(s, z); finally fs.Free; end; finally s.Free; end; ShowMessage('ok'); Image1.Picture.Graphic := nil; end; procedure TForm1.btnLoadClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; z: integer; jpg: TJPEGImage; begin s := TMemoryStream.Create; try fs := TFileStream.Create('c:\temp\a.my', fmOpenRead); try fs.ReadBuffer(z, SizeOf(integer)); s.CopyFrom(fs, z); finally fs.Free; end; jpg := TJPEGImage.Create; try s.Position := 0; jpg.LoadFromStream(s); Image1.Picture.Graphic := jpg; finally jpg.Free; end; finally s.Free; end; ShowMessage('ok'); end; 

However, if you use several graphic types, you need to save the image type in a file and read it back to create the correct class object, for example:

 procedure TForm1.btnSaveClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; z: integer; str: AnsiString; begin str := Image1.Picture.Graphic.ClassName; s := TMemoryStream.Create; try Image1.Picture.Graphic.SaveToStream(s); fs := TFileStream.Create('c:\temp\a.my', fmCreate); try z := Length(str); fs.WriteBuffer(z, SizeOf(integer)); fs.WriteBuffer(Str[1], z); z := s.Size; fs.WriteBuffer(z, SizeOf(integer)); s.Position := 0; fs.CopyFrom(s, z); finally fs.Free; end; finally s.Free; end; ShowMessage('ok'); Image1.Picture.Graphic := nil; end; procedure TForm1.btnLoadClick(Sender: TObject); var fs: TFileStream; s: TMemoryStream; z: integer; str: AnsiString; g: TGraphic; begin s := TMemoryStream.Create; try fs := TFileStream.Create('c:\temp\a.my', fmOpenRead); try fs.ReadBuffer(z, SizeOf(integer)); SetLength(str, z); fs.ReadBuffer(str[1], z); fs.ReadBuffer(z, SizeOf(integer)); s.CopyFrom(fs, z); finally fs.Free; end; g := TGraphicClass(FindClass(str)).Create; try s.Position := 0; g.LoadFromStream(s); Image1.Picture.Graphic := g; finally g.Free; end; finally s.Free; end; ShowMessage('ok'); end; 
+2
source

Source: https://habr.com/ru/post/1368956/


All Articles