简体   繁体   English

通过asp.net使用linQ从数据库检索图像

[英]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 我想使用asp.netlinq从数据库中获取图像,我想在用户进入该页面时创建一个页面,他看到一个图像

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 ASP.net MVC

I did it a little bit different. 我做了一点不同。 I stored the byteData as is in the database and then did the following: 我将byteData按原样存储在数据库中,然后执行以下操作:

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 MVC,则可以返回ImageResult,在其中将bmp放置为Image并用于ImageFormat ImageFormat.Jpeg例如。

ASP.NET 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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