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 9079797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:55:15+00:00 2026-06-16T19:55:15+00:00

Following is the code to retrieve image from database and then saving it to

  • 0

Following is the code to retrieve image from database and then saving it to a folder.

public string BinarytoNewsImage(int ID)
        {
            byte[] btimage = null;
            string image = "";
            string filename = null;
            int mediaid;

            DataSet dsNews = new DataSet();
            adp = new SqlDataAdapter("Select Top 1 * from tblNew Where intNewId=" + ID, offcon);
            adp.Fill(dsNews, "tblNews1");
            if (dsNews.Tables["tblNews1"].Rows.Count > 0)
            {
                if (dsNews.Tables["tblNews1"].Rows[0]["strImage"] != DBNull.Value)
                {
                    btimage = (byte[])dsNews.Tables["tblNews1"].Rows[0]["strImage"];
                    mediaid = Convert.ToInt32(dsNews.Tables["tblNews1"].Rows[0]["intMediaId"].ToString());
                    filename = dsNews.Tables["tblNews1"].Rows[0]["strfilename"].ToString();
                    image = BinarytoImage(btimage, mediaid);
                }
                else
                {
                    filename = dsNews.Tables["tblNews1"].Rows[0]["strfilename"].ToString();
                    image = "http://www.patrika.com/media/" + filename;
                }
            }

            return image;
        }

        public string BinarytoImage(byte[] stream, int ID)
        {
            string ImagePath = "";
            string Image = ID + ".jpg";

            var URL = System.Configuration.ConfigurationManager.AppSettings["ImagePath"].ToString();

            string FolderName = new Uri(URL).LocalPath;

            var help = HttpContext.Current.Server.MapPath(FolderName);

            if (Directory.Exists(HttpContext.Current.Server.MapPath(FolderName)))
            {
                string[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath(FolderName), ID + ".jpg");
                if (files.Length > 0)
                {
                    ImagePath = URL + ID + ".jpg";
                }
                else
                {
                    using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                    {
                        MS.Write(stream, 0, stream.Length);

                        System.Drawing.Image img = System.Drawing.Image.FromStream(MS);

                        img.Save(help + ID + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
                        img.Dispose();
                        img = null;
                        ImagePath = URL + ID + ".jpg";
                    }
                }
            }
            return ImagePath;
        }

Everything is working fine the images are saving to a folder but my problem is images are getting blur after retrieval.

I just don’t know the reason as when I am using another code for retrieval than images are coming fine but are not saved to folder:

DataSet dsNews = new DataSet();
            adp = new SqlDataAdapter("Select Top 1 * from tblNew Where intNewId=901371", con);
            adp.Fill(dsNews, "tblNews1");

            if (dsNews.Tables["tblNews1"].Rows[0]["strImage"] != DBNull.Value)
            {
                byte[] btimage = (byte[])dsNews.Tables["tblNews1"].Rows[0]["strImage"];
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(btimage);
            }

I need those images to be saved to folder so that I don’t have to call database after once image comes.

  • 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-16T19:55:16+00:00Added an answer on June 16, 2026 at 7:55 pm

    Wouldn’t it help to change this line

        img.Save(help + ID + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
    

    to store it as JPEG rather? as that’s the source format

    EDIT:
    Your are not moving the stream pointer back to the start.

    Try change these lines:

                using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                    {
                        MS.Write(stream, 0, stream.Length);
    
                        System.Drawing.Image img = System.Drawing.Image.FromStream(MS);
                 ...
    

    To

                using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                    {
                        MS.Write(stream, 0, stream.Length);
                        MS.Seek(0, SeekOrigin.Begin);
                        System.Drawing.Image img = System.Drawing.Image.FromStream(MS);
    

    …

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

Sidebar

Related Questions

following code i used to retrieve Image from database through linq query but i
I'm using the following code to retrieve a json string from my webserver: var
I use the following code to retrieve image from the phone or SDCard and
I used the following code to retrieve an image from a url and display
I have the following code, which will retrieve a filename from a table and
I am using this code to retrieve and display an image from the web:
I'm using the following code to get the image filenames from an HTML file.
I have the following code: self.bg_br = mechanize.Browser() self.bg_br.retrieve(self.bg_imageurl, image2.jpg) self.bg_file2 = open(image.jpg, mode=w)
I want to crop an image retrieved from the database. I found the following
Currently I use the following code to retrieve the IP address of the local

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.