Popular Posts

Sunday, December 8, 2013

Show images using model

Trick to show the image in MVC page. Image that is stored in database table:

The trick is to convert the bytes of image to string using this syntax:

ThumbnailPhotoString = "data:image/png;base64," + Convert.ToBase64String(product.ThumbnailPhoto),


Example:

foreach (var product in productDetail)
            {
                homePageProductDetailsDtos.Add(
                    new HomePageProductDetailsDto()
                    {
                        Name = product.Name,
                        Id = product.Id,
                        ProductDescription = product.Description,
                        ProductNumber = product.ProductNumber,
                        ListPrice = product.ListPrice,
                        SubCategoryId = product.ProductSubcategoryID,
                        LargPhotoString = "data:image/png;base64," + Convert.ToBase64String(product.LargPhoto),
                        ThumbnailPhotoString = "data:image/png;base64," + Convert.ToBase64String(product.ThumbnailPhoto),
                        LargePhotoFileName = product.LargePhotoFileName,
                        ThumbnailPhotoFileName = product.ThumbnailPhotoFileName,
                        ThumbnailUrl =  product.ThumbnailPhotoFileName
                    });
            }

No comments:

Post a Comment