Because of a project, you have to save the picture to the database and then read it from the database as needed. At first, I thought it was simple, not just a stream. In fact, it's very simple, too. A lot of code, but it's all pits!
Look at the source of the timage, there is a loadfromstream and Savetostream under the picture.graphic. If you are not mistaken, then use these two functions directly. So, I did a demo test.
Put two timage.timage1 in the demo to put a picture, and then use the code to save the TImage1 picture to Stream.timage2 and then remove the image from the stream. Naturally.
This is my code.
picstream:=tmemorystream. Create ; Self.Image1.Picture.Graphic.SaveToStream (picstream); Picstream.position:=0; Self.Image2.Picture.Graphic.loadFromStream (picstream); Picstream.free;
But in fact, he made a mistake. So I Baidu and Baidu. The results are also written. (I don't understand why there are so many problematic codes on the Web.) is it a different version?
So I followed his tpicture.assign because if I copied Timage1 directly with Tpicture.assign, there was no error.
proceduretpicture.assign (source:tpersistent);begin ifSource =Nil ThenSetgraphic (Nil) Else ifSource isTpicture Thensetgraphic (Tpicture (Source). Graphic)Else ifSource isTgraphic Thensetgraphic (Tgraphic (Source))Else inheritedAssign (Source);End;
It turns out that when he calls assign, he calls Setgraphic to create the Tgraphic object.
So, if I still want to use the Graphic.loadfromstream function, You have to build this Tgraphic object by hand. Otherwise it must have been wrong. And this tgraphic has to be created according to different image formats and calls to different classtype. And I used a Tjpegimage object, Call his loadfromstream is able to read the stream directly, so I lazy a bit
Jpg:=tjpegimage. Create ; Picstream:=tmemorystream. Create ; Self.Image1.Picture.Graphic.SaveToStream (picstream); Picstream.position:=0; Jpg. Loadfromstream (picstream); Self.Image2.Picture.Assign (JPg); Picstream.free;
I don't read much in my book. This blog is a strange one. I don't want to write this blog. But see the site code under D10 is not available. So it's caught dead.
Timage save pictures to stream and take pictures from stream