First do the normal, store our local image, convert it into binary stream stored in the table corresponding to the database.
The code is as follows:
stringPath =".. /.. /a.jpg"; FileStream FS=NewFileStream (path, FileMode.Open); intStreamlength = (int) fs. Length;//gets the length of the file stream. byte[] image =New byte[Streamlength];//declares a byte array for saving picture filesFs. Read (Image,0, streamlength);//Convert a picture file to a byte array saveFS. Close (); varp =NewPictureurl {pictureUrl1=image}; Db.pictureUrl.InsertOnSubmit (p);//Here you use LINQ statements to implement an INSERT record. Db. SubmitChanges ();
This situation is used more, but there are other situations, such as we want to access a picture on the network, but do not want to download it locally, it is very troublesome, just want to pass the path of the picture, to turn it into
Binary stream, which is stored in the database.
The code is as follows
stringPath ="https://img3.doubanio.com/mpic/s8896281.jpg"; Uri URL=NewUri (path); WebRequest WebRequest=webrequest.create (URL); WebResponse WebResponse=Webrequest.getresponse (); Bitmap MyImage=NewBitmap (WebResponse.GetResponseStream ()); MemoryStream Ms=NewMemoryStream (); Myimage.save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); varp =NewPictureurl {pictureUrl1=Ms. ToArray ()}; Db.pictureUrl.InsertOnSubmit (P); Db. SubmitChanges ();
The code that reads the picture, like both, is displayed in the foreground through the image control
var from inch Select Picture ; = pictures. First (); New MemoryStream (MyPicture.pictureUrl1.ToArray ()); = Image.fromstream (Mymemorystream);
Operation Result:
Use C # to access network pictures and local pictures (in the form of binary streams) to SQL Sever