[RESOLVED]showing default image for user profile

Hi, in my project each user can save a picture for him/her profile.

now when a user log in I will show the user image and if the user has no profile image I will show an image in this url: "~/Images/default.png";

I’ve written this code. and i need help for else part to show the default image.

        public ActionResult GetProfilePhoto()
        {
            var image = (from u in db.UserProfiles
                         where u.UserName== User.Identity.Name
                         select u.Image).FirstOrDefault();
            if (image != null)
            {
                var stream = new MemoryStream(image.ToArray());
                return new FileStreamResult(stream, "image/jpeg");
            }
            else
            {

            }
        }

Hi,

You may convert a default image file from and set it in to variable "image".

            if (image != null)
            {
                var stream = new MemoryStream(image.ToArray());
                return new FileStreamResult(stream, "image/jpeg");
            }
            else
            {
               // convert a default image file from a folder to stream, and return the converted stream
            }

Or you can manipulate on .cshtml view like this:

            @if (item.Photo == null)
            {
                <img src="~/images/normal/defaultPhoto.png" width="50" />
            }
            else
            {
                <img src="data:image/png;base64,@Convert.ToBase64String(item.Photo, 0, item.Photo.Length)" width="50" />
            }

Have fun

how can i convert a default image file from a folder to stream

Leave a Reply