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

  • Home
  • SEARCH
  • 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 6029319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:52:08+00:00 2026-05-23T04:52:08+00:00

i got out of memory exception upon loading image from file.. i retrieve the

  • 0

i got out of memory exception upon loading image from file.. i retrieve the file from database and the output file image can be viewed..

String temp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/img.jpg";

using (FileStream fs = new FileStream(temp, FileMode.Create))
            {
                fs.Write(StudentImage(id), 0, StudentImage(id).Length);
                fParent.picAvatar.BackgroundImage = Image.FromFile(temp);
                //fParent.picAvatar.Image = Image.FromFile(temp2);
            }
private byte[] StudentImage(String _id)
    {
        try
        {
            String sqlCmd = String.Format("SELECT studpic FROM dbo.studentpic WHERE idnum = '{0}'", _id);
            using (SqlConnection con = new SqlConnection(gVar._conString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlCmd, con);
                using (SqlDataReader r = cmd.ExecuteReader())
                {
                    r.Read();

                    byte[] imgData = (byte[])r["studpic"];
                    return imgData;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }

did i miss any object that needs to close?

  • 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-05-23T04:52:09+00:00Added an answer on May 23, 2026 at 4:52 am

    You tend to get an OutOfMemory exception when an image cannot be read for a number of reasons.

    In your case the issue is that you have not closed your file! Also, you are calling the database twice.

    I suggest the following code:

    File.WriteAll(temp, StudentImage(id));
    fParent.picAvatar.Image = Image.FromFile(temp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My memory is 4G physical, but why I got out of memory exception even
I got an out of memory exception in a photo editor thingy i was
I was loading a Bitmap Image from a File. When I tried to save
Has anyone out there got a good set of instructions for building/compiling Ruby from
I've got a table with checkbox-style cells, and I can't figure out how to
....... I keep getting that nasty out of memory exception, for which I plan
I went to school for programming years ago and when I got out I
I've got a web app that sends out emails in response to a user-initaited
I got some legacy code with that caption spread out as comment almost in
My company has got quite a number of client laptops out in the field.

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.