简体   繁体   中英

retrieve an image from database using linQ by asp.net

I want to get image from database using asp.net and linq I want to make a page when the user enter that page he see an image

Professor_Dim prof = sdc.Professor_Dims.SingleOrDefault(x => x.P_ID == 0);

if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0) {
  string fileName = FileUpload1.FileName;
  byte[] fileByte = FileUpload1.FileBytes;
  Binary binaryObj = new Binary(fileByte);
  prof.P_Image = binaryObj;
  sdc.SubmitChanges();
}

this code upload the image to database i want to retrieve that image in another page

ASP.net MVC

I did it a little bit different. I stored the byteData as is in the database and then did the following:

Bitmap bmp;

        byte[] img = //retrieve bytes from DB

        if (img == null)
            return new EmptyResult();

        using (MemoryStream ms = new MemoryStream(img))
        {
            bmp = new Bitmap(Image.FromStream(ms));
        }

If you're using ASP MVC you can return an ImageResult where you place the bmp as the Image and use for example for the ImageFormat ImageFormat.Jpeg.

ASP.NET

byte[] bytes = //retrieve the Image bytes from the database;
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    Image1.ImageUrl = "data:image/jpeg;base64," + base64String;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM