I have never tried to contribute to a newspaper. For the first time, I tried to contribute for a long time, But I logged out. Although I didn't have a draft fee, I was very happy.
The text is as follows:
In many cases, we need to save the images in the image box to the database and read them from the database. Although the image boxes in VB support direct binding to databases to store images and read images, many merge operations cannot be used to solve the problem. As a result, many people use the following method during operations: Read the image into the image box, and then useOpen "Picture" For Binary
To open the file and read it as a byte () array. When reading the image format from the database, you need to read the field data to byte () before usingOpen "Picture" For Binary
Use loadpicture to read the temporary files written to the disk. Writing and reading are redundant steps, wasting time and leaving temporary files. In the case of large images, the impact on efficiency is quite serious.
Is there a more convenient way to save the picture box image/read the picture in byte? Yes, there is a little-known object in VB:Propertybag,The object originally stores the data in the property of ActiveX and writes it to the file. However, we can use it to read the image in the byte () format. See the following code:
'Assume that the picture1 control already exists on the form and the image has been set.
Dim pbag as new propertybag
Dim B () as byte
Pbag. writeproperty "picture", picture1.picture
'Read the picture1.picture image and write it to the pbag container.
B = pbag. Contents 'get the byte of the container to the B () array.
'In this case, we want to obtain the byte array.
'Read byte () array content to picture
Pbag. Contents = B 'specifies the content of the pbag container.
'Read the image in the container
Set picture1.picture = pbag. readproperty ("picture ")
A few simple codes can be used to convert an image from byte () to byte. In addition, the original format is used for saving and reading the image. That is, if your image box is in JPG format, the saved byte () array is also an array in JPG format, instead of ordinary bitmap arrays, you do not have to worry about the uncompress and large size of the converted array.
In fact, the propertybag object does not stop reading and saving images. It is like a sack, which can hide anything and put it in the place you want to put it. It is up to you to decide what to hide. You can save any object as byte () and read it as needed.
For example, save the recordset object of ADO and read it as needed ......
Lotte