Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:10:08+00:00 2026-06-18T16:10:08+00:00

I am storing images as byte arrays inside a SQL database. Here is my

  • 0

I am storing images as byte arrays inside a SQL database.

Here is my Index method (inside the controller):

public ActionResult Index()
{
    var list = db.UserProfiles.ToList();

    Dictionary<int, ActionResult> picture = list.ToDictionary(item => item.CarID, item => CarPic(item.Picture));

    ViewBag.CarPictures = picture;

    return View(list);
}

Here is my CarPic method:

public ActionResult CarPic(byte[] imageBytes)
{
    return imageBytes == null ? null : File(imageBytes, "image/jpeg");
}

Here is how I am trying to display the image inside my view:

foreach(var item in Model)
{
    <img src="@ViewBag.CarPictures[item.CarID]"/>
}

This is what shows up in the web browser:

Image

Here is a screenshot of Intellisense:

Intellisense

So Picture is not null, it is a byte array. The image itself is a JPEG. Here is the code I am using to convert the image to a byte array to store it in the database:

[Authorize]
[HttpPost]
public ActionResult Create(Stock stock, HttpPostedFileBase file)
{
    if (file != null)
    {
        if (file.ContentType.Contains("image"))
        {
            using (var inputStream = file.InputStream)
            {
                var memoryStream = inputStream as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    inputStream.CopyTo(memoryStream);
                }

                var data = memoryStream.ToArray();

                stock.Picture = data;
            }
        }
    }

    if (ModelState.IsValid)
    {
        db.UserProfiles.Add(stock);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(stock);
}

I have checked the Database and the field is indeed populated. Here is my Stock model:

[Table("Stock")]
public class Stock
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int CarID { get; set; }

    public byte[] Picture { get; set; }
    //etc etc
}

I am tearing my hair out here trying to work out why this isn’t working, as I think I have done everything right. What am I doing wrong?

Thanks very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:10:10+00:00Added an answer on June 18, 2026 at 4:10 pm

    You’re misunderstanding the <img> tag.

    <img src="..." /> allows you to display an image from a URL.
    The src attribute must specify a URL that returns the actual image.

    If you want to embed the image in the page, you can use a data: URI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am converting images to byte array and storing in a text file using
I would like to hash images, which have been converted to byte arrays. The
I'm trying to store images in a sql server database. I've got a column
I have a database that contains a table for storing URL Images (since storing
I have a database which stores .png images as the sql image type. I
I have a List<Employee> . Each Employee has a byte[] storing a picture (or
public Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageBytes =
Possible Duplicate: Storing Images in DB - Yea or Nay? For ages I've been
I just switched from storing my images uploaded via Carrierwave locally to using Amazon
I am storing a few images in MYSQL for security reasons. When I download

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.