Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public ActionResult GetImage(string name)
       {
           byte[] cover = GetImageFromDataBase(name);
           if (cover != null)
           {
               return File(cover, "image/jpeg");
           }
           else
           {
               return null;
           }

       }

       public byte[] GetImageFromDataBase(string name)
       {
           RegisterLogic logic = new RegisterLogic();
           byte[] image = logic.GetImage(name);
           return image;
       }


My Logic is
C#
public byte[] GetImage(string name)
       {
           con.ConnectionString = str;
           cmd.Connection = con;
           if (con.State == ConnectionState.Open)
               con.Close();
           if (con.State == ConnectionState.Closed)
               con.Open();
           cmd.CommandText = "Select Picture from InspiniaMessage where UserName='" + name + "'";
           cmd.CommandType = CommandType.Text;

           SqlDataAdapter sda = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           Message m = new Message();
           foreach (DataRow dr in dt.Rows)
           {
               Message mn = new Message();
               mn.Picture = (byte[])(dr["Picture"]);
               m.Picture = mn.Picture;
           }

           return m.Picture;
           con.Close();
       }
   }


And my view is
HTML
<table id="tblMessage" class="table table-hover table-mail">
                        <tbody>
                            @foreach (var item in Model)
                            {
                                using (Html.BeginForm("Delete", "Mailbox", new { id = item.ID }))
                                {
                               
                                <tr class="read">
                                    <td class="check-mail">
                                        <input id="check" type="checkbox" name="check" class="i-checks">
                                    </td>
                                    <td class="mail-ontact" style="font-weight: bold">
                                        @item.UserName
                                    </td>
                                    <td class="mail-subject">
                                        @item.MessageText
                                    </td>
                                    <td>
                                      
                                      <img src="@Url.Action("GetImage", "Mailbox", new { name = @ViewData["UserName"] })"/>                           
                                    </td>
                                    <td>
                                        @item.Timing
                                    </td>

                                    <td>
                                        @* <button class="btn btn-danger btn-sm" data-toggle="modal" data-target=".bs-example-modal-sm" title="Reply" type="button"></button>*@
                                        <button id="delete" class="btn btn-white btn-sm" title="Move to trash" type="submit" value="Delete" >^__i class="fa fa-trash-o"></button>
                                    </td>
                                </tr>
                                }
                            }
                        </tbody>
                    </table>
Posted

1 solution

You should create a class, named ImageResult, that extends the ActionResult and override the ExecuteResult method like in the example below.

C#
public override void ExecuteResult(ControllerContext context)
{
    if (context == null)
        throw new ArgumentNullException("context");
    //
    try
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = this.ContentType;
        //
        if (this.ImageStream == null)
        {
            string filePath = context.HttpContext.Server.MapPath("/Content/noimageSmall.jpg");
            System.Drawing.Image imageIn = System.Drawing.Image.FromFile(filePath);
            MemoryStream ms = new MemoryStream();
            //
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
        }
        else
        {
            byte[] buffer = new byte[4096];
            //
            while (true)
            {
                int read = this.ImageStream.Read(buffer, 0, buffer.Length);
                if (read == 0)
                    break;
                //
                response.OutputStream.Write(buffer, 0, read);
            }
        }
        //
        response.End();
    }
    catch (Exception ex)
    {
        MvcBasicLog.LogException(ex);
    }
}


For details (and demo code) about this you could have a look on my next article (sub-chapters ImageResult and OpenFileResult): MVC Basic Site: Step 3 – Dynamic Layouts and Site Admin with: AJAX, jqGrid, Controller Extensions, HTML Helpers, and more[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900