Timage save pictures to stream and take pictures from stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.