1

I have avatars stored in a database. I have used the the data type image for the avatars. I want to load the avatar of a user into an image control, but i cant get it to work. I have tried this code without any luck:

public void GetUserAvatar()
    {
        string username = Convert.ToString(Session["username"]);

        var image = from u in dc.Users
                    where u.username == username
                    select u.image;
        imgAvatar.Controls.Add(image);
    }

I think i might have to save the images to a folder and then save the file path in the database instead. This seems to be easier and smoother. Any ideas?

I ended up saving the image to a folder and then saving the path to the database. My code for image uploading now looks like this:

public void addImage()
    {
        if (fuAvatar.PostedFile.ContentType.ToLower().StartsWith
            ("image") && fuAvatar.HasFile)
        {
            string saveLocation = Server.MapPath("savedAvatars/");
            string fileExtension = System.IO.Path.GetExtension(fuAvatar.FileName);
            string fileName = Convert.ToString(Session["userid"]);
            string savePath = saveLocation + fileName + fileExtension;
            fuAvatar.SaveAs(savePath);

            string imageDBPath = fileName + fileExtension;

            LinqClass1DataContext dc = new LinqClass1DataContext();
            int userid = Convert.ToInt32(Session["userid"]);
            var tblUser = (from u in dc.Users
                           where u.userid == userid
                           select u).First();
            tblUser.imagePath = @"\savedAvatars\"+imageDBPath;
            dc.SubmitChanges();
            lblResult.Text = "Avatar lastet opp!";
        }
        else lblResult.Text = "Avatar ikke lastet opp!";
    }

And to load the picture:

public void GetUserAvatar()
   {
       int userid = Convert.ToInt32(Session["userid"]);

       var varPath = dc.Users.Single(u => (u.userid == userid)).imagePath;

       string imagePath = Convert.ToString(varPath);
       imgAvatar.ImageUrl = imagePath;
   }
Twistar
  • 782
  • 5
  • 21
  • 35

3 Answers3

3

you should add a generic handler file (.ashx) and write in it something like that:

string sql = "select image from table1";
SqlCommand command = new SqlCommand(sql, con);
SqlDataReader dReader = command.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Source"]);
dReader.Close();

then in your page do something like this:

img.ImageUrl = "Handler.ashx;
Alex
  • 5,971
  • 11
  • 42
  • 80
  • Do I have to create a new method in this file or can I just use the public void ProcessRequest(HttpContext context)? – Twistar Mar 27 '12 at 09:12
  • you don't need to create a new method, actually you should use the ProcessRequest method :) – Alex Mar 27 '12 at 09:17
  • A little more info can be found here: http://www.codeproject.com/Articles/33310/C-Save-and-Load-Image-from-Database – lko Nov 30 '12 at 11:32
0

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 read this article, first I've found. Basically you need to create a handler which responds with the raw image.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
0

Please find the answer in below link.

Get images from database using Handler

You will have to create a handler which will load the image and u can pass the source to image tag.

Community
  • 1
  • 1
Murtaza
  • 3,045
  • 2
  • 25
  • 39